sdk-go icon indicating copy to clipboard operation
sdk-go copied to clipboard

Setting different HTTP clients rountrippers leads to use the last set roundripper for every client

Open pierDipi opened this issue 2 years ago • 1 comments
trafficstars

rt1 := // ... create rountripper 
p1 := http.New(http.WithRoundTripper(rt1))

rt2 := // ... create rountripper 
p2 := http.New(http.WithRoundTripper(rt2))

// unexpected:
// p1 has the same roundtripper rt2 as p2

This is caused by the use of the same underlying DefaultClient

https://github.com/cloudevents/sdk-go/blob/4fb49a39a22b41c03d956996313995cf35ec0ac2/v2/protocol/http/protocol.go#L104-L110

This can also technically cause data races.

Workaround

Specify a different client for each Protocol using WithClient:


rt1 := // ... create rountripper 
p1 := http.New(http.WithClient(nethttp.Client{Transport: rt1}))

rt2 := // ... create rountripper 
p2 := http.New(http.WithClient(nethttp.Client{Transport: rt2}))

pierDipi avatar Jul 18 '23 09:07 pierDipi

@pierDipi thx for flagging this. Wondering if we can close this issue since you document a workaround?

embano1 avatar Jul 28 '23 19:07 embano1