2018-12-06 18:19:05 +00:00
|
|
|
package gossip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-10 13:15:40 +03:00
|
|
|
"time"
|
2018-12-06 18:19:05 +00:00
|
|
|
|
2018-12-19 00:57:16 +03:00
|
|
|
"github.com/hashicorp/memberlist"
|
2018-12-06 18:19:05 +00:00
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
2019-02-13 14:39:20 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 18:19:05 +00:00
|
|
|
|
|
|
|
// 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 {
|
2019-02-13 14:39:20 +00:00
|
|
|
return setRegistryOption(secretKey{}, k)
|
2018-12-06 18:19:05 +00:00
|
|
|
}
|
2018-12-19 00:57:16 +03:00
|
|
|
|
|
|
|
// Address to bind to - host:port
|
|
|
|
func Address(a string) registry.Option {
|
2019-02-13 14:39:20 +00:00
|
|
|
return setRegistryOption(addressKey{}, a)
|
2018-12-19 00:57:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:39:20 +00:00
|
|
|
// Config sets *memberlist.Config for configuring gossip
|
2018-12-19 00:57:16 +03:00
|
|
|
func Config(c *memberlist.Config) registry.Option {
|
2019-02-13 14:39:20 +00:00
|
|
|
return setRegistryOption(configKey{}, c)
|
2018-12-19 00:57:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:39:20 +00:00
|
|
|
// The address to advertise for other gossip members to connect to - host:port
|
2018-12-19 00:57:16 +03:00
|
|
|
func Advertise(a string) registry.Option {
|
2019-02-13 14:39:20 +00:00
|
|
|
return setRegistryOption(advertiseKey{}, a)
|
2019-01-30 15:39:57 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:39:20 +00:00
|
|
|
// ConnectTimeout sets the registry connect timeout. Use -1 to specify infinite timeout
|
2019-02-10 13:15:40 +03:00
|
|
|
func ConnectTimeout(td time.Duration) registry.Option {
|
2019-02-13 14:39:20 +00:00
|
|
|
return setRegistryOption(connectTimeoutKey{}, td)
|
2019-02-10 13:15:40 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:39:20 +00:00
|
|
|
// ConnectRetry enables reconnect to registry then connection closed,
|
2019-02-10 13:15:40 +03:00
|
|
|
// use with ConnectTimeout to specify how long retry
|
|
|
|
func ConnectRetry(v bool) registry.Option {
|
2019-02-13 14:39:20 +00:00
|
|
|
return setRegistryOption(connectRetryKey{}, v)
|
2019-02-10 13:15:40 +03:00
|
|
|
}
|