-
Notifications
You must be signed in to change notification settings - Fork 53
/
client.go
58 lines (47 loc) · 1.41 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
package remoteread
import (
"fmt"
"github.com/rancher/opni/pkg/util/waitctx"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type ClientOptions struct {
listenAddr string
dialOptions []grpc.DialOption
}
type ClientOption func(*ClientOptions)
func (opt *ClientOptions) apply(opts ...ClientOption) {
for _, op := range opts {
op(opt)
}
}
func WithListenAddress(addr string) ClientOption {
return func(opt *ClientOptions) {
opt.listenAddr = addr
}
}
func WithDialOptions(options ...grpc.DialOption) ClientOption {
return func(opt *ClientOptions) {
opt.dialOptions = append(opt.dialOptions, options...)
}
}
func NewGatewayClient(ctx waitctx.PermissiveContext, opts ...ClientOption) (RemoteReadGatewayClient, error) {
options := ClientOptions{
dialOptions: []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithChainStreamInterceptor(otelgrpc.StreamClientInterceptor()),
grpc.WithChainUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
},
}
options.apply(opts...)
connection, err := grpc.DialContext(ctx, options.listenAddr, options.dialOptions...)
if err != nil {
return nil, fmt.Errorf("could not establish connection: %w", err)
}
waitctx.Permissive.Go(ctx, func() {
<-ctx.Done()
connection.Close()
})
return NewRemoteReadGatewayClient(connection), nil
}