Skip to content

Commit b35fb30

Browse files
authored
CLOUDP-279538 Unify http transport (#3403)
1 parent ce759e9 commit b35fb30

File tree

3 files changed

+94
-78
lines changed

3 files changed

+94
-78
lines changed

internal/oauth/oauth.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,17 @@
1515
package oauth
1616

1717
import (
18-
"net"
1918
"net/http"
20-
"time"
2119

2220
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
21+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/transport"
2322
"go.mongodb.org/atlas/auth"
2423
)
2524

2625
const (
27-
timeout = 5 * time.Second
28-
keepAlive = 30 * time.Second
29-
maxIdleConns = 5
30-
maxIdleConnsPerHost = 4
31-
idleConnTimeout = 30 * time.Second
32-
expectContinueTimeout = 1 * time.Second
33-
cloudGovServiceURL = "https://cloud.mongodbgov.com/"
26+
cloudGovServiceURL = "https://cloud.mongodbgov.com/"
3427
)
3528

36-
var defaultTransport = &http.Transport{
37-
DialContext: (&net.Dialer{
38-
Timeout: timeout,
39-
KeepAlive: keepAlive,
40-
}).DialContext,
41-
MaxIdleConns: maxIdleConns,
42-
MaxIdleConnsPerHost: maxIdleConnsPerHost,
43-
Proxy: http.ProxyFromEnvironment,
44-
IdleConnTimeout: idleConnTimeout,
45-
ExpectContinueTimeout: expectContinueTimeout,
46-
}
47-
4829
type ServiceGetter interface {
4930
Service() string
5031
OpsManagerURL() string
@@ -58,7 +39,7 @@ const (
5839

5940
func FlowWithConfig(c ServiceGetter) (*auth.Config, error) {
6041
client := http.DefaultClient
61-
client.Transport = defaultTransport
42+
client.Transport = transport.Default()
6243
id := ClientID
6344
if c.Service() == config.CloudGovService {
6445
id = GovClientID

internal/store/store.go

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,20 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23-
"net"
2423
"net/http"
2524
"strings"
26-
"time"
2725

28-
"github.com/mongodb-forks/digest"
2926
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
3027
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/log"
28+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/transport"
3129
atlasClustersPinned "go.mongodb.org/atlas-sdk/v20240530005/admin"
3230
atlasv2 "go.mongodb.org/atlas-sdk/v20241113001/admin"
3331
atlasauth "go.mongodb.org/atlas/auth"
3432
atlas "go.mongodb.org/atlas/mongodbatlas"
3533
)
3634

3735
const (
38-
telemetryTimeout = 1 * time.Second
39-
timeout = 5 * time.Second
40-
keepAlive = 30 * time.Second
41-
maxIdleConns = 5
42-
maxIdleConnsPerHost = 4
43-
idleConnTimeout = 30 * time.Second
44-
expectContinueTimeout = 1 * time.Second
45-
cloudGovServiceURL = "https://cloud.mongodbgov.com/"
36+
cloudGovServiceURL = "https://cloud.mongodbgov.com/"
4637
)
4738

4839
var errUnsupportedService = errors.New("unsupported service")
@@ -62,67 +53,26 @@ type Store struct {
6253
ctx context.Context
6354
}
6455

65-
var defaultTransport = &http.Transport{
66-
DialContext: (&net.Dialer{
67-
Timeout: timeout,
68-
KeepAlive: keepAlive,
69-
}).DialContext,
70-
MaxIdleConns: maxIdleConns,
71-
MaxIdleConnsPerHost: maxIdleConnsPerHost,
72-
Proxy: http.ProxyFromEnvironment,
73-
IdleConnTimeout: idleConnTimeout,
74-
ExpectContinueTimeout: expectContinueTimeout,
75-
}
76-
77-
var telemetryTransport = &http.Transport{
78-
DialContext: (&net.Dialer{
79-
Timeout: telemetryTimeout,
80-
KeepAlive: keepAlive,
81-
}).DialContext,
82-
MaxIdleConns: maxIdleConns,
83-
MaxIdleConnsPerHost: maxIdleConnsPerHost,
84-
Proxy: http.ProxyFromEnvironment,
85-
IdleConnTimeout: idleConnTimeout,
86-
ExpectContinueTimeout: expectContinueTimeout,
87-
}
88-
8956
func (s *Store) httpClient(httpTransport http.RoundTripper) (*http.Client, error) {
9057
if s.username != "" && s.password != "" {
91-
t := &digest.Transport{
92-
Username: s.username,
93-
Password: s.password,
94-
}
95-
t.Transport = httpTransport
58+
t := transport.NewDigestTransport(s.username, s.password, httpTransport)
9659
return t.Client()
9760
}
9861
if s.accessToken != nil {
99-
tr := &Transport{
100-
token: s.accessToken,
101-
base: httpTransport,
102-
}
62+
tr := transport.NewAccessTokenTransport(s.accessToken, httpTransport)
10363

10464
return &http.Client{Transport: tr}, nil
10565
}
10666

10767
return &http.Client{Transport: httpTransport}, nil
10868
}
10969

110-
type Transport struct {
111-
token *atlasauth.Token
112-
base http.RoundTripper
113-
}
114-
115-
func (tr *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
116-
tr.token.SetAuthHeader(req)
117-
return tr.base.RoundTrip(req)
118-
}
119-
12070
func (s *Store) transport() *http.Transport {
12171
switch {
12272
case s.telemetry:
123-
return telemetryTransport
73+
return transport.Telemetry()
12474
default:
125-
return defaultTransport
75+
return transport.Default()
12676
}
12777
}
12878

internal/transport/transport.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2024 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package transport
16+
17+
import (
18+
"net"
19+
"net/http"
20+
"time"
21+
22+
"github.com/mongodb-forks/digest"
23+
atlasauth "go.mongodb.org/atlas/auth"
24+
)
25+
26+
const (
27+
telemetryTimeout = 1 * time.Second
28+
timeout = 5 * time.Second
29+
keepAlive = 30 * time.Second
30+
maxIdleConns = 5
31+
maxIdleConnsPerHost = 4
32+
idleConnTimeout = 30 * time.Second
33+
expectContinueTimeout = 1 * time.Second
34+
)
35+
36+
var defaultTransport = newTransport(timeout)
37+
38+
func Default() *http.Transport {
39+
return defaultTransport
40+
}
41+
42+
var telemetryTransport = newTransport(telemetryTimeout)
43+
44+
func Telemetry() *http.Transport {
45+
return telemetryTransport
46+
}
47+
48+
func newTransport(timeout time.Duration) *http.Transport {
49+
return &http.Transport{
50+
DialContext: (&net.Dialer{
51+
Timeout: timeout,
52+
KeepAlive: keepAlive,
53+
}).DialContext,
54+
MaxIdleConns: maxIdleConns,
55+
MaxIdleConnsPerHost: maxIdleConnsPerHost,
56+
Proxy: http.ProxyFromEnvironment,
57+
IdleConnTimeout: idleConnTimeout,
58+
ExpectContinueTimeout: expectContinueTimeout,
59+
}
60+
}
61+
62+
func NewDigestTransport(username, password string, base http.RoundTripper) *digest.Transport {
63+
return &digest.Transport{
64+
Username: username,
65+
Password: password,
66+
Transport: base,
67+
}
68+
}
69+
70+
func NewAccessTokenTransport(token *atlasauth.Token, base http.RoundTripper) http.RoundTripper {
71+
return &tokenTransport{
72+
token: token,
73+
base: base,
74+
}
75+
}
76+
77+
type tokenTransport struct {
78+
token *atlasauth.Token
79+
base http.RoundTripper
80+
}
81+
82+
func (tr *tokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
83+
tr.token.SetAuthHeader(req)
84+
return tr.base.RoundTrip(req)
85+
}

0 commit comments

Comments
 (0)