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

View File

@ -2,14 +2,15 @@ package client
import ( import (
"github.com/micro/go-micro/broker" "github.com/micro/go-micro/broker"
"github.com/micro/go-micro/codec"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
"github.com/micro/go-micro/transport" "github.com/micro/go-micro/transport"
) )
type options struct { type options struct {
contentType string contentType string
codecs map[string]CodecFunc
broker broker.Broker broker broker.Broker
codecs map[string]codec.Codec
registry registry.Registry registry registry.Registry
transport transport.Transport transport transport.Transport
wrappers []Wrapper 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 // 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) { return func(o *options) {
o.codecs[contentType] = cf o.codecs[contentType] = c
} }
} }

View File

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

23
codec/bson/bson.go Normal file
View File

@ -0,0 +1,23 @@
package bson
import (
"labix.org/v2/mgo/bson"
)
var (
Codec = bsonCodec{}
)
type bsonCodec struct {}
func (bsonCodec) Marshal(v interface{}) ([]byte, error) {
return bson.Marshal(v)
}
func (bsonCodec) Unmarshal(data []byte, v interface{}) error {
return bson.Unmarshal(data, v)
}
func (bsonCodec) String() string {
return "bson"
}

12
codec/codec.go Normal file
View File

@ -0,0 +1,12 @@
package codec
// Codec used to encode and decode request/responses
type Codec interface {
// Marshal returns the wire format of v.
Marshal(v interface{}) ([]byte, error)
// Unmarshal parses the wire format into v.
Unmarshal(data []byte, v interface{}) error
// String returns the name of the Codec implementation. The returned
// string will be used as part of content type in transmission.
String() string
}

24
codec/pb/pb.go Normal file
View File

@ -0,0 +1,24 @@
package pb
import (
"github.com/golang/protobuf/proto"
)
var (
Codec = protoCodec{}
)
type protoCodec struct {}
func (protoCodec) Marshal(v interface{}) ([]byte, error) {
return proto.Marshal(v.(proto.Message))
}
func (protoCodec) Unmarshal(data []byte, v interface{}) error {
return proto.Unmarshal(data, v.(proto.Message))
}
func (protoCodec) String() string {
return "proto"
}

View File

@ -6,6 +6,7 @@ import (
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd" "github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/codec/pb"
c "github.com/micro/go-micro/context" c "github.com/micro/go-micro/context"
example "github.com/micro/go-micro/examples/server/proto/example" example "github.com/micro/go-micro/examples/server/proto/example"
"golang.org/x/net/context" "golang.org/x/net/context"
@ -119,6 +120,12 @@ func stream() {
func main() { func main() {
cmd.Init() cmd.Init()
client.DefaultClient = client.NewClient(
client.Codec("application/pb", pb.Codec),
client.ContentType("application/pb"),
)
fmt.Println("\n--- Call example ---\n") fmt.Println("\n--- Call example ---\n")
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
call(i) call(i)

View File

@ -3,6 +3,7 @@ package main
import ( import (
log "github.com/golang/glog" log "github.com/golang/glog"
"github.com/micro/go-micro/cmd" "github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/codec/bson"
"github.com/micro/go-micro/examples/server/handler" "github.com/micro/go-micro/examples/server/handler"
"github.com/micro/go-micro/examples/server/subscriber" "github.com/micro/go-micro/examples/server/subscriber"
"github.com/micro/go-micro/server" "github.com/micro/go-micro/server"
@ -12,11 +13,16 @@ func main() {
// optionally setup command line usage // optionally setup command line usage
cmd.Init() cmd.Init()
server.DefaultServer = server.NewServer(
server.Codec("application/bson", bson.Codec),
)
// Initialise Server // Initialise Server
server.Init( server.Init(
server.Name("go.micro.srv.example"), server.Name("go.micro.srv.example"),
) )
// Register Handlers // Register Handlers
server.Handle( server.Handle(
server.NewHandler( server.NewHandler(

View File

@ -3,6 +3,9 @@ package server
import ( import (
"io" "io"
"net/rpc" "net/rpc"
"sync"
"github.com/micro/go-micro/codec"
"github.com/youtube/vitess/go/rpcplus" "github.com/youtube/vitess/go/rpcplus"
"github.com/youtube/vitess/go/rpcplus/jsonrpc" "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 // for internal use only
type codecFunc func(io.ReadWriteCloser) rpcplus.ServerCodec type codecFunc func(io.ReadWriteCloser) rpcplus.ServerCodec
// wraps an net/rpc ServerCodec to provide an rpcplus.ServerCodec // wraps an net/rpc ServerCodec to provide an rpcplus.ServerCodec
// temporary until we strip out use of rpcplus // temporary until we strip out use of rpcplus
type rpcCodecWrap struct { type rpcCodecWrap struct {
r rpc.ServerCodec sync.Mutex
rwc io.ReadWriteCloser
c codec.Codec
} }
func (cw *rpcCodecWrap) ReadRequestHeader(r *rpcplus.Request) error { func (cw *rpcCodecWrap) ReadRequestHeader(r *rpcplus.Request) error {
rc := &rpc.Request{ data, err := pbrpc.ReadNetString(cw.rwc)
ServiceMethod: r.ServiceMethod, if err != nil {
Seq: r.Seq,
}
err := cw.r.ReadRequestHeader(rc)
r.ServiceMethod = rc.ServiceMethod
r.Seq = rc.Seq
return err 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 { 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 { func (cw *rpcCodecWrap) WriteResponse(r *rpcplus.Response, b interface{}, l bool) error {
rc := &rpc.Response{ cw.Lock()
ServiceMethod: r.ServiceMethod, defer cw.Unlock()
Seq: r.Seq, rtmp := &rpc.Response{ServiceMethod: r.ServiceMethod, Seq: r.Seq, Error: r.Error}
Error: r.Error, data, err := cw.c.Marshal(rtmp)
} if err != nil {
err := cw.r.WriteResponse(rc, b)
r.ServiceMethod = rc.ServiceMethod
r.Seq = rc.Seq
r.Error = r.Error
return err 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 { func (cw *rpcCodecWrap) Close() error {
return cw.r.Close() return cw.rwc.Close()
} }
// wraps a CodecFunc to provide an internal codecFunc // wraps a CodecFunc to provide an internal codecFunc
// temporary until we strip rpcplus out // 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 func(rwc io.ReadWriteCloser) rpcplus.ServerCodec {
return &rpcCodecWrap{ return &rpcCodecWrap{
r: cf(rwc), rwc: rwc,
c: c,
} }
} }
} }

View File

@ -2,12 +2,13 @@ package server
import ( import (
"github.com/micro/go-micro/broker" "github.com/micro/go-micro/broker"
"github.com/micro/go-micro/codec"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
"github.com/micro/go-micro/transport" "github.com/micro/go-micro/transport"
) )
type options struct { type options struct {
codecs map[string]CodecFunc codecs map[string]codec.Codec
broker broker.Broker broker broker.Broker
registry registry.Registry registry registry.Registry
transport transport.Transport transport transport.Transport
@ -21,7 +22,7 @@ type options struct {
func newOptions(opt ...Option) options { func newOptions(opt ...Option) options {
opts := options{ opts := options{
codecs: make(map[string]CodecFunc), codecs: make(map[string]codec.Codec),
} }
for _, o := range opt { 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 // 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) { return func(o *options) {
o.codecs[contentType] = cf o.codecs[contentType] = c
} }
} }