http.Client interface

719 views
Skip to first unread message

omarsharif...@gmail.com

unread,
Dec 11, 2016, 5:07:42 AM12/11/16
to golang-nuts
Just a quick question - I've noticed that the http package doesn't have an interface for Client. How would I swap out a http.Client that is required by a third-party library with a another that supports, say retries with exponential back-off? It appears that it is not possible? Would the http package benefit from an interface for the http.Client?

Cheers!
Ben 

Kevin Conway

unread,
Dec 11, 2016, 2:59:12 PM12/11/16
to omarsharif...@gmail.com, golang-nuts
 How would I swap out a http.Client that is required by a third-party library with a another that supports, say retries with exponential back-off?
I'd suggest looking at http.Transport and http.RoundTripper [https://golang.org/pkg/net/http/#RoundTripper]. The docs explicitly forbid using RoundTripper to implement higher level protocol features like retry-with-backoff based on HTTP response codes, but those are your only hooks into making this a feature of the client rather than moving the retry logic somewhere else.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

omarsharif...@gmail.com

unread,
Dec 12, 2016, 4:55:26 AM12/12/16
to golang-nuts, omarsharif...@gmail.com
Yeah, I thought as much.

I will raise a pull request with the third-party package and implement the requirement of a http.Client as an interface that matches the http.Client methods used by the package.

Should the standard library http.Client provide an interface for http.Client so that third-party packages can accept that instead of the concrete stdlib http.Client, or is it the communities opinion that third-party packages should create an interface they accept, which covers the methods they call?

Cheers,
Ben

Dave Cheney

unread,
Dec 12, 2016, 5:25:10 AM12/12/16
to golang-nuts, omarsharif...@gmail.com
I don't think it's up to the std lib to define such an interface; that should be the responsibility of the third party package -- it should define an interface with the behaviour it requires

omarsharif...@gmail.com

unread,
Dec 12, 2016, 9:08:35 AM12/12/16
to golang-nuts, omarsharif...@gmail.com
Thats what I think too. Thanks for the confirmation, I'll raise a pull request with the aforementioned third-party package.

Thanks everyone.
Reply all
Reply to author
Forward
0 new messages