Rename shared msgpack handle

This commit is contained in:
Armon Dadgar 2014-06-08 14:02:42 -07:00
parent 3847db27b5
commit 474e286aca
8 changed files with 24 additions and 17 deletions

View File

@ -60,7 +60,12 @@ const (
monitorExists = "Monitor already exists"
)
var mh = codec.MsgpackHandle{RawToString: true, WriteExt: true}
// msgpackHandle is a shared handle for encoding/decoding of
// messages
var msgpackHandle = &codec.MsgpackHandle{
RawToString: true,
WriteExt: true,
}
// Request header is sent before each request
type requestHeader struct {
@ -251,8 +256,8 @@ func (i *AgentRPC) listen() {
reader: bufio.NewReader(conn),
writer: bufio.NewWriter(conn),
}
client.dec = codec.NewDecoder(client.reader, &mh)
client.enc = codec.NewEncoder(client.writer, &mh)
client.dec = codec.NewDecoder(client.reader, msgpackHandle)
client.enc = codec.NewEncoder(client.writer, msgpackHandle)
if err != nil {
i.logger.Printf("[ERR] agent.rpc: Failed to create decoder: %v", err)
conn.Close()

View File

@ -94,8 +94,8 @@ func NewRPCClient(addr string) (*RPCClient, error) {
dispatch: make(map[uint64]seqHandler),
shutdownCh: make(chan struct{}),
}
client.dec = codec.NewDecoder(client.reader, &mh)
client.enc = codec.NewEncoder(client.writer, &mh)
client.dec = codec.NewDecoder(client.reader, msgpackHandle)
client.enc = codec.NewEncoder(client.writer, msgpackHandle)
go client.listen()
// Do the initial handshake

View File

@ -221,7 +221,7 @@ func (c *consulFSM) Restore(old io.ReadCloser) error {
c.state = state
// Create a decoder
dec := codec.NewDecoder(old, &mh)
dec := codec.NewDecoder(old, msgpackHandle)
// Read in the header
var header snapshotHeader
@ -277,7 +277,7 @@ func (c *consulFSM) Restore(old io.ReadCloser) error {
func (s *consulSnapshot) Persist(sink raft.SnapshotSink) error {
// Register the nodes
encoder := codec.NewEncoder(sink, &mh)
encoder := codec.NewEncoder(sink, msgpackHandle)
// Write the header
header := snapshotHeader{

View File

@ -19,7 +19,7 @@ type MockData struct {
func MockEncoder(obj interface{}) []byte {
buf := bytes.NewBuffer(nil)
encoder := codec.NewEncoder(buf, &mh)
encoder := codec.NewEncoder(buf, msgpackHandle)
err := encoder.Encode(obj)
if err != nil {
panic(err)
@ -29,7 +29,7 @@ func MockEncoder(obj interface{}) []byte {
func MockDecoder(buf []byte) interface{} {
out := new(MockData)
err := codec.NewDecoder(bytes.NewReader(buf), &mh).Decode(out)
err := codec.NewDecoder(bytes.NewReader(buf), msgpackHandle).Decode(out)
if err != nil {
panic(err)
}

View File

@ -15,7 +15,8 @@ import (
"time"
)
var mh = codec.MsgpackHandle{}
// msgpackHandle is a shared handle for encoding/decoding of RPC messages
var msgpackHandle = &codec.MsgpackHandle{}
// muxSession is used to provide an interface for either muxado or yamux
type muxSession interface {
@ -81,7 +82,7 @@ func (c *Conn) getClient() (*StreamClient, error) {
}
// Create the RPC client
cc := codec.GoRpc.ClientCodec(stream, &mh)
cc := codec.GoRpc.ClientCodec(stream, msgpackHandle)
client := rpc.NewClientWithCodec(cc)
// Return a new stream client

View File

@ -148,7 +148,7 @@ func (s *Server) handleMultiplexV2(conn net.Conn) {
// handleConsulConn is used to service a single Consul RPC connection
func (s *Server) handleConsulConn(conn net.Conn) {
defer conn.Close()
rpcCodec := codec.GoRpc.ServerCodec(conn, &mh)
rpcCodec := codec.GoRpc.ServerCodec(conn, msgpackHandle)
for !s.shutdown {
if err := s.rpcServer.ServeRequest(rpcCodec); err != nil {
if err != io.EOF && !strings.Contains(err.Error(), "closed") {

View File

@ -19,7 +19,7 @@ func rpcClient(t *testing.T, s *Server) *rpc.Client {
// Write the Consul RPC byte to set the mode
conn.Write([]byte{byte(rpcConsul)})
cc := codec.GoRpc.ClientCodec(conn, &mh)
cc := codec.GoRpc.ClientCodec(conn, msgpackHandle)
return rpc.NewClientWithCodec(cc)
}

View File

@ -395,17 +395,18 @@ type IndexedSessions struct {
QueryMeta
}
var mh = codec.MsgpackHandle{}
// msgpackHandle is a shared handle for encoding/decoding of structs
var msgpackHandle = &codec.MsgpackHandle{}
// Decode is used to decode a MsgPack encoded object
func Decode(buf []byte, out interface{}) error {
return codec.NewDecoder(bytes.NewReader(buf), &mh).Decode(out)
return codec.NewDecoder(bytes.NewReader(buf), msgpackHandle).Decode(out)
}
// Encode is used to encode a MsgPack object with type prefix
func Encode(t MessageType, msg interface{}) ([]byte, error) {
buf := bytes.NewBuffer(nil)
var buf bytes.Buffer
buf.WriteByte(uint8(t))
err := codec.NewEncoder(buf, &mh).Encode(msg)
err := codec.NewEncoder(&buf, msgpackHandle).Encode(msg)
return buf.Bytes(), err
}