playground: increase urlfetch timeout on GAE classic

The default timeout for urlfetch requests on App Engine Classic is
5 seconds. Sometimes the requests can take longer, so increase it to
the maximum value of 60 seconds. The playground has its own timeout for
running code so there's no need to impose a second level of protection.

Also cleans up the code to remove old appenginevm code and use the
correct imports.

Change-Id: I15da96e5ba70fb008bf821f4609f431847662223
Reviewed-on: https://go-review.googlesource.com/129395
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Andrew Bonventre <andybons@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Andrew Bonventre 2018-08-14 18:55:10 -04:00
parent a434f64ace
commit 8f8fd1f239
4 changed files with 33 additions and 21 deletions

View File

@ -7,20 +7,28 @@
package playground package playground
import ( import (
"context"
"io"
"net/http" "net/http"
"appengine" "google.golang.org/appengine"
"appengine/urlfetch" "google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
) )
func init() { func init() {
onAppengine = !appengine.IsDevAppServer() onAppengine = !appengine.IsDevAppServer()
} }
func client(r *http.Request) *http.Client { func contextFunc(r *http.Request) context.Context {
return urlfetch.Client(appengine.NewContext(r)) return appengine.NewContext(r)
}
func post(ctx context.Context, url, contentType string, body io.Reader) (*http.Response, error) {
return urlfetch.Client(ctx).Post(url, contentType, body)
} }
func report(r *http.Request, err error) { func report(r *http.Request, err error) {
appengine.NewContext(r).Errorf("%v", err) ctx := appengine.NewContext(r)
log.Errorf(ctx, "%v", err)
} }

View File

@ -1,11 +0,0 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build appenginevm
package playground
func init() {
onAppengine = true
}

View File

@ -9,10 +9,12 @@ package playground // import "golang.org/x/tools/playground"
import ( import (
"bytes" "bytes"
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"time"
) )
const baseURL = "https://golang.org" const baseURL = "https://golang.org"
@ -25,7 +27,7 @@ func init() {
func bounce(w http.ResponseWriter, r *http.Request) { func bounce(w http.ResponseWriter, r *http.Request) {
b := new(bytes.Buffer) b := new(bytes.Buffer)
if err := passThru(b, r); err != nil { if err := passThru(b, r); err != nil {
http.Error(w, "Server error.", http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
report(r, err) report(r, err)
return return
} }
@ -38,7 +40,9 @@ func passThru(w io.Writer, req *http.Request) error {
} }
defer req.Body.Close() defer req.Body.Close()
url := baseURL + req.URL.Path url := baseURL + req.URL.Path
r, err := client(req).Post(url, req.Header.Get("Content-type"), req.Body) ctx, cancel := context.WithTimeout(contextFunc(req), 60*time.Second)
defer cancel()
r, err := post(ctx, url, req.Header.Get("Content-type"), req.Body)
if err != nil { if err != nil {
return fmt.Errorf("making POST request: %v", err) return fmt.Errorf("making POST request: %v", err)
} }
@ -49,7 +53,7 @@ func passThru(w io.Writer, req *http.Request) error {
return nil return nil
} }
var onAppengine = false // will be overridden by appengine.go and appenginevm.go var onAppengine = false // will be overridden by appengine.go
func allowShare(r *http.Request) bool { func allowShare(r *http.Request) bool {
if !onAppengine { if !onAppengine {

View File

@ -7,12 +7,23 @@
package playground package playground
import ( import (
"context"
"fmt"
"io"
"log" "log"
"net/http" "net/http"
) )
func client(r *http.Request) *http.Client { func post(ctx context.Context, url, contentType string, body io.Reader) (*http.Response, error) {
return http.DefaultClient req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, fmt.Errorf("http.NewRequest: %v", err)
}
return http.DefaultClient.Do(req.WithContext(ctx))
}
func contextFunc(_ *http.Request) context.Context {
return context.Background()
} }
func report(r *http.Request, err error) { func report(r *http.Request, err error) {