mirror of https://github.com/status-im/consul.git
consul: Adding user event handler for callbacks
This commit is contained in:
parent
1c341e6766
commit
be78e355de
|
@ -300,14 +300,22 @@ func (c *Client) localEvent(event serf.UserEvent) {
|
|||
return
|
||||
}
|
||||
|
||||
switch event.Name {
|
||||
case newLeaderEvent:
|
||||
switch name := event.Name; {
|
||||
case name == newLeaderEvent:
|
||||
c.logger.Printf("[INFO] consul: New leader elected: %s", event.Payload)
|
||||
|
||||
// Trigger the callback
|
||||
if c.config.ServerUp != nil {
|
||||
c.config.ServerUp()
|
||||
}
|
||||
case isUserEvent(name):
|
||||
event.Name = rawUserEventName(name)
|
||||
c.logger.Printf("[DEBUG] consul: user event: %s", event.Name)
|
||||
|
||||
// Trigger the callback
|
||||
if c.config.UserEventHandler != nil {
|
||||
c.config.UserEventHandler(event)
|
||||
}
|
||||
default:
|
||||
c.logger.Printf("[WARN] consul: Unhandled local event: %v", event)
|
||||
}
|
||||
|
|
|
@ -163,6 +163,10 @@ type Config struct {
|
|||
// ServerUp callback can be used to trigger a notification that
|
||||
// a Consul server is now up and known about.
|
||||
ServerUp func()
|
||||
|
||||
// UserEventHandler callback can be used to handle incoming
|
||||
// user events. This function should not block.
|
||||
UserEventHandler func(serf.UserEvent)
|
||||
}
|
||||
|
||||
// CheckVersion is used to check if the ProtocolVersion is valid
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package consul
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
|
@ -12,11 +11,24 @@ const (
|
|||
// StatusReap is used to update the status of a node if we
|
||||
// are handling a EventMemberReap
|
||||
StatusReap = serf.MemberStatus(-1)
|
||||
|
||||
// userEventPrefix is pre-pended to a user event to distinguish it
|
||||
userEventPrefix = "consul:event:"
|
||||
)
|
||||
|
||||
// userEventName computes the name of a user event
|
||||
func userEventName(name string) string {
|
||||
return fmt.Sprintf("consul:event:%s")
|
||||
return userEventPrefix + name
|
||||
}
|
||||
|
||||
// isUserEvent checks if a serf event is a user event
|
||||
func isUserEvent(name string) bool {
|
||||
return strings.HasPrefix(name, userEventPrefix)
|
||||
}
|
||||
|
||||
// rawUserEventName is used to get the raw user event name
|
||||
func rawUserEventName(name string) string {
|
||||
return strings.TrimPrefix(name, userEventPrefix)
|
||||
}
|
||||
|
||||
// lanEventHandler is used to handle events from the lan Serf cluster
|
||||
|
@ -108,14 +120,22 @@ func (s *Server) localEvent(event serf.UserEvent) {
|
|||
return
|
||||
}
|
||||
|
||||
switch event.Name {
|
||||
case newLeaderEvent:
|
||||
switch name := event.Name; {
|
||||
case name == newLeaderEvent:
|
||||
s.logger.Printf("[INFO] consul: New leader elected: %s", event.Payload)
|
||||
|
||||
// Trigger the callback
|
||||
if s.config.ServerUp != nil {
|
||||
s.config.ServerUp()
|
||||
}
|
||||
case isUserEvent(name):
|
||||
event.Name = rawUserEventName(name)
|
||||
s.logger.Printf("[DEBUG] consul: user event: %s", event.Name)
|
||||
|
||||
// Trigger the callback
|
||||
if s.config.UserEventHandler != nil {
|
||||
s.config.UserEventHandler(event)
|
||||
}
|
||||
default:
|
||||
s.logger.Printf("[WARN] consul: Unhandled local event: %v", event)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue