-
Notifications
You must be signed in to change notification settings - Fork 72
/
options.go
100 lines (82 loc) · 2.61 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package mesh
import (
"context"
"github.com/dobyte/due/v2/crypto"
"github.com/dobyte/due/v2/encoding"
"github.com/dobyte/due/v2/etc"
"github.com/dobyte/due/v2/locate"
"github.com/dobyte/due/v2/registry"
"github.com/dobyte/due/v2/transport"
"time"
)
const (
defaultName = "mesh" // 默认节点名称
defaultCodec = "proto" // 默认编解码器名称
defaultTimeout = 3 * time.Second // 默认超时时间
)
const (
defaultNameKey = "etc.cluster.mesh.name"
defaultCodecKey = "etc.cluster.mesh.codec"
defaultTimeoutKey = "etc.cluster.mesh.timeout"
)
type Option func(o *options)
type options struct {
name string // 实例名称
ctx context.Context // 上下文
codec encoding.Codec // 编解码器
timeout time.Duration // RPC调用超时时间
locator locate.Locator // 用户定位器
registry registry.Registry // 服务注册器
transporter transport.Transporter // 消息传输器
encryptor crypto.Encryptor // 消息加密器
}
func defaultOptions() *options {
opts := &options{
ctx: context.Background(),
name: defaultName,
codec: encoding.Invoke(defaultCodec),
timeout: defaultTimeout,
}
if name := etc.Get(defaultNameKey).String(); name != "" {
opts.name = name
}
if codec := etc.Get(defaultCodecKey).String(); codec != "" {
opts.codec = encoding.Invoke(codec)
}
if timeout := etc.Get(defaultTimeoutKey).Int64(); timeout > 0 {
opts.timeout = time.Duration(timeout) * time.Second
}
return opts
}
// WithName 设置实例名称
func WithName(name string) Option {
return func(o *options) { o.name = name }
}
// WithCodec 设置编解码器
func WithCodec(codec encoding.Codec) Option {
return func(o *options) { o.codec = codec }
}
// WithContext 设置上下文
func WithContext(ctx context.Context) Option {
return func(o *options) { o.ctx = ctx }
}
// WithTimeout 设置RPC调用超时时间
func WithTimeout(timeout time.Duration) Option {
return func(o *options) { o.timeout = timeout }
}
// WithLocator 设置定位器
func WithLocator(locator locate.Locator) Option {
return func(o *options) { o.locator = locator }
}
// WithRegistry 设置服务注册器
func WithRegistry(r registry.Registry) Option {
return func(o *options) { o.registry = r }
}
// WithTransporter 设置消息传输器
func WithTransporter(transporter transport.Transporter) Option {
return func(o *options) { o.transporter = transporter }
}
// WithEncryptor 设置消息加密器
func WithEncryptor(encryptor crypto.Encryptor) Option {
return func(o *options) { o.encryptor = encryptor }
}