forked from sunmi-OS/gocore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
51 lines (40 loc) · 1013 Bytes
/
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
package rpcx
import (
"time"
"github.com/sunmi-OS/gocore/rpcx/clientinterceptors"
"google.golang.org/grpc"
)
type (
ClientOptions struct {
Timeout time.Duration
DialOptions []grpc.DialOption
}
ClientOption func(options *ClientOptions)
Client interface {
Next() (*grpc.ClientConn, bool)
}
)
func WithDialOption(opt grpc.DialOption) ClientOption {
return func(options *ClientOptions) {
options.DialOptions = append(options.DialOptions, opt)
}
}
func WithTimeout(timeout time.Duration) ClientOption {
return func(options *ClientOptions) {
options.Timeout = timeout
}
}
func buildDialOptions(opts ...ClientOption) []grpc.DialOption {
var clientOptions ClientOptions
for _, opt := range opts {
opt(&clientOptions)
}
options := []grpc.DialOption{
grpc.WithInsecure(),
WithUnaryClientInterceptors(
clientinterceptors.DurationInterceptor,
clientinterceptors.ForTimeoutInterceptor(clientOptions.Timeout),
),
}
return append(options, clientOptions.DialOptions...)
}