This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
67 lines (55 loc) · 1.6 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
package grpc
import (
"context"
v1 "github.com/alexfalkowski/auth/api/auth/v1"
v1c "github.com/alexfalkowski/auth/client/v1/config"
"github.com/alexfalkowski/go-service/security/token"
"github.com/alexfalkowski/go-service/transport/grpc"
"github.com/alexfalkowski/go-service/transport/grpc/telemetry/tracer"
"go.opentelemetry.io/otel/metric"
"go.uber.org/fx"
"go.uber.org/zap"
)
// ServiceClientParams for gRPC.
type ServiceClientParams struct {
fx.In
Lifecycle fx.Lifecycle
GRPCConfig *grpc.Config
ClientConfig *v1c.Config
TokenConfig *token.Config
Logger *zap.Logger
Tracer tracer.Tracer
Meter metric.Meter
}
// NewServiceClient for gRPC.
func NewServiceClient(params ServiceClientParams) (v1.ServiceClient, error) {
if params.TokenConfig.Kind != "auth" {
return nil, nil
}
opts := []grpc.ClientOption{
grpc.WithClientLogger(params.Logger), grpc.WithClientTracer(params.Tracer),
grpc.WithClientMetrics(params.Meter), grpc.WithClientRetry(¶ms.GRPCConfig.Retry),
grpc.WithClientUserAgent(params.GRPCConfig.UserAgent),
}
if params.GRPCConfig.Security.IsEnabled() {
sec, err := grpc.WithClientSecure(params.GRPCConfig.Security)
if err != nil {
return nil, err
}
opts = append(opts, sec)
}
conn, err := grpc.NewClient(context.Background(), params.ClientConfig.Host, opts...)
if err != nil {
return nil, err
}
params.Lifecycle.Append(fx.Hook{
OnStart: func(_ context.Context) error {
conn.ResetConnectBackoff()
return nil
},
OnStop: func(_ context.Context) error {
return conn.Close()
},
})
return v1.NewServiceClient(conn), nil
}