From 1c341e67669543fe8d4c717cf44f05a10ec189c0 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Tue, 26 Aug 2014 18:50:03 -0700 Subject: [PATCH] consul: expose UserEvent from Serf --- command/agent/agent.go | 9 +++++++++ consul/client.go | 5 +++++ consul/serf.go | 6 ++++++ consul/server.go | 5 +++++ 4 files changed, 25 insertions(+) diff --git a/command/agent/agent.go b/command/agent/agent.go index 289637adcc..d2a9d3e97b 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -372,6 +372,15 @@ func (a *Agent) WANMembers() []serf.Member { } } +// UserEvent is used to fire an event via the Serf layer on the LAN +func (a *Agent) UserEvent(name string, payload []byte) error { + if a.server != nil { + return a.server.UserEvent(name, payload) + } else { + return a.client.UserEvent(name, payload) + } +} + // StartSync is called once Services and Checks are registered. // This is called to prevent a race between clients and the anti-entropy routines func (a *Agent) StartSync() { diff --git a/consul/client.go b/consul/client.go index 70626d2de2..0ea5d31662 100644 --- a/consul/client.go +++ b/consul/client.go @@ -201,6 +201,11 @@ func (c *Client) RemoveFailedNode(node string) error { return c.serf.RemoveFailedNode(node) } +// UserEvent is used to fire an event via the Serf layer +func (c *Client) UserEvent(name string, payload []byte) error { + return c.serf.UserEvent(userEventName(name), payload, false) +} + // lanEventHandler is used to handle events from the lan Serf cluster func (c *Client) lanEventHandler() { for { diff --git a/consul/serf.go b/consul/serf.go index 37aae27257..3057846248 100644 --- a/consul/serf.go +++ b/consul/serf.go @@ -1,6 +1,7 @@ package consul import ( + "fmt" "net" "strings" @@ -13,6 +14,11 @@ const ( StatusReap = serf.MemberStatus(-1) ) +// userEventName computes the name of a user event +func userEventName(name string) string { + return fmt.Sprintf("consul:event:%s") +} + // lanEventHandler is used to handle events from the lan Serf cluster func (s *Server) lanEventHandler() { for { diff --git a/consul/server.go b/consul/server.go index d3fe6da924..a16b0e0be4 100644 --- a/consul/server.go +++ b/consul/server.go @@ -530,6 +530,11 @@ func (s *Server) RemoveFailedNode(node string) error { return nil } +// UserEvent is used to fire an event via the Serf layer on the LAN +func (s *Server) UserEvent(name string, payload []byte) error { + return s.serfLAN.UserEvent(userEventName(name), payload, false) +} + // IsLeader checks if this server is the cluster leader func (s *Server) IsLeader() bool { return s.raft.State() == raft.Leader