From 8c9f03a8e57eb486e42badaed3fb287da51807ba Mon Sep 17 00:00:00 2001 From: Bobby DeSimone Date: Mon, 20 Jan 2020 20:51:36 -0800 Subject: [PATCH] http: make http client request with context (#132) * http: make request with context - use ctx naming convention - use context.TODO() instead of nil Signed-off-by: Bobby DeSimone --- http.go | 5 +++-- http_test.go | 2 +- peers.go | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/http.go b/http.go index 56c9406..e0d391a 100644 --- a/http.go +++ b/http.go @@ -192,7 +192,7 @@ var bufferPool = sync.Pool{ New: func() interface{} { return new(bytes.Buffer) }, } -func (h *httpGetter) Get(context context.Context, in *pb.GetRequest, out *pb.GetResponse) error { +func (h *httpGetter) Get(ctx context.Context, in *pb.GetRequest, out *pb.GetResponse) error { u := fmt.Sprintf( "%v%v/%v", h.baseURL, @@ -203,9 +203,10 @@ func (h *httpGetter) Get(context context.Context, in *pb.GetRequest, out *pb.Get if err != nil { return err } + req = req.WithContext(ctx) tr := http.DefaultTransport if h.transport != nil { - tr = h.transport(context) + tr = h.transport(ctx) } res, err := tr.RoundTrip(req) if err != nil { diff --git a/http_test.go b/http_test.go index e2c9093..132c117 100644 --- a/http_test.go +++ b/http_test.go @@ -93,7 +93,7 @@ func TestHTTPPool(t *testing.T) { for _, key := range testKeys(nGets) { var value string - if err := g.Get(nil, key, StringSink(&value)); err != nil { + if err := g.Get(context.TODO(), key, StringSink(&value)); err != nil { t.Fatal(err) } if suffix := ":" + key; !strings.HasSuffix(value, suffix) { diff --git a/peers.go b/peers.go index f42e907..33e1fba 100644 --- a/peers.go +++ b/peers.go @@ -26,7 +26,7 @@ import ( // ProtoGetter is the interface that must be implemented by a peer. type ProtoGetter interface { - Get(context context.Context, in *pb.GetRequest, out *pb.GetResponse) error + Get(ctx context.Context, in *pb.GetRequest, out *pb.GetResponse) error } // PeerPicker is the interface that must be implemented to locate