-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
130 lines (117 loc) · 4.32 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright 2022 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package http
import (
"context"
"github.com/armory-io/go-commons/iam/token"
"go.uber.org/zap"
"io"
"net"
"net/http"
"strings"
"time"
)
func NewClient(ctx context.Context, log *zap.SugaredLogger, svc ClientSettings, identity token.Identity) (*Client, error) {
rt, err := makeClient(ctx, log, svc, identity)
if err != nil {
return nil, err
}
return &Client{
baseUrl: strings.TrimSuffix(svc.BaseUrl, "/"),
c: &http.Client{
Transport: rt,
},
}, nil
}
type Client struct {
baseUrl string
c *http.Client
}
func makeClient(ctx context.Context, log *zap.SugaredLogger, s ClientSettings, identity token.Identity) (http.RoundTripper, error) {
t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: time.Duration(s.TimeoutSeconds) * time.Second,
KeepAlive: time.Duration(s.KeepAliveSeconds) * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: s.MaxIdleConns,
IdleConnTimeout: time.Duration(s.IdleConnTimeoutSeconds) * time.Second,
TLSHandshakeTimeout: time.Duration(s.TLSHandshakeTimeoutSeconds) * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
cfg, err := GetTLSConfig(s.TLS)
if err != nil {
return nil, err
}
t.TLSClientConfig = cfg
return token.GetTokenWrapper(t, identity, log), nil
}
func (s *Client) NewRequest(method, relativeUrl string, body io.Reader) (*http.Request, error) {
return http.NewRequest(method, s.baseUrl+relativeUrl, body)
}
func (s *Client) GetClient() *http.Client {
return s.c
}
func NewDefaultClientSettings() ClientSettings {
return ClientSettings{
MaxIdleConns: 10,
IdleConnTimeoutSeconds: 90,
TLSHandshakeTimeoutSeconds: 10,
KeepAliveSeconds: 30,
TimeoutSeconds: 30,
}
}
type ClientSettings struct {
// Client base URL (e.g. https://my-service/some/path/)
BaseUrl string `yaml:"baseUrl,omitempty" json:"baseUrl,omitempty"`
// MaxIdleConns controls the maximum number of idle (keep-alive)
// connections for the given service. Zero means no limit.
// From http.Transport
// Defaults to 10
MaxIdleConns int `yaml:"maxIdleConns,omitempty" json:"maxIdleConns,omitempty"`
// IdleConnTimeoutSeconds is the maximum amount of time an idle
// (keep-alive) connection will remain idle before closing
// itself.
// Zero means no limit.
// From http.Transport
// Defaults to 90s
IdleConnTimeoutSeconds int32 `yaml:"idleConnTimeoutSeconds,omitempty" json:"idleConnTimeoutSeconds,omitempty"`
// MaxConnections optionally limits the number of connections to the service.
// Zero means no limit.
// Defaults to zero
MaxConnections int32 `yaml:"maxConnections,omitempty" json:"maxConnections,omitempty"`
// TLSHandshakeTimeoutSeconds specifies the maximum amount of time waiting to
// wait for a TLS handshake. Zero means no timeout.
// From http.Transport
// Defaults to 10s
TLSHandshakeTimeoutSeconds int32 `yaml:"tlsHandshakeTimeoutSeconds,omitempty" json:"tlsHandshakeTimeoutSeconds,omitempty"`
// TimeoutSeconds limits how long we wait for a response
// Zero means no limit
// Defaults to 30s
TimeoutSeconds int32 `yaml:"timeoutSeconds,omitempty" json:"timeoutSeconds,omitempty"`
// KeepAliveSeconds specifies the interval between keep-alive
// probes for an active network connection.
// If zero, keep-alive probes are sent with a default value
// (currently 15 seconds), if supported by the protocol and operating
// system. Network protocols or operating systems that do
// not support keep-alives ignore this field.
// If negative, keep-alive probes are disabled.
// ^ from net.dial
// Defaults to 30s
KeepAliveSeconds int32 `yaml:"keepAliveSeconds,omitempty" json:"keepAliveSeconds,omitempty"`
TLS *ClientTLSSettings `yaml:"tls,omitempty" json:"tls,omitempty"`
}