Skip to content

Commit

Permalink
atomic http client
Browse files Browse the repository at this point in the history
  • Loading branch information
THIERRY SALLE committed Apr 18, 2017
1 parent f8ca6ae commit 4026021
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions services/slack/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

type Service struct {
configValue atomic.Value
clientValue atomic.Value
logger *log.Logger
client *http.Client
}
Expand All @@ -32,10 +33,13 @@ func NewService(c Config, l *log.Logger) (*Service, error) {
}
s := &Service{
logger: l,
client: &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}},
}
s.configValue.Store(c)

s.clientValue.Store(&http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
})
return s, nil
}

Expand Down Expand Up @@ -65,8 +69,12 @@ func (s *Service) Update(newConfig []interface{}) error {
if tlsConfig.InsecureSkipVerify {
s.logger.Println("W! Slack service is configured to skip ssl verification")
}
s.client = &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}}
s.configValue.Store(c)
s.clientValue.Store(&http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
})
}
return nil
}
Expand Down Expand Up @@ -120,7 +128,8 @@ func (s *Service) Alert(channel, message, username, iconEmoji string, level aler
return err
}
s.logger.Println("D! Posting event to Slack", post)
resp, err := s.client.Post(url, "application/json", post)
client := s.clientValue.Load().(*http.Client)
resp, err := client.Post(url, "application/json", post)
if err != nil {
return err
}
Expand Down

0 comments on commit 4026021

Please sign in to comment.