implement some gossip options
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
f2efc685d3
commit
5bcdf189de
@ -2,9 +2,12 @@
|
|||||||
package gossip
|
package gossip
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -12,7 +15,7 @@ import (
|
|||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/hashicorp/memberlist"
|
"github.com/hashicorp/memberlist"
|
||||||
"github.com/micro/go-log"
|
log "github.com/micro/go-log"
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
pb "github.com/micro/go-micro/registry/gossip/proto"
|
pb "github.com/micro/go-micro/registry/gossip/proto"
|
||||||
"github.com/mitchellh/hashstructure"
|
"github.com/mitchellh/hashstructure"
|
||||||
@ -56,7 +59,7 @@ type update struct {
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
// You should change this if using secure
|
// You should change this if using secure
|
||||||
DefaultSecret = []byte("gossip")
|
DefaultSecret = []byte("micro-gossip-key") // exactly 16 bytes
|
||||||
ExpiryTick = time.Second * 5
|
ExpiryTick = time.Second * 5
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -104,14 +107,40 @@ func configure(g *gossipRegistry, opts ...registry.Option) error {
|
|||||||
RetransmitMult: 3,
|
RetransmitMult: 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
// machine hostname
|
|
||||||
hostname, _ := os.Hostname()
|
|
||||||
|
|
||||||
// create a new default config
|
// create a new default config
|
||||||
c := memberlist.DefaultLocalConfig()
|
c := memberlist.DefaultLocalConfig()
|
||||||
|
|
||||||
|
if optConfig, ok := g.options.Context.Value(contextConfig{}).(*memberlist.Config); ok && optConfig != nil {
|
||||||
|
c = optConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
if hostport, ok := g.options.Context.Value(contextAddress{}).(string); ok {
|
||||||
|
host, port, err := net.SplitHostPort(hostport)
|
||||||
|
if err == nil {
|
||||||
|
pn, err := strconv.Atoi(port)
|
||||||
|
if err == nil {
|
||||||
|
c.BindPort = pn
|
||||||
|
}
|
||||||
|
c.BindAddr = host
|
||||||
|
}
|
||||||
|
} else {
|
||||||
// set bind to random port
|
// set bind to random port
|
||||||
c.BindPort = 0
|
c.BindPort = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if hostport, ok := g.options.Context.Value(contextAdvertise{}).(string); ok {
|
||||||
|
host, port, err := net.SplitHostPort(hostport)
|
||||||
|
if err == nil {
|
||||||
|
pn, err := strconv.Atoi(port)
|
||||||
|
if err == nil {
|
||||||
|
c.AdvertisePort = pn
|
||||||
|
}
|
||||||
|
c.AdvertiseAddr = host
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// machine hostname
|
||||||
|
hostname, _ := os.Hostname()
|
||||||
|
|
||||||
// set the name
|
// set the name
|
||||||
c.Name = strings.Join([]string{"micro", hostname, uuid.New().String()}, "-")
|
c.Name = strings.Join([]string{"micro", hostname, uuid.New().String()}, "-")
|
||||||
@ -135,8 +164,6 @@ func configure(g *gossipRegistry, opts ...registry.Option) error {
|
|||||||
c.SecretKey = k
|
c.SecretKey = k
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: set advertise addr to advertise behind nat
|
|
||||||
|
|
||||||
// create the memberlist
|
// create the memberlist
|
||||||
m, err := memberlist.Create(c)
|
m, err := memberlist.Create(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -550,9 +577,13 @@ func NewRegistry(opts ...registry.Option) registry.Registry {
|
|||||||
watchers: make(map[string]chan *registry.Result),
|
watchers: make(map[string]chan *registry.Result),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if gossip.options.Context == nil {
|
||||||
|
gossip.options.Context = context.Background()
|
||||||
|
}
|
||||||
|
|
||||||
// configure the gossiper
|
// configure the gossiper
|
||||||
if err := configure(gossip, opts...); err != nil {
|
if err := configure(gossip, opts...); err != nil {
|
||||||
log.Fatal("Error configuring registry: %v", err)
|
log.Fatalf("Error configuring registry: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// run the updater
|
// run the updater
|
||||||
|
@ -3,6 +3,7 @@ package gossip
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/hashicorp/memberlist"
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,3 +16,30 @@ func Secret(k []byte) registry.Option {
|
|||||||
o.Context = context.WithValue(o.Context, contextSecretKey{}, k)
|
o.Context = context.WithValue(o.Context, contextSecretKey{}, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type contextAddress struct{}
|
||||||
|
|
||||||
|
// Address to bind to - host:port
|
||||||
|
func Address(a string) registry.Option {
|
||||||
|
return func(o *registry.Options) {
|
||||||
|
o.Context = context.WithValue(o.Context, contextAddress{}, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type contextConfig struct{}
|
||||||
|
|
||||||
|
// Config allow to inject a *memberlist.Config struct for configuring gossip
|
||||||
|
func Config(c *memberlist.Config) registry.Option {
|
||||||
|
return func(o *registry.Options) {
|
||||||
|
o.Context = context.WithValue(o.Context, contextConfig{}, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type contextAdvertise struct{}
|
||||||
|
|
||||||
|
// The address to advertise for other gossip members - host:port
|
||||||
|
func Advertise(a string) registry.Option {
|
||||||
|
return func(o *registry.Options) {
|
||||||
|
o.Context = context.WithValue(o.Context, contextAdvertise{}, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user