mirror of https://github.com/status-im/consul.git
agent: Adding server up callback to make state sync faster
This commit is contained in:
parent
f8bd1a1ac3
commit
c1637b4978
|
@ -93,14 +93,17 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) {
|
|||
shutdownCh: make(chan struct{}),
|
||||
}
|
||||
|
||||
// Initialize the local state
|
||||
agent.state.Init(config, agent.logger)
|
||||
|
||||
// Setup either the client or the server
|
||||
var err error
|
||||
if config.Server {
|
||||
err = agent.setupServer()
|
||||
agent.state.Init(config, agent.server, agent.logger)
|
||||
agent.state.SetIface(agent.server)
|
||||
} else {
|
||||
err = agent.setupClient()
|
||||
agent.state.Init(config, agent.client, agent.logger)
|
||||
agent.state.SetIface(agent.client)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -162,6 +165,9 @@ func (a *Agent) consulConfig() *consul.Config {
|
|||
base.Bootstrap = true
|
||||
}
|
||||
|
||||
// Setup the ServerUp callback
|
||||
base.ServerUp = a.state.ConsulServerUp
|
||||
|
||||
// Setup the loggers
|
||||
base.LogOutput = a.logOutput
|
||||
return base
|
||||
|
|
|
@ -56,9 +56,8 @@ type localState struct {
|
|||
}
|
||||
|
||||
// Init is used to initialize the local state
|
||||
func (l *localState) Init(config *Config, iface consul.Interface, logger *log.Logger) {
|
||||
func (l *localState) Init(config *Config, logger *log.Logger) {
|
||||
l.config = config
|
||||
l.iface = iface
|
||||
l.logger = logger
|
||||
l.services = make(map[string]*structs.NodeService)
|
||||
l.serviceStatus = make(map[string]syncStatus)
|
||||
|
@ -68,6 +67,12 @@ func (l *localState) Init(config *Config, iface consul.Interface, logger *log.Lo
|
|||
l.triggerCh = make(chan struct{}, 1)
|
||||
}
|
||||
|
||||
// SetIface is used to set the Consul interface. Must be set prior to
|
||||
// starting anti-entropy
|
||||
func (l *localState) SetIface(iface consul.Interface) {
|
||||
l.iface = iface
|
||||
}
|
||||
|
||||
// changeMade is used to trigger an anti-entropy run
|
||||
func (l *localState) changeMade() {
|
||||
select {
|
||||
|
|
|
@ -218,6 +218,11 @@ func (c *Client) nodeJoin(me serf.MemberEvent) {
|
|||
c.consuls = append(c.consuls, addr)
|
||||
}
|
||||
c.consulLock.Unlock()
|
||||
|
||||
// Trigger the callback
|
||||
if c.config.ServerUp != nil {
|
||||
c.config.ServerUp()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -64,6 +64,10 @@ type Config struct {
|
|||
// LogOutput is the location to write logs to. If this is not set,
|
||||
// logs will go to stderr.
|
||||
LogOutput io.Writer
|
||||
|
||||
// ServerUp callback can be used to trigger a notification that
|
||||
// a Consul server is now up and known about.
|
||||
ServerUp func()
|
||||
}
|
||||
|
||||
// DefaultConfig is used to return a sane default configuration
|
||||
|
|
|
@ -95,6 +95,11 @@ func (s *Server) remoteJoin(me serf.MemberEvent) {
|
|||
s.remoteConsuls[parts.Datacenter] = append(existing, addr)
|
||||
}
|
||||
s.remoteLock.Unlock()
|
||||
|
||||
// Trigger the callback
|
||||
if s.config.ServerUp != nil {
|
||||
s.config.ServerUp()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue