Skip to content

Commit 11ed51a

Browse files
committed
Use the context's deadline for urlfetch, and drop urlfetch.Transport's Deadline field.
Change-Id: I566ee598d3f5af45e44f18903c91edd3d2d379e7
1 parent 8a84f41 commit 11ed51a

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ This list summarises the differences:
5757
* `appengine.Datacenter` now takes a `context.Context` argument.
5858
* `datastore.PropertyLoadSaver` has been simplified to use slices in place of channels.
5959
* `search.FieldLoadSaver` now handles document metadata.
60+
* `urlfetch.Transport` no longer has a Deadline field; set a deadline on the
61+
`context.Context` instead.
6062
* `taskqueue.QueueStats` no longer takes a maxTasks argument. That argument has been
6163
deprecated and unused for a long time.
6264
* `appengine/aetest`, `appengine/blobstore`, `appengine/cloudsql`

urlfetch/urlfetch.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ import (
2929
// this transport and use the Client rather than using this transport
3030
// directly.
3131
type Transport struct {
32-
Context context.Context
33-
Deadline time.Duration // zero means 5-second default
32+
Context context.Context
3433

3534
// Controls whether the application checks the validity of SSL certificates
3635
// over HTTPS connections. A value of false (the default) instructs the
@@ -47,10 +46,13 @@ var _ http.RoundTripper = (*Transport)(nil)
4746
// Client returns an *http.Client using a default urlfetch Transport. This
4847
// client will have the default deadline of 5 seconds, and will check the
4948
// validity of SSL certificates.
50-
func Client(context context.Context) *http.Client {
49+
//
50+
// Any deadline of the provided context will be used for requests through this client;
51+
// if the client does not have a deadline then a 5 second default is used.
52+
func Client(ctx context.Context) *http.Client {
5153
return &http.Client{
5254
Transport: &Transport{
53-
Context: context,
55+
Context: ctx,
5456
},
5557
}
5658
}
@@ -136,11 +138,8 @@ func (t *Transport) RoundTrip(req *http.Request) (res *http.Response, err error)
136138
FollowRedirects: proto.Bool(false), // http.Client's responsibility
137139
MustValidateServerCertificate: proto.Bool(!t.AllowInvalidServerCertificate),
138140
}
139-
ctx := t.Context
140-
141-
if t.Deadline != 0 {
142-
freq.Deadline = proto.Float64(t.Deadline.Seconds())
143-
ctx, _ = context.WithTimeout(ctx, t.Deadline)
141+
if deadline, ok := t.Context.Deadline(); ok {
142+
freq.Deadline = proto.Float64(deadline.Sub(time.Now()).Seconds())
144143
}
145144

146145
for k, vals := range req.Header {
@@ -167,7 +166,7 @@ func (t *Transport) RoundTrip(req *http.Request) (res *http.Response, err error)
167166
}
168167

169168
fres := &pb.URLFetchResponse{}
170-
if err := internal.Call(ctx, "urlfetch", "Fetch", freq, fres); err != nil {
169+
if err := internal.Call(t.Context, "urlfetch", "Fetch", freq, fres); err != nil {
171170
return nil, err
172171
}
173172

0 commit comments

Comments
 (0)