1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-30 08:06:40 +02:00
go-micro/registry/gossip/options.go

59 lines
1.6 KiB
Go
Raw Normal View History

2018-12-06 20:19:05 +02:00
package gossip
import (
"context"
"time"
2018-12-06 20:19:05 +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
}
// Address to bind to - host:port
func Address(a string) registry.Option {
2019-02-13 16:39:20 +02:00
return setRegistryOption(addressKey{}, a)
}
2019-02-13 16:39:20 +02:00
// Config sets *memberlist.Config for configuring gossip
func Config(c *memberlist.Config) registry.Option {
2019-02-13 16:39:20 +02:00
return setRegistryOption(configKey{}, c)
}
2019-02-13 16:39:20 +02:00
// The address to advertise for other gossip members to connect to - host:port
func Advertise(a string) registry.Option {
2019-02-13 16:39:20 +02:00
return setRegistryOption(advertiseKey{}, a)
}
2019-02-13 16:39:20 +02:00
// ConnectTimeout sets the registry connect timeout. Use -1 to specify infinite timeout
func ConnectTimeout(td time.Duration) registry.Option {
2019-02-13 16:39:20 +02:00
return setRegistryOption(connectTimeoutKey{}, td)
}
2019-02-13 16:39:20 +02:00
// ConnectRetry enables reconnect to registry then connection closed,
// 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)
}