From c684076ff1c47651cd134b8b0f864a659cd0a712 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Tue, 16 Sep 2014 16:06:51 +0400 Subject: [PATCH] Initialize http client in NewClient func --- client/client.go | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/client/client.go b/client/client.go index 23eb076..060c2a1 100644 --- a/client/client.go +++ b/client/client.go @@ -44,16 +44,32 @@ type ( } ) -func (c *Config) UseDefaults() { - c.Host = "127.0.0.1" - c.Port = 4401 - c.Timeout = 60 * time.Second +func NewConfig() *Config { + return &Config{ + Host: "127.0.0.1", + Port: 4401, + Timeout: 60 * time.Second, + } } func NewClient(c *Config) *Client { + if c == nil { + c = NewConfig() + } + + transport := http.Transport{ + Dial: func(network, addr string) (net.Conn, error) { + return net.DialTimeout(network, addr, c.Timeout) + }, + } + + client := http.Client{ + Transport: &transport, + } + return &Client{ Config: c, - httpClient: &http.Client{}, + httpClient: &client, } } @@ -80,17 +96,7 @@ func (c *Client) Publish(m *Message) (ok bool) { func (c *Client) Subscribe(queues ...string) (m *Message) { url := c.url(subscribeEndpoint, "?queues=", strings.Join(queues, ",")) - transport := http.Transport{ - Dial: func(network, addr string) (net.Conn, error) { - return net.DialTimeout("tcp", addr, c.Config.Timeout) - }, - } - - client := http.Client{ - Transport: &transport, - } - - res, err := client.Get(url) + res, err := c.httpClient.Get(url) if err != nil { return }