-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
client.go
52 lines (45 loc) · 1.39 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
package client
import (
"fmt"
"net/http"
"github.com/plgd-dev/hub/v2/pkg/fsnotify"
"github.com/plgd-dev/hub/v2/pkg/log"
"github.com/plgd-dev/hub/v2/pkg/security/certManager/client"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
)
// Server handles gRPC requests to the service.
type Client struct {
client *http.Client
closeFunc []func()
}
func (c *Client) HTTP() *http.Client {
return c.client
}
func (c *Client) AddCloseFunc(f func()) {
c.closeFunc = append(c.closeFunc, f)
}
func (c *Client) Close() {
c.client.CloseIdleConnections()
for _, f := range c.closeFunc {
f()
}
}
func New(config Config, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider) (*Client, error) {
certManager, err := client.New(config.TLS, fileWatcher, logger)
if err != nil {
return nil, fmt.Errorf("cannot create cert manager %w", err)
}
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = config.MaxIdleConns
t.MaxConnsPerHost = config.MaxConnsPerHost
t.MaxIdleConnsPerHost = config.MaxIdleConnsPerHost
t.IdleConnTimeout = config.IdleConnTimeout
t.TLSClientConfig = certManager.GetTLSConfig()
return &Client{
client: &http.Client{
Transport: otelhttp.NewTransport(t, otelhttp.WithTracerProvider(tracerProvider)),
Timeout: config.Timeout,
}, closeFunc: []func(){certManager.Close},
}, nil
}