@@ -29,8 +29,7 @@ import (
2929// this transport and use the Client rather than using this transport
3030// directly.
3131type 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