From 1a9a51c5c24285f9b91e08cd6359bce1f99602f3 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 9 Dec 2013 14:49:07 -0800 Subject: [PATCH] consul: Adding Raft rpc endpoint --- consul/raft_endpoint.go | 35 +++++++++++++++++++++++++++++++++++ consul/server.go | 3 +++ 2 files changed, 38 insertions(+) create mode 100644 consul/raft_endpoint.go diff --git a/consul/raft_endpoint.go b/consul/raft_endpoint.go new file mode 100644 index 0000000000..96a6602012 --- /dev/null +++ b/consul/raft_endpoint.go @@ -0,0 +1,35 @@ +package consul + +import ( + "net" +) + +// Raft endpoint is used to manipulate the Raft subsystem +type Raft struct { + server *Server +} + +func (r *Raft) Apply(args []byte, reply *struct{}) error { + future := r.server.raft.Apply(args, 0) + return future.Error() +} + +func (r *Raft) AddPeer(args string, reply *struct{}) error { + peer, err := net.ResolveTCPAddr("tcp", args) + if err != nil { + r.server.logger.Printf("[ERR] Failed to parse peer: %v", err) + return err + } + future := r.server.raft.AddPeer(peer) + return future.Error() +} + +func (r *Raft) RemovePeer(args string, reply *struct{}) error { + peer, err := net.ResolveTCPAddr("tcp", args) + if err != nil { + r.server.logger.Printf("[ERR] Failed to parse peer: %v", err) + return err + } + future := r.server.raft.AddPeer(peer) + return future.Error() +} diff --git a/consul/server.go b/consul/server.go index 550f6f5257..027c582840 100644 --- a/consul/server.go +++ b/consul/server.go @@ -201,6 +201,9 @@ func (s *Server) setupRaft() error { // setupRPC is used to setup the RPC listener func (s *Server) setupRPC() error { + // Register the handlers + s.rpcServer.Register(&Raft{server: s}) + list, err := net.Listen("tcp", s.config.RPCAddr) if err != nil { return err