eventsource
eventsource copied to clipboard
fix go vet and go test -race
Here are a few fixes to satisfy go vet and test. I'm running these on circleci. We use them regularly and it might be worth enabling them for this repo (maybe even adding a badge). There is also travis if you'd prefer that. You can see the failures this fixes at:
https://circleci.com/gh/launchdarkly/eventsource/19
Looks good! Bit worried about the race condition fix, as users who had counted on SubscribeWith() following redirects will now have to configure their http.Client themselves:
https://github.com/donovanhide/eventsource/pull/35/commits/6fc0f23cc4a72344ced8336883bdaf4439f2f9bd#diff-e2d91404d5609adc9273e4603f6ce67dL73
I moved the redirecting logic into a new redirectingClient
, which I believe should be used instead of the defaultClient. I don't think the user has to set up anything. I think the one change is that we haven't altered the behavior of defaultClient
everywhere else it is used in the application/library.
If a user has created a Stream
with SubscribeWith
and provided their own http.Client
, then with this patch, the Client
will not have its CheckRedirect
func set to our checkRedirect
handler, which could break user code which depends on all the headers being copied. This seems more subtle than you might expect:
https://github.com/golang/go/issues/4800
Setting the CheckRedirect
func in library code is inherently racy... One solution is to export the RedirectingClient
and document its use, but I'm not aware of the exact situations when it should be used, although this commit gives some examples in the commit message:
https://github.com/donovanhide/eventsource/commit/aee4cf057cd8838b8c54680f4709845f5381f86a
Actually seems like copying headers on redirect was added in Go 1.8:
https://golang.org/doc/go1.8#net_http
Tricky decision is whether to remove the redirect functionality, and document Go 1.8 as a minimum requirement or to leave in backwards compatibility.