experimental codec branch

This commit is contained in:
Asim
2015-11-27 00:17:36 +00:00
parent dc5f121c64
commit f49922f6b3
10 changed files with 183 additions and 57 deletions

View File

@@ -3,6 +3,9 @@ package client
import (
"io"
"net/rpc"
"sync"
"github.com/micro/go-micro/codec"
"github.com/youtube/vitess/go/rpcplus"
"github.com/youtube/vitess/go/rpcplus/jsonrpc"
@@ -21,55 +24,78 @@ var (
}
)
type CodecFunc func(io.ReadWriteCloser) rpc.ClientCodec
// only for internal use
type codecFunc func(io.ReadWriteCloser) rpcplus.ClientCodec
// wraps an net/rpc ClientCodec to provide an rpcplus.ClientCodec
// temporary until we strip out use of rpcplus
type rpcCodecWrap struct {
r rpc.ClientCodec
sync.Mutex
c codec.Codec
rwc io.ReadWriteCloser
}
func (cw *rpcCodecWrap) WriteRequest(r *rpcplus.Request, b interface{}) error {
rc := &rpc.Request{
ServiceMethod: r.ServiceMethod,
Seq: r.Seq,
cw.Lock()
defer cw.Unlock()
req := &rpc.Request{ServiceMethod: r.ServiceMethod, Seq: r.Seq}
data, err := cw.c.Marshal(req)
if err != nil {
return err
}
err := cw.r.WriteRequest(rc, b)
r.ServiceMethod = rc.ServiceMethod
r.Seq = rc.Seq
return err
_, err = pbrpc.WriteNetString(cw.rwc, data)
if err != nil {
return err
}
data, err = cw.c.Marshal(b)
if err != nil {
return err
}
_, err = pbrpc.WriteNetString(cw.rwc, data)
if err != nil {
return err
}
return nil
}
func (cw *rpcCodecWrap) ReadResponseHeader(r *rpcplus.Response) error {
rc := &rpc.Response{
ServiceMethod: r.ServiceMethod,
Seq: r.Seq,
Error: r.Error,
data, err := pbrpc.ReadNetString(cw.rwc)
if err != nil {
return err
}
err := cw.r.ReadResponseHeader(rc)
r.ServiceMethod = rc.ServiceMethod
r.Seq = rc.Seq
r.Error = r.Error
return err
rtmp := new(rpc.Response)
err = cw.c.Unmarshal(data, rtmp)
if err != nil {
return err
}
r.ServiceMethod = rtmp.ServiceMethod
r.Seq = rtmp.Seq
r.Error = rtmp.Error
return nil
}
func (cw *rpcCodecWrap) ReadResponseBody(b interface{}) error {
return cw.r.ReadResponseBody(b)
data, err := pbrpc.ReadNetString(cw.rwc)
if err != nil {
return err
}
if b != nil {
return cw.c.Unmarshal(data, b)
}
return nil
}
func (cw *rpcCodecWrap) Close() error {
return cw.r.Close()
return cw.rwc.Close()
}
// wraps a CodecFunc to provide an internal codecFunc
// temporary until we strip rpcplus out
func codecWrap(cf CodecFunc) codecFunc {
func codecWrap(c codec.Codec) codecFunc {
return func(rwc io.ReadWriteCloser) rpcplus.ClientCodec {
return &rpcCodecWrap{
r: cf(rwc),
rwc: rwc,
c: c,
}
}
}

View File

@@ -2,14 +2,15 @@ package client
import (
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/codec"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/transport"
)
type options struct {
contentType string
codecs map[string]CodecFunc
broker broker.Broker
codecs map[string]codec.Codec
registry registry.Registry
transport transport.Transport
wrappers []Wrapper
@@ -23,9 +24,9 @@ func Broker(b broker.Broker) Option {
}
// Codec to be used to encode/decode requests for a given content type
func Codec(contentType string, cf CodecFunc) Option {
func Codec(contentType string, c codec.Codec) Option {
return func(o *options) {
o.codecs[contentType] = cf
o.codecs[contentType] = c
}
}

View File

@@ -6,6 +6,7 @@ import (
"sync"
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/codec"
c "github.com/micro/go-micro/context"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/registry"
@@ -26,7 +27,7 @@ func newRpcClient(opt ...Option) Client {
var once sync.Once
opts := options{
codecs: make(map[string]CodecFunc),
codecs: make(map[string]codec.Codec),
}
for _, o := range opt {
@@ -36,6 +37,7 @@ func newRpcClient(opt ...Option) Client {
if len(opts.contentType) == 0 {
opts.contentType = defaultContentType
}
fmt.Println("content type", opts.contentType)
if opts.transport == nil {
opts.transport = transport.DefaultTransport
@@ -61,8 +63,8 @@ func newRpcClient(opt ...Option) Client {
}
func (r *rpcClient) codecFunc(contentType string) (codecFunc, error) {
if cf, ok := r.opts.codecs[contentType]; ok {
return codecWrap(cf), nil
if c, ok := r.opts.codecs[contentType]; ok {
return codecWrap(c), nil
}
if cf, ok := defaultCodecs[contentType]; ok {
return cf, nil