Skip to content

Commit 40c5811

Browse files
committed
parse the conn timeouts if not empty
Signed-off-by: Afzal Ansari <[email protected]>
1 parent 784a17f commit 40c5811

File tree

11 files changed

+75
-32
lines changed

11 files changed

+75
-32
lines changed

pkg/services/alertmanager.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,13 @@ func (s alertmanagerService) Send(notification Notification, dest Destination) e
211211
func (s alertmanagerService) sendOneTarget(ctx context.Context, target string, rawBody []byte) error {
212212
rawURL := fmt.Sprintf("%v://%v%v", s.opts.Scheme, target, s.opts.APIPath)
213213

214-
idleConnTimeout, err := time.ParseDuration(s.opts.IdleConnTimeout)
215-
if err != nil {
216-
return fmt.Errorf("failed to parse idle connection timeout")
214+
var idleConnTimeout time.Duration
215+
if s.opts.IdleConnTimeout != "" {
216+
var err error
217+
idleConnTimeout, err = time.ParseDuration(s.opts.IdleConnTimeout)
218+
if err != nil {
219+
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
220+
}
217221
}
218222
transport := httputil.NewTransport(rawURL, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, s.opts.InsecureSkipVerify)
219223
client := &http.Client{

pkg/services/github.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,13 @@ 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")
403+
var idleConnTimeout time.Duration
404+
if opts.IdleConnTimeout != "" {
405+
var err error
406+
idleConnTimeout, err = time.ParseDuration(opts.IdleConnTimeout)
407+
if err != nil {
408+
return nil, fmt.Errorf("failed to parse idle connection timeout: %w", err)
409+
}
406410
}
407411
tr := httputil.NewLoggingRoundTripper(
408412
httputil.NewTransport(url, opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxConnsPerHost, idleConnTimeout, false), log.WithField("service", "github"))

pkg/services/googlechat.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ 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-
idleConnTimeout, err := time.ParseDuration(s.opts.IdleConnTimeout)
116-
if err != nil {
117-
return nil, fmt.Errorf("failed to parse idle connection timeout")
115+
var idleConnTimeout time.Duration
116+
if s.opts.IdleConnTimeout != "" {
117+
var err error
118+
idleConnTimeout, err = time.ParseDuration(s.opts.IdleConnTimeout)
119+
if err != nil {
120+
return nil, fmt.Errorf("failed to parse idle connection timeout: %w", err)
121+
}
118122
}
119123
transport := httputil.NewTransport(webhookUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, false)
120124
client := &http.Client{

pkg/services/grafana.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +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-
idleConnTimeout, err := time.ParseDuration(s.opts.IdleConnTimeout)
56-
if err != nil {
57-
return fmt.Errorf("failed to parse idle connection timeout")
55+
var idleConnTimeout time.Duration
56+
if s.opts.IdleConnTimeout != "" {
57+
var err error
58+
idleConnTimeout, err = time.ParseDuration(s.opts.IdleConnTimeout)
59+
if err != nil {
60+
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
61+
}
5862
}
5963
client := &http.Client{
6064
Transport: httputil.NewLoggingRoundTripper(

pkg/services/mattermost.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ func NewMattermostService(opts MattermostOptions) NotificationService {
5656
}
5757

5858
func (m *mattermostService) Send(notification Notification, dest Destination) error {
59-
idleConnTimeout, err := time.ParseDuration(m.opts.IdleConnTimeout)
60-
if err != nil {
61-
return fmt.Errorf("failed to parse idle connection timeout")
59+
var idleConnTimeout time.Duration
60+
if m.opts.IdleConnTimeout != "" {
61+
var err error
62+
idleConnTimeout, err = time.ParseDuration(m.opts.IdleConnTimeout)
63+
if err != nil {
64+
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
65+
}
6266
}
6367
transport := httputil.NewTransport(m.opts.ApiURL, m.opts.MaxIdleConns, m.opts.MaxIdleConnsPerHost, m.opts.MaxConnsPerHost, idleConnTimeout, m.opts.InsecureSkipVerify)
6468
client := &http.Client{

pkg/services/newrelic.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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")
141+
var idleConnTimeout time.Duration
142+
if s.opts.IdleConnTimeout != "" {
143+
var err error
144+
idleConnTimeout, err = time.ParseDuration(s.opts.IdleConnTimeout)
145+
if err != nil {
146+
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
147+
}
144148
}
145149
client := &http.Client{
146150
Transport: httputil.NewLoggingRoundTripper(

pkg/services/opsgenie.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,13 @@ 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")
258+
var idleConnTimeout time.Duration
259+
if s.opts.IdleConnTimeout != "" {
260+
var err error
261+
idleConnTimeout, err = time.ParseDuration(s.opts.IdleConnTimeout)
262+
if err != nil {
263+
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
264+
}
261265
}
262266
alertClient, _ := alert.NewClient(&client.Config{
263267
ApiKey: apiKey,
@@ -318,7 +322,7 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) erro
318322
}
319323
}
320324

321-
_, err = alertClient.Create(context.TODO(), &alert.CreateAlertRequest{
325+
_, err := alertClient.Create(context.TODO(), &alert.CreateAlertRequest{
322326
Message: notification.Message,
323327
Description: description,
324328
Priority: priority,

pkg/services/slack.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ func newSlackClient(opts SlackOptions) *slack.Client {
201201
if opts.ApiURL != "" {
202202
apiURL = opts.ApiURL
203203
}
204-
idleConnTimeout, _ := time.ParseDuration(opts.IdleConnTimeout)
204+
var idleConnTimeout time.Duration
205+
if opts.IdleConnTimeout != "" {
206+
idleConnTimeout, _ = time.ParseDuration(opts.IdleConnTimeout)
207+
}
205208
transport := httputil.NewTransport(apiURL, opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxIdleConns, idleConnTimeout, opts.InsecureSkipVerify)
206209
client := &http.Client{
207210
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "slack")),

pkg/services/teams.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,13 @@ 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-
idleConnTimeout, err := time.ParseDuration(s.opts.IdleConnTimeout)
165-
if err != nil {
166-
return fmt.Errorf("failed to parse idle connection timeout")
164+
var idleConnTimeout time.Duration
165+
if s.opts.IdleConnTimeout != "" {
166+
var err error
167+
idleConnTimeout, err = time.ParseDuration(s.opts.IdleConnTimeout)
168+
if err != nil {
169+
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
170+
}
167171
}
168172
transport := httputil.NewTransport(webhookUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, false)
169173
client := &http.Client{

pkg/services/webex.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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")
52+
var idleConnTimeout time.Duration
53+
if w.opts.IdleConnTimeout != "" {
54+
var err error
55+
idleConnTimeout, err = time.ParseDuration(w.opts.IdleConnTimeout)
56+
if err != nil {
57+
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
58+
}
5559
}
5660
client := &http.Client{
5761
Transport: httputil.NewLoggingRoundTripper(

0 commit comments

Comments
 (0)