4 ms·
Yep - (my understanding is) the Go HTTP stdlib module predates the concept of context in Golang, so the implementation was bolted on to ensure backwards compati
by maxwellg 3y ago
Yep - (my understanding is) the Go HTTP stdlib module predates the concept of context in Golang, so the implementation was bolted on to ensure backwards compatibility. NewRequestWithContext was only added in Go 1.13 [1]. Previously, requests were cancelled manually with CancelReqest [2]. This is an unfortunate wart of the language - it means it's very easy to accidentally spin up a new Request which doesn't inherit the parent context by calling NewRequest instead. And adding the context via the builder pattern means it's possible to introduce the storage bugs described in the article. My preferred way to consume a context would be to take it in when the work is actually about to be performed - e.g. client.Do(ctx, reqest)
1 - https://pkg.go.dev/net/http#NewRequestWithContext https://pkg.go.dev/net/http#NewRequestWithContext
2 - https://pkg.go.dev/net/http#Transport.CancelRequest https://pkg.go.dev/net/http#Transport.CancelRequest