/
options.go
58 lines (48 loc) · 1.57 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
package gossip
import (
"context"
"time"
"github.com/hashicorp/memberlist"
"go-micro.dev/v4/registry"
)
type secretKey struct{}
type addressKey struct{}
type configKey struct{}
type advertiseKey struct{}
type connectTimeoutKey struct{}
type connectRetryKey struct{}
// helper for setting registry options.
func setRegistryOption(k, v interface{}) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}
// Secret specifies an encryption key. The value should be either
// 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
func Secret(k []byte) registry.Option {
return setRegistryOption(secretKey{}, k)
}
// Address to bind to - host:port.
func Address(a string) registry.Option {
return setRegistryOption(addressKey{}, a)
}
// Config sets *memberlist.Config for configuring gossip.
func Config(c *memberlist.Config) registry.Option {
return setRegistryOption(configKey{}, c)
}
// The address to advertise for other gossip members to connect to - host:port.
func Advertise(a string) registry.Option {
return setRegistryOption(advertiseKey{}, a)
}
// ConnectTimeout sets the registry connect timeout. Use -1 to specify infinite timeout.
func ConnectTimeout(td time.Duration) registry.Option {
return setRegistryOption(connectTimeoutKey{}, td)
}
// ConnectRetry enables reconnect to registry then connection closed,
// use with ConnectTimeout to specify how long retry.
func ConnectRetry(v bool) registry.Option {
return setRegistryOption(connectRetryKey{}, v)
}