-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
47 lines (40 loc) · 1.15 KB
/
grpc.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
package grpc
import (
"context"
"github.com/alexfalkowski/go-service/client"
"github.com/alexfalkowski/go-service/env"
"github.com/alexfalkowski/go-service/transport/grpc"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
"go.uber.org/zap"
g "google.golang.org/grpc"
)
// ClientOpts for gRPC.
type ClientOpts struct {
Lifecycle fx.Lifecycle
Client *client.Config
Logger *zap.Logger
Tracer trace.Tracer
Meter metric.Meter
UseAgent env.UserAgent
}
// NewClient for gRPC.
func NewClient(options ClientOpts) (*g.ClientConn, error) {
sec, err := grpc.WithClientTLS(options.Client.TLS)
if err != nil {
return nil, err
}
opts := []grpc.ClientOption{
grpc.WithClientLogger(options.Logger), grpc.WithClientTracer(options.Tracer),
grpc.WithClientMetrics(options.Meter), grpc.WithClientRetry(options.Client.Retry),
grpc.WithClientUserAgent(options.UseAgent), grpc.WithClientTimeout(options.Client.Timeout), sec,
}
conn, err := grpc.NewClient(options.Client.Host, opts...)
options.Lifecycle.Append(fx.Hook{
OnStop: func(_ context.Context) error {
return conn.Close()
},
})
return conn, err
}