From be78e355deef981fa9919206ec7cdef31b18707c Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Tue, 26 Aug 2014 19:04:07 -0700 Subject: [PATCH] consul: Adding user event handler for callbacks --- consul/client.go | 12 ++++++++++-- consul/config.go | 4 ++++ consul/serf.go | 28 ++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/consul/client.go b/consul/client.go index 0ea5d31662..050d147c2b 100644 --- a/consul/client.go +++ b/consul/client.go @@ -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) } diff --git a/consul/config.go b/consul/config.go index f49c8933a6..5404e71e6c 100644 --- a/consul/config.go +++ b/consul/config.go @@ -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 diff --git a/consul/serf.go b/consul/serf.go index 3057846248..0a6dc18ca3 100644 --- a/consul/serf.go +++ b/consul/serf.go @@ -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) }