Skip to content

Commit b1f590d

Browse files
committed
modify to use IdleConnTimeout as a string
Signed-off-by: Afzal Ansari <[email protected]>
1 parent a9346ed commit b1f590d

File tree

11 files changed

+98
-67
lines changed

11 files changed

+98
-67
lines changed

pkg/services/github.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ var (
2626
)
2727

2828
type GitHubOptions struct {
29-
AppID interface{} `json:"appID"`
30-
InstallationID interface{} `json:"installationID"`
31-
PrivateKey string `json:"privateKey"`
32-
EnterpriseBaseURL string `json:"enterpriseBaseURL"`
33-
InsecureSkipVerify bool `json:"insecureSkipVerify"`
34-
MaxIdleConns int `json:"maxIdleConns"`
35-
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
36-
MaxConnsPerHost int `json:"maxConnsPerHost"`
37-
IdleConnTimeout time.Duration `json:"idleConnTimeout"`
29+
AppID interface{} `json:"appID"`
30+
InstallationID interface{} `json:"installationID"`
31+
PrivateKey string `json:"privateKey"`
32+
EnterpriseBaseURL string `json:"enterpriseBaseURL"`
33+
InsecureSkipVerify bool `json:"insecureSkipVerify"`
34+
MaxIdleConns int `json:"maxIdleConns"`
35+
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
36+
MaxConnsPerHost int `json:"maxConnsPerHost"`
37+
IdleConnTimeout string `json:"idleConnTimeout"`
3838
}
3939

4040
type GitHubNotification struct {
@@ -400,8 +400,12 @@ func NewGitHubService(opts GitHubOptions) (*gitHubService, error) {
400400
return nil, err
401401
}
402402

403+
idleConnTimeout, err := time.ParseDuration(opts.IdleConnTimeout)
404+
if err != nil {
405+
return nil, fmt.Errorf("failed to parse idle connection timeout")
406+
}
403407
tr := httputil.NewLoggingRoundTripper(
404-
httputil.NewTransport(url, opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxConnsPerHost, opts.IdleConnTimeout, false), log.WithField("service", "github"))
408+
httputil.NewTransport(url, opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxConnsPerHost, idleConnTimeout, false), log.WithField("service", "github"))
405409
itr, err := ghinstallation.New(tr, appID, installationID, []byte(opts.PrivateKey))
406410
if err != nil {
407411
return nil, err

pkg/services/googlechat.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type GoogleChatOptions struct {
8686
MaxIdleConns int `json:"maxIdleConns"`
8787
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
8888
MaxConnsPerHost int `json:"maxConnsPerHost"`
89-
IdleConnTimeout time.Duration `json:"idleConnTimeout"`
89+
IdleConnTimeout string `json:"idleConnTimeout"`
9090
}
9191

9292
type googleChatService struct {
@@ -112,7 +112,11 @@ func (s googleChatService) getClient(recipient string) (*googlechatClient, error
112112
if !ok {
113113
return nil, fmt.Errorf("no Google chat webhook configured for recipient %s", recipient)
114114
}
115-
transport := httputil.NewTransport(webhookUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, false)
115+
idleConnTimeout, err := time.ParseDuration(s.opts.IdleConnTimeout)
116+
if err != nil {
117+
return nil, fmt.Errorf("failed to parse idle connection timeout")
118+
}
119+
transport := httputil.NewTransport(webhookUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, false)
116120
client := &http.Client{
117121
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "googlechat")),
118122
}

pkg/services/grafana.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import (
1717
)
1818

1919
type GrafanaOptions struct {
20-
ApiUrl string `json:"apiUrl"`
21-
ApiKey string `json:"apiKey"`
22-
InsecureSkipVerify bool `json:"insecureSkipVerify"`
23-
MaxIdleConns int `json:"maxIdleConns"`
24-
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
25-
MaxConnsPerHost int `json:"maxConnsPerHost"`
26-
IdleConnTimeout time.Duration `json:"idleConnTimeout"`
20+
ApiUrl string `json:"apiUrl"`
21+
ApiKey string `json:"apiKey"`
22+
InsecureSkipVerify bool `json:"insecureSkipVerify"`
23+
MaxIdleConns int `json:"maxIdleConns"`
24+
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
25+
MaxConnsPerHost int `json:"maxConnsPerHost"`
26+
IdleConnTimeout string `json:"idleConnTimeout"`
2727
}
2828

2929
type grafanaService struct {
@@ -52,10 +52,13 @@ func (s *grafanaService) Send(notification Notification, dest Destination) error
5252
if notification.Message == "" {
5353
log.Warnf("Message is an empty string or not provided in the notifications template")
5454
}
55-
55+
idleConnTimeout, err := time.ParseDuration(s.opts.IdleConnTimeout)
56+
if err != nil {
57+
return fmt.Errorf("failed to parse idle connection timeout")
58+
}
5659
client := &http.Client{
5760
Transport: httputil.NewLoggingRoundTripper(
58-
httputil.NewTransport(s.opts.ApiUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, s.opts.InsecureSkipVerify), log.WithField("service", "grafana")),
61+
httputil.NewTransport(s.opts.ApiUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, s.opts.InsecureSkipVerify), log.WithField("service", "grafana")),
5962
}
6063

6164
jsonValue, _ := json.Marshal(ga)

pkg/services/grafana_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66
"net/http/httptest"
77
"testing"
8-
"time"
98

109
"github.com/stretchr/testify/assert"
1110
)
@@ -22,13 +21,9 @@ func TestGrafana_SuccessfullySendsNotification(t *testing.T) {
2221
defer server.Close()
2322

2423
service := NewGrafanaService(GrafanaOptions{
25-
ApiUrl: server.URL,
26-
ApiKey: "something-secret-but-not-relevant-in-this-test",
27-
MaxIdleConns: 100,
28-
MaxIdleConnsPerHost: 10,
29-
MaxConnsPerHost: 20,
30-
IdleConnTimeout: 90 * time.Second,
31-
InsecureSkipVerify: true,
24+
ApiUrl: server.URL,
25+
ApiKey: "something-secret-but-not-relevant-in-this-test",
26+
InsecureSkipVerify: true,
3227
})
3328
err := service.Send(
3429
Notification{

pkg/services/mattermost.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ func (n *MattermostNotification) GetTemplater(name string, f texttemplate.FuncMa
3838
}
3939

4040
type MattermostOptions struct {
41-
ApiURL string `json:"apiURL"`
42-
Token string `json:"token"`
43-
InsecureSkipVerify bool `json:"insecureSkipVerify"`
44-
MaxIdleConns int `json:"maxIdleConns"`
45-
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
46-
MaxConnsPerHost int `json:"maxConnsPerHost"`
47-
IdleConnTimeout time.Duration `json:"idleConnTimeout"`
41+
ApiURL string `json:"apiURL"`
42+
Token string `json:"token"`
43+
InsecureSkipVerify bool `json:"insecureSkipVerify"`
44+
MaxIdleConns int `json:"maxIdleConns"`
45+
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
46+
MaxConnsPerHost int `json:"maxConnsPerHost"`
47+
IdleConnTimeout string `json:"idleConnTimeout"`
4848
}
4949

5050
type mattermostService struct {
@@ -56,7 +56,11 @@ func NewMattermostService(opts MattermostOptions) NotificationService {
5656
}
5757

5858
func (m *mattermostService) Send(notification Notification, dest Destination) error {
59-
transport := httputil.NewTransport(m.opts.ApiURL, m.opts.MaxIdleConns, m.opts.MaxIdleConnsPerHost, m.opts.MaxConnsPerHost, m.opts.IdleConnTimeout, m.opts.InsecureSkipVerify)
59+
idleConnTimeout, err := time.ParseDuration(m.opts.IdleConnTimeout)
60+
if err != nil {
61+
return fmt.Errorf("failed to parse idle connection timeout")
62+
}
63+
transport := httputil.NewTransport(m.opts.ApiURL, m.opts.MaxIdleConns, m.opts.MaxIdleConnsPerHost, m.opts.MaxConnsPerHost, idleConnTimeout, m.opts.InsecureSkipVerify)
6064
client := &http.Client{
6165
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "mattermost")),
6266
}

pkg/services/newrelic.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type NewrelicOptions struct {
2222
MaxIdleConns int `json:"maxIdleConns"`
2323
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
2424
MaxConnsPerHost int `json:"maxConnsPerHost"`
25-
IdleConnTimeout time.Duration `json:"idleConnTimeout"`
25+
IdleConnTimeout string `json:"idleConnTimeout"`
2626
}
2727

2828
type NewrelicNotification struct {
@@ -138,9 +138,13 @@ func (s newrelicService) Send(notification Notification, dest Destination) error
138138
},
139139
}
140140

141+
idleConnTimeout, err := time.ParseDuration(s.opts.IdleConnTimeout)
142+
if err != nil {
143+
return fmt.Errorf("failed to parse idle connection timeout")
144+
}
141145
client := &http.Client{
142146
Transport: httputil.NewLoggingRoundTripper(
143-
httputil.NewTransport(s.opts.ApiURL, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, false), log.WithField("service", dest.Service)),
147+
httputil.NewTransport(s.opts.ApiURL, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, false), log.WithField("service", dest.Service)),
144148
}
145149

146150
jsonValue, err := json.Marshal(deploymentMarker)

pkg/services/opsgenie.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type OpsgenieOptions struct {
2222
MaxIdleConns int `json:"maxIdleConns"`
2323
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
2424
MaxConnsPerHost int `json:"maxConnsPerHost"`
25-
IdleConnTimeout time.Duration `json:"idleConnTimeout"`
25+
IdleConnTimeout string `json:"idleConnTimeout"`
2626
}
2727

2828
type OpsgenieNotification struct {
@@ -255,12 +255,16 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) erro
255255
if !ok {
256256
return fmt.Errorf("no API key configured for recipient %s", dest.Recipient)
257257
}
258+
idleConnTimeout, err := time.ParseDuration(s.opts.IdleConnTimeout)
259+
if err != nil {
260+
return fmt.Errorf("failed to parse idle connection timeout")
261+
}
258262
alertClient, _ := alert.NewClient(&client.Config{
259263
ApiKey: apiKey,
260264
OpsGenieAPIURL: client.ApiUrl(s.opts.ApiUrl),
261265
HttpClient: &http.Client{
262266
Transport: httputil.NewLoggingRoundTripper(
263-
httputil.NewTransport(s.opts.ApiUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, false), log.WithField("service", "opsgenie")),
267+
httputil.NewTransport(s.opts.ApiUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, false), log.WithField("service", "opsgenie")),
264268
},
265269
})
266270

@@ -314,7 +318,7 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) erro
314318
}
315319
}
316320

317-
_, err := alertClient.Create(context.TODO(), &alert.CreateAlertRequest{
321+
_, err = alertClient.Create(context.TODO(), &alert.CreateAlertRequest{
318322
Message: notification.Message,
319323
Description: description,
320324
Priority: priority,

pkg/services/slack.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,18 @@ func (n *SlackNotification) GetTemplater(name string, f texttemplate.FuncMap) (T
9797
}
9898

9999
type SlackOptions struct {
100-
Username string `json:"username"`
101-
Icon string `json:"icon"`
102-
Token string `json:"token"`
103-
SigningSecret string `json:"signingSecret"`
104-
Channels []string `json:"channels"`
105-
ApiURL string `json:"apiURL"`
106-
DisableUnfurl bool `json:"disableUnfurl"`
107-
InsecureSkipVerify bool `json:"insecureSkipVerify"`
108-
MaxIdleConns int `json:"maxIdleConns"`
109-
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
110-
MaxConnsPerHost int `json:"maxConnsPerHost"`
111-
IdleConnTimeout time.Duration `json:"idleConnTimeout"`
100+
Username string `json:"username"`
101+
Icon string `json:"icon"`
102+
Token string `json:"token"`
103+
SigningSecret string `json:"signingSecret"`
104+
Channels []string `json:"channels"`
105+
ApiURL string `json:"apiURL"`
106+
DisableUnfurl bool `json:"disableUnfurl"`
107+
InsecureSkipVerify bool `json:"insecureSkipVerify"`
108+
MaxIdleConns int `json:"maxIdleConns"`
109+
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
110+
MaxConnsPerHost int `json:"maxConnsPerHost"`
111+
IdleConnTimeout string `json:"idleConnTimeout"`
112112
}
113113

114114
type slackService struct {
@@ -201,7 +201,8 @@ func newSlackClient(opts SlackOptions) *slack.Client {
201201
if opts.ApiURL != "" {
202202
apiURL = opts.ApiURL
203203
}
204-
transport := httputil.NewTransport(apiURL, opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxIdleConns, opts.IdleConnTimeout, opts.InsecureSkipVerify)
204+
idleConnTimeout, _ := time.ParseDuration(opts.IdleConnTimeout)
205+
transport := httputil.NewTransport(apiURL, opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxIdleConns, idleConnTimeout, opts.InsecureSkipVerify)
205206
client := &http.Client{
206207
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "slack")),
207208
}

pkg/services/teams.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ type TeamsOptions struct {
145145
MaxIdleConns int `json:"maxIdleConns"`
146146
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
147147
MaxConnsPerHost int `json:"maxConnsPerHost"`
148-
IdleConnTimeout time.Duration `json:"idleConnTimeout"`
148+
IdleConnTimeout string `json:"idleConnTimeout"`
149149
}
150150

151151
type teamsService struct {
@@ -161,7 +161,11 @@ func (s teamsService) Send(notification Notification, dest Destination) error {
161161
if !ok {
162162
return fmt.Errorf("no teams webhook configured for recipient %s", dest.Recipient)
163163
}
164-
transport := httputil.NewTransport(webhookUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, false)
164+
idleConnTimeout, err := time.ParseDuration(s.opts.IdleConnTimeout)
165+
if err != nil {
166+
return fmt.Errorf("failed to parse idle connection timeout")
167+
}
168+
transport := httputil.NewTransport(webhookUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, false)
165169
client := &http.Client{
166170
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "teams")),
167171
}

pkg/services/webex.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import (
1616
)
1717

1818
type WebexOptions struct {
19-
Token string `json:"token"`
20-
ApiURL string `json:"apiURL"`
21-
InsecureSkipVerify bool `json:"insecureSkipVerify"`
22-
MaxIdleConns int `json:"maxIdleConns"`
23-
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
24-
MaxConnsPerHost int `json:"maxConnsPerHost"`
25-
IdleConnTimeout time.Duration `json:"idleConnTimeout"`
19+
Token string `json:"token"`
20+
ApiURL string `json:"apiURL"`
21+
InsecureSkipVerify bool `json:"insecureSkipVerify"`
22+
MaxIdleConns int `json:"maxIdleConns"`
23+
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
24+
MaxConnsPerHost int `json:"maxConnsPerHost"`
25+
IdleConnTimeout string `json:"idleConnTimeout"`
2626
}
2727

2828
type webexService struct {
@@ -49,9 +49,13 @@ var validEmail = regexp.MustCompile(`^\S+@\S+\.\S+$`)
4949
func (w webexService) Send(notification Notification, dest Destination) error {
5050
requestURL := fmt.Sprintf("%s/v1/messages", w.opts.ApiURL)
5151

52+
idleConnTimeout, err := time.ParseDuration(w.opts.IdleConnTimeout)
53+
if err != nil {
54+
return fmt.Errorf("failed to parse idle connection timeout")
55+
}
5256
client := &http.Client{
5357
Transport: httputil.NewLoggingRoundTripper(
54-
httputil.NewTransport(requestURL, w.opts.MaxIdleConns, w.opts.MaxIdleConnsPerHost, w.opts.MaxConnsPerHost, w.opts.IdleConnTimeout, false), log.WithField("service", dest.Service)),
58+
httputil.NewTransport(requestURL, w.opts.MaxIdleConns, w.opts.MaxIdleConnsPerHost, w.opts.MaxConnsPerHost, idleConnTimeout, false), log.WithField("service", dest.Service)),
5559
}
5660

5761
message := webexMessage{

0 commit comments

Comments
 (0)