Skip to content

Commit d3dfddf

Browse files
committed
refactor the transport connection into the svc
Signed-off-by: Afzal Ansari <[email protected]>
1 parent afbc39f commit d3dfddf

File tree

16 files changed

+157
-143
lines changed

16 files changed

+157
-143
lines changed

pkg/services/alertmanager.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@ type AlertmanagerNotification struct {
3333

3434
// AlertmanagerOptions cluster configuration
3535
type AlertmanagerOptions struct {
36-
Targets []string `json:"targets"`
37-
Scheme string `json:"scheme"`
38-
APIPath string `json:"apiPath"`
39-
BasicAuth *BasicAuth `json:"basicAuth"`
40-
BearerToken string `json:"bearerToken"`
41-
Transport httputil.HTTPTransportSettings `json:"transport"`
42-
Timeout int `json:"timeout"`
36+
Targets []string `json:"targets"`
37+
Scheme string `json:"scheme"`
38+
APIPath string `json:"apiPath"`
39+
BasicAuth *BasicAuth `json:"basicAuth"`
40+
BearerToken string `json:"bearerToken"`
41+
Timeout int `json:"timeout"`
42+
InsecureSkipVerify bool `json:"insecureSkipVerify"`
43+
MaxIdleConns int `json:"maxIdleConns"`
44+
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
45+
MaxConnsPerHost int `json:"maxConnsPerHost"`
46+
IdleConnTimeout time.Duration `json:"idleConnTimeout"`
4347
}
4448

4549
// NewAlertmanagerService new service
@@ -207,7 +211,7 @@ func (s alertmanagerService) Send(notification Notification, dest Destination) e
207211
func (s alertmanagerService) sendOneTarget(ctx context.Context, target string, rawBody []byte) error {
208212
rawURL := fmt.Sprintf("%v://%v%v", s.opts.Scheme, target, s.opts.APIPath)
209213

210-
transport := httputil.NewTransport(rawURL, s.opts.Transport)
214+
transport := httputil.NewTransport(rawURL, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, s.opts.InsecureSkipVerify)
211215
client := &http.Client{
212216
Transport: httputil.NewLoggingRoundTripper(transport, s.entry),
213217
}

pkg/services/github.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +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-
Transport httputil.HTTPTransportSettings `json:"transport"`
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"`
3438
}
3539

3640
type GitHubNotification struct {
@@ -382,7 +386,6 @@ func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (
382386

383387
func NewGitHubService(opts GitHubOptions) (*gitHubService, error) {
384388
url := "https://api.github.com"
385-
opts.Transport.InsecureSkipVerify = false
386389
if opts.EnterpriseBaseURL != "" {
387390
url = opts.EnterpriseBaseURL
388391
}
@@ -398,7 +401,7 @@ func NewGitHubService(opts GitHubOptions) (*gitHubService, error) {
398401
}
399402

400403
tr := httputil.NewLoggingRoundTripper(
401-
httputil.NewTransport(url, opts.Transport), log.WithField("service", "github"))
404+
httputil.NewTransport(url, opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxConnsPerHost, opts.IdleConnTimeout, false), log.WithField("service", "github"))
402405
itr, err := ghinstallation.New(tr, appID, installationID, []byte(opts.PrivateKey))
403406
if err != nil {
404407
return nil, err

pkg/services/googlechat.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"net/url"
1010
texttemplate "text/template"
11+
"time"
1112

1213
"github.com/google/uuid"
1314

@@ -80,8 +81,12 @@ func (n *GoogleChatNotification) GetTemplater(name string, f texttemplate.FuncMa
8081
}
8182

8283
type GoogleChatOptions struct {
83-
WebhookUrls map[string]string `json:"webhooks"`
84-
Transport httputil.HTTPTransportSettings `json:"transport"`
84+
WebhookUrls map[string]string `json:"webhooks"`
85+
InsecureSkipVerify bool `json:"insecureSkipVerify"`
86+
MaxIdleConns int `json:"maxIdleConns"`
87+
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
88+
MaxConnsPerHost int `json:"maxConnsPerHost"`
89+
IdleConnTimeout time.Duration `json:"idleConnTimeout"`
8590
}
8691

8792
type googleChatService struct {
@@ -104,11 +109,10 @@ type webhookError struct {
104109

105110
func (s googleChatService) getClient(recipient string) (*googlechatClient, error) {
106111
webhookUrl, ok := s.opts.WebhookUrls[recipient]
107-
s.opts.Transport.InsecureSkipVerify = false
108112
if !ok {
109113
return nil, fmt.Errorf("no Google chat webhook configured for recipient %s", recipient)
110114
}
111-
transport := httputil.NewTransport(webhookUrl, s.opts.Transport)
115+
transport := httputil.NewTransport(webhookUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, false)
112116
client := &http.Client{
113117
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "googlechat")),
114118
}

pkg/services/grafana.go

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

1919
type GrafanaOptions struct {
20-
ApiUrl string `json:"apiUrl"`
21-
ApiKey string `json:"apiKey"`
22-
Transport httputil.HTTPTransportSettings `json:"transport"`
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"`
2327
}
2428

2529
type grafanaService struct {
@@ -51,7 +55,7 @@ func (s *grafanaService) Send(notification Notification, dest Destination) error
5155

5256
client := &http.Client{
5357
Transport: httputil.NewLoggingRoundTripper(
54-
httputil.NewTransport(s.opts.ApiUrl, s.opts.Transport), log.WithField("service", "grafana")),
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")),
5559
}
5660

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

pkg/services/grafana_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import (
55
"net/http"
66
"net/http/httptest"
77
"testing"
8-
9-
httputil "github.com/argoproj/notifications-engine/pkg/util/http"
8+
"time"
109

1110
"github.com/stretchr/testify/assert"
1211
)
@@ -23,11 +22,13 @@ func TestGrafana_SuccessfullySendsNotification(t *testing.T) {
2322
defer server.Close()
2423

2524
service := NewGrafanaService(GrafanaOptions{
26-
ApiUrl: server.URL,
27-
ApiKey: "something-secret-but-not-relevant-in-this-test",
28-
Transport: httputil.HTTPTransportSettings{
29-
InsecureSkipVerify: true,
30-
},
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,
3132
})
3233
err := service.Send(
3334
Notification{
@@ -52,9 +53,8 @@ func TestGrafana_UnSuccessfullySendsNotification(t *testing.T) {
5253
service := NewGrafanaService(GrafanaOptions{
5354
ApiUrl: server.URL,
5455
ApiKey: "something-secret-but-not-relevant-in-this-test",
55-
Transport: httputil.HTTPTransportSettings{
56-
InsecureSkipVerify: true,
57-
},
56+
57+
InsecureSkipVerify: true,
5858
})
5959
err := service.Send(
6060
Notification{}, Destination{Recipient: "tag1|tag2", Service: "test-service"})

pkg/services/mattermost.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"net/http"
99
texttemplate "text/template"
10+
"time"
1011

1112
log "github.com/sirupsen/logrus"
1213

@@ -37,9 +38,13 @@ func (n *MattermostNotification) GetTemplater(name string, f texttemplate.FuncMa
3738
}
3839

3940
type MattermostOptions struct {
40-
ApiURL string `json:"apiURL"`
41-
Token string `json:"token"`
42-
Transport httputil.HTTPTransportSettings `json:"transport"`
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"`
4348
}
4449

4550
type mattermostService struct {
@@ -51,7 +56,7 @@ func NewMattermostService(opts MattermostOptions) NotificationService {
5156
}
5257

5358
func (m *mattermostService) Send(notification Notification, dest Destination) error {
54-
transport := httputil.NewTransport(m.opts.ApiURL, m.opts.Transport)
59+
transport := httputil.NewTransport(m.opts.ApiURL, m.opts.MaxIdleConns, m.opts.MaxIdleConnsPerHost, m.opts.MaxConnsPerHost, m.opts.IdleConnTimeout, m.opts.InsecureSkipVerify)
5560
client := &http.Client{
5661
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "mattermost")),
5762
}

pkg/services/mattermost_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"testing"
88
"text/template"
99

10-
httputil "github.com/argoproj/notifications-engine/pkg/util/http"
11-
1210
"github.com/stretchr/testify/assert"
1311
)
1412

@@ -43,11 +41,9 @@ func TestSend_Mattermost(t *testing.T) {
4341
defer ts.Close()
4442

4543
service := NewMattermostService(MattermostOptions{
46-
ApiURL: ts.URL,
47-
Token: "token",
48-
Transport: httputil.HTTPTransportSettings{
49-
InsecureSkipVerify: true,
50-
},
44+
ApiURL: ts.URL,
45+
Token: "token",
46+
InsecureSkipVerify: true,
5147
})
5248
err := service.Send(Notification{
5349
Message: "message",

pkg/services/newrelic.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ import (
88
"net/http"
99
"strings"
1010
texttemplate "text/template"
11+
"time"
1112

1213
log "github.com/sirupsen/logrus"
1314

1415
httputil "github.com/argoproj/notifications-engine/pkg/util/http"
1516
)
1617

1718
type NewrelicOptions struct {
18-
ApiKey string `json:"apiKey"`
19-
ApiURL string `json:"apiURL"`
20-
Transport httputil.HTTPTransportSettings `json:"transport"`
19+
ApiKey string `json:"apiKey"`
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"`
2126
}
2227

2328
type NewrelicNotification struct {
@@ -133,10 +138,9 @@ func (s newrelicService) Send(notification Notification, dest Destination) error
133138
},
134139
}
135140

136-
s.opts.Transport.InsecureSkipVerify = false
137141
client := &http.Client{
138142
Transport: httputil.NewLoggingRoundTripper(
139-
httputil.NewTransport(s.opts.ApiURL, s.opts.Transport), log.WithField("service", dest.Service)),
143+
httputil.NewTransport(s.opts.ApiURL, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, false), log.WithField("service", dest.Service)),
140144
}
141145

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

pkg/services/opsgenie.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
texttemplate "text/template"
9+
"time"
910

1011
"github.com/opsgenie/opsgenie-go-sdk-v2/alert"
1112
"github.com/opsgenie/opsgenie-go-sdk-v2/client"
@@ -15,9 +16,13 @@ import (
1516
)
1617

1718
type OpsgenieOptions struct {
18-
ApiUrl string `json:"apiUrl"`
19-
ApiKeys map[string]string `json:"apiKeys"`
20-
Transport httputil.HTTPTransportSettings `json:"transport"`
19+
ApiUrl string `json:"apiUrl"`
20+
ApiKeys map[string]string `json:"apiKeys"`
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"`
2126
}
2227

2328
type OpsgenieNotification struct {
@@ -250,13 +255,12 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) erro
250255
if !ok {
251256
return fmt.Errorf("no API key configured for recipient %s", dest.Recipient)
252257
}
253-
s.opts.Transport.InsecureSkipVerify = false
254258
alertClient, _ := alert.NewClient(&client.Config{
255259
ApiKey: apiKey,
256260
OpsGenieAPIURL: client.ApiUrl(s.opts.ApiUrl),
257261
HttpClient: &http.Client{
258262
Transport: httputil.NewLoggingRoundTripper(
259-
httputil.NewTransport(s.opts.ApiUrl, s.opts.Transport), log.WithField("service", "opsgenie")),
263+
httputil.NewTransport(s.opts.ApiUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, false), log.WithField("service", "opsgenie")),
260264
},
261265
})
262266

pkg/services/slack.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010
"regexp"
1111
texttemplate "text/template"
12+
"time"
1213

1314
httputil "github.com/argoproj/notifications-engine/pkg/util/http"
1415
slackutil "github.com/argoproj/notifications-engine/pkg/util/slack"
@@ -96,14 +97,18 @@ func (n *SlackNotification) GetTemplater(name string, f texttemplate.FuncMap) (T
9697
}
9798

9899
type SlackOptions struct {
99-
Username string `json:"username"`
100-
Icon string `json:"icon"`
101-
Token string `json:"token"`
102-
SigningSecret string `json:"signingSecret"`
103-
Channels []string `json:"channels"`
104-
ApiURL string `json:"apiURL"`
105-
DisableUnfurl bool `json:"disableUnfurl"`
106-
Transport httputil.HTTPTransportSettings `json:"transport"`
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"`
107112
}
108113

109114
type slackService struct {
@@ -196,7 +201,7 @@ func newSlackClient(opts SlackOptions) *slack.Client {
196201
if opts.ApiURL != "" {
197202
apiURL = opts.ApiURL
198203
}
199-
transport := httputil.NewTransport(apiURL, opts.Transport)
204+
transport := httputil.NewTransport(apiURL, opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxIdleConns, opts.IdleConnTimeout, opts.InsecureSkipVerify)
200205
client := &http.Client{
201206
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "slack")),
202207
}

0 commit comments

Comments
 (0)