1
0
Fork 0

Initialize http client in NewClient func

This commit is contained in:
Gregory Eremin 2014-09-16 16:06:51 +04:00
parent 030af0a7b0
commit c684076ff1
1 changed files with 22 additions and 16 deletions

View File

@ -44,16 +44,32 @@ type (
} }
) )
func (c *Config) UseDefaults() { func NewConfig() *Config {
c.Host = "127.0.0.1" return &Config{
c.Port = 4401 Host: "127.0.0.1",
c.Timeout = 60 * time.Second Port: 4401,
Timeout: 60 * time.Second,
}
} }
func NewClient(c *Config) *Client { 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{ return &Client{
Config: c, 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) { func (c *Client) Subscribe(queues ...string) (m *Message) {
url := c.url(subscribeEndpoint, "?queues=", strings.Join(queues, ",")) url := c.url(subscribeEndpoint, "?queues=", strings.Join(queues, ","))
transport := http.Transport{ res, err := c.httpClient.Get(url)
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)
if err != nil { if err != nil {
return return
} }