-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
49 lines (41 loc) · 1.31 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
package grpc
import (
"context"
sgrpc "github.com/alexfalkowski/go-service/transport/grpc"
"github.com/alexfalkowski/go-service/transport/grpc/metrics/prometheus"
"github.com/alexfalkowski/go-service/transport/grpc/trace/opentracing"
v1 "github.com/alexfalkowski/konfig/api/konfig/v1"
"github.com/alexfalkowski/konfig/client/v1/config"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/grpc"
)
// ServiceClientParams for gRPC.
type ServiceClientParams struct {
fx.In
Lifecycle fx.Lifecycle
Config *sgrpc.Config
Logger *zap.Logger
Tracer opentracing.Tracer
Client *config.Config
Metrics *prometheus.ClientMetrics
}
// NewServiceClient for gRPC.
func NewServiceClient(params ServiceClientParams) (v1.ServiceClient, error) {
ctx, cancel := context.WithTimeout(context.Background(), params.Client.Timeout)
defer cancel()
conn, err := sgrpc.NewClient(
sgrpc.ClientParams{Context: ctx, Host: params.Client.Host, Config: params.Config},
sgrpc.WithClientLogger(params.Logger), sgrpc.WithClientTracer(params.Tracer), sgrpc.WithClientDialOption(grpc.WithBlock()),
sgrpc.WithClientMetrics(params.Metrics),
)
if err != nil {
return nil, err
}
params.Lifecycle.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return conn.Close()
},
})
return v1.NewServiceClient(conn), nil
}