api: uses context from net/http

This commit removes the now unnecessary CancelableTransport and rely on
the net/http context support.
This commit is contained in:
André Carvalho 2017-04-20 10:31:18 -03:00
parent 11fae2ef0c
commit 09dcce7042
No known key found for this signature in database
GPG Key ID: 440C9C458D3A1E61
2 changed files with 22 additions and 28 deletions

View File

@ -23,19 +23,10 @@ import (
"path" "path"
"strings" "strings"
"time" "time"
"golang.org/x/net/context/ctxhttp"
) )
// CancelableTransport is like net.Transport but provides // DefaultRoundTripper is used if no RoundTripper is set in Config.
// per-request cancelation functionality. var DefaultRoundTripper http.RoundTripper = &http.Transport{
type CancelableTransport interface {
http.RoundTripper
CancelRequest(req *http.Request)
}
// DefaultTransport is used if no Transport is set in Config.
var DefaultTransport CancelableTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{ Dial: (&net.Dialer{
Timeout: 30 * time.Second, Timeout: 30 * time.Second,
@ -49,16 +40,16 @@ type Config struct {
// The address of the Prometheus to connect to. // The address of the Prometheus to connect to.
Address string Address string
// Transport is used by the Client to drive HTTP requests. If not // RoundTripper is used by the Client to drive HTTP requests. If not
// provided, DefaultTransport will be used. // provided, DefaultRoundTripper will be used.
Transport CancelableTransport RoundTripper http.RoundTripper
} }
func (cfg *Config) transport() CancelableTransport { func (cfg *Config) roundTripper() http.RoundTripper {
if cfg.Transport == nil { if cfg.RoundTripper == nil {
return DefaultTransport return DefaultRoundTripper
} }
return cfg.Transport return cfg.RoundTripper
} }
// Client is the interface for an API client. // Client is the interface for an API client.
@ -79,13 +70,13 @@ func New(cfg Config) (Client, error) {
return &httpClient{ return &httpClient{
endpoint: u, endpoint: u,
transport: cfg.transport(), client: http.Client{Transport: cfg.roundTripper()},
}, nil }, nil
} }
type httpClient struct { type httpClient struct {
endpoint *url.URL endpoint *url.URL
transport CancelableTransport client http.Client
} }
func (c *httpClient) URL(ep string, args map[string]string) *url.URL { func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
@ -103,8 +94,10 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
} }
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) { func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
resp, err := ctxhttp.Do(ctx, &http.Client{Transport: c.transport}, req) if ctx != nil {
req = req.WithContext(ctx)
}
resp, err := c.client.Do(req)
defer func() { defer func() {
if resp != nil { if resp != nil {
resp.Body.Close() resp.Body.Close()

View File

@ -14,14 +14,15 @@
package api package api
import ( import (
"net/http"
"net/url" "net/url"
"testing" "testing"
) )
func TestConfig(t *testing.T) { func TestConfig(t *testing.T) {
c := Config{} c := Config{}
if c.transport() != DefaultTransport { if c.roundTripper() != DefaultRoundTripper {
t.Fatalf("expected default transport for nil Transport field") t.Fatalf("expected default roundtripper for nil RoundTripper field")
} }
} }
@ -100,7 +101,7 @@ func TestClientURL(t *testing.T) {
hclient := &httpClient{ hclient := &httpClient{
endpoint: ep, endpoint: ep,
transport: DefaultTransport, client: http.Client{Transport: DefaultRoundTripper},
} }
u := hclient.URL(test.endpoint, test.args) u := hclient.URL(test.endpoint, test.args)