-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.go
55 lines (45 loc) · 1.11 KB
/
options.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
package client
import (
"github.com/hb-go/grpc-contrib/registry"
"google.golang.org/grpc"
)
type Option func(options *Options)
type Options struct {
Name string
RegistryOptions []registry.Option
DialOptions []grpc.DialOption
}
// 默认gRPC DialOption
var DefaultDialOpts = []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithBalancerName("round_robin"),
grpc.WithBlock(),
}
func newOptions(options ...Option) Options {
opts := Options{
RegistryOptions: make([]registry.Option, 0),
DialOptions: DefaultDialOpts,
}
for _, o := range options {
o(&opts)
}
return opts
}
// 指定gRPC服务名称,替换ServiceDesc.ServiceName
func WithName(name string) Option {
return func(options *Options) {
options.Name = name
}
}
// 注册中心选项
func WithRegistryOptions(option ...registry.Option) Option {
return func(options *Options) {
options.RegistryOptions = append(options.RegistryOptions, option...)
}
}
// gRPC DialOption
func WithDialOptions(option ...grpc.DialOption) Option {
return func(options *Options) {
options.DialOptions = append(options.DialOptions, option...)
}
}