Skip to content

Commit 8c9f03a

Browse files
desimonebradfitz
authored andcommitted
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 <[email protected]>
1 parent 215e871 commit 8c9f03a

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

http.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ var bufferPool = sync.Pool{
192192
New: func() interface{} { return new(bytes.Buffer) },
193193
}
194194

195-
func (h *httpGetter) Get(context context.Context, in *pb.GetRequest, out *pb.GetResponse) error {
195+
func (h *httpGetter) Get(ctx context.Context, in *pb.GetRequest, out *pb.GetResponse) error {
196196
u := fmt.Sprintf(
197197
"%v%v/%v",
198198
h.baseURL,
@@ -203,9 +203,10 @@ func (h *httpGetter) Get(context context.Context, in *pb.GetRequest, out *pb.Get
203203
if err != nil {
204204
return err
205205
}
206+
req = req.WithContext(ctx)
206207
tr := http.DefaultTransport
207208
if h.transport != nil {
208-
tr = h.transport(context)
209+
tr = h.transport(ctx)
209210
}
210211
res, err := tr.RoundTrip(req)
211212
if err != nil {

http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestHTTPPool(t *testing.T) {
9393

9494
for _, key := range testKeys(nGets) {
9595
var value string
96-
if err := g.Get(nil, key, StringSink(&value)); err != nil {
96+
if err := g.Get(context.TODO(), key, StringSink(&value)); err != nil {
9797
t.Fatal(err)
9898
}
9999
if suffix := ":" + key; !strings.HasSuffix(value, suffix) {

peers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
// ProtoGetter is the interface that must be implemented by a peer.
2828
type ProtoGetter interface {
29-
Get(context context.Context, in *pb.GetRequest, out *pb.GetResponse) error
29+
Get(ctx context.Context, in *pb.GetRequest, out *pb.GetResponse) error
3030
}
3131

3232
// PeerPicker is the interface that must be implemented to locate

0 commit comments

Comments
 (0)