cloudevents / sdk-go

Go SDK for CloudEvents
https://cloudevents.github.io/sdk-go/
Apache License 2.0
837 stars 223 forks source link

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

Closed pierDipi closed 5 months ago

pierDipi commented 1 year ago
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}))
embano1 commented 1 year ago

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