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