consul: Allow receiving RPC connections

pull/19/head
Armon Dadgar 2014-02-05 15:29:52 -08:00
parent e72ad27850
commit 15d2a6a51e
1 changed files with 22 additions and 0 deletions

View File

@ -3,10 +3,12 @@ package consul
import ( import (
"fmt" "fmt"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
"github.com/inconshreveable/muxado"
"github.com/ugorji/go/codec" "github.com/ugorji/go/codec"
"io" "io"
"math/rand" "math/rand"
"net" "net"
"strings"
"time" "time"
) )
@ -15,6 +17,7 @@ type RPCType byte
const ( const (
rpcConsul RPCType = iota rpcConsul RPCType = iota
rpcRaft rpcRaft
rpcMultiplex
) )
const ( const (
@ -62,6 +65,9 @@ func (s *Server) handleConn(conn net.Conn) {
case rpcRaft: case rpcRaft:
s.raftLayer.Handoff(conn) s.raftLayer.Handoff(conn)
case rpcMultiplex:
s.handleMultiplex(conn)
default: default:
s.logger.Printf("[ERR] consul.rpc: unrecognized RPC byte: %v", buf[0]) s.logger.Printf("[ERR] consul.rpc: unrecognized RPC byte: %v", buf[0])
conn.Close() conn.Close()
@ -69,6 +75,22 @@ func (s *Server) handleConn(conn net.Conn) {
} }
} }
// handleMultiplex is used to multiplex a single incoming connection
func (s *Server) handleMultiplex(conn net.Conn) {
defer conn.Close()
server := muxado.Server(conn)
for {
sub, err := server.Accept()
if err != nil {
if !strings.Contains(err.Error(), "closed") {
s.logger.Printf("[ERR] consul.rpc: multiplex conn accept failed: %v", err)
}
return
}
go s.handleConsulConn(sub)
}
}
// handleConsulConn is used to service a single Consul RPC connection // handleConsulConn is used to service a single Consul RPC connection
func (s *Server) handleConsulConn(conn net.Conn) { func (s *Server) handleConsulConn(conn net.Conn) {
defer func() { defer func() {