mirror of
https://github.com/go-micro/go-micro.git
synced 2025-08-10 21:52:01 +02:00
experimental codec branch
This commit is contained in:
@@ -3,6 +3,9 @@ package server
|
||||
import (
|
||||
"io"
|
||||
"net/rpc"
|
||||
"sync"
|
||||
|
||||
"github.com/micro/go-micro/codec"
|
||||
|
||||
"github.com/youtube/vitess/go/rpcplus"
|
||||
"github.com/youtube/vitess/go/rpcplus/jsonrpc"
|
||||
@@ -19,56 +22,77 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// CodecFunc is used to encode/decode requests/responses
|
||||
type CodecFunc func(io.ReadWriteCloser) rpc.ServerCodec
|
||||
|
||||
// for internal use only
|
||||
type codecFunc func(io.ReadWriteCloser) rpcplus.ServerCodec
|
||||
|
||||
// wraps an net/rpc ServerCodec to provide an rpcplus.ServerCodec
|
||||
// temporary until we strip out use of rpcplus
|
||||
type rpcCodecWrap struct {
|
||||
r rpc.ServerCodec
|
||||
sync.Mutex
|
||||
rwc io.ReadWriteCloser
|
||||
c codec.Codec
|
||||
}
|
||||
|
||||
func (cw *rpcCodecWrap) ReadRequestHeader(r *rpcplus.Request) error {
|
||||
rc := &rpc.Request{
|
||||
ServiceMethod: r.ServiceMethod,
|
||||
Seq: r.Seq,
|
||||
data, err := pbrpc.ReadNetString(cw.rwc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err := cw.r.ReadRequestHeader(rc)
|
||||
r.ServiceMethod = rc.ServiceMethod
|
||||
r.Seq = rc.Seq
|
||||
return err
|
||||
rtmp := new(rpc.Request)
|
||||
err = cw.c.Unmarshal(data, rtmp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.ServiceMethod = rtmp.ServiceMethod
|
||||
r.Seq = rtmp.Seq
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cw *rpcCodecWrap) ReadRequestBody(b interface{}) error {
|
||||
return cw.r.ReadRequestBody(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) WriteResponse(r *rpcplus.Response, b interface{}, l bool) error {
|
||||
rc := &rpc.Response{
|
||||
ServiceMethod: r.ServiceMethod,
|
||||
Seq: r.Seq,
|
||||
Error: r.Error,
|
||||
cw.Lock()
|
||||
defer cw.Unlock()
|
||||
rtmp := &rpc.Response{ServiceMethod: r.ServiceMethod, Seq: r.Seq, Error: r.Error}
|
||||
data, err := cw.c.Marshal(rtmp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err := cw.r.WriteResponse(rc, b)
|
||||
r.ServiceMethod = rc.ServiceMethod
|
||||
r.Seq = rc.Seq
|
||||
r.Error = r.Error
|
||||
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) 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.ServerCodec {
|
||||
return &rpcCodecWrap{
|
||||
r: cf(rwc),
|
||||
rwc: rwc,
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,12 +2,13 @@ package server
|
||||
|
||||
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 {
|
||||
codecs map[string]CodecFunc
|
||||
codecs map[string]codec.Codec
|
||||
broker broker.Broker
|
||||
registry registry.Registry
|
||||
transport transport.Transport
|
||||
@@ -21,7 +22,7 @@ type options struct {
|
||||
|
||||
func newOptions(opt ...Option) options {
|
||||
opts := options{
|
||||
codecs: make(map[string]CodecFunc),
|
||||
codecs: make(map[string]codec.Codec),
|
||||
}
|
||||
|
||||
for _, o := range opt {
|
||||
@@ -126,9 +127,9 @@ func Broker(b broker.Broker) Option {
|
||||
}
|
||||
|
||||
// Codec to use 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
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user