Skip to content

Commit

Permalink
[+] support thrift
Browse files Browse the repository at this point in the history
  • Loading branch information
goodjava@qq.com committed Jun 27, 2018
1 parent 59c2597 commit 1cae50b
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 0 deletions.
7 changes: 7 additions & 0 deletions _testutils/GoUnusedProtection__.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions _testutils/thrift_arith_service-consts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

238 changes: 238 additions & 0 deletions _testutils/thrift_arith_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions _testutils/thrift_arith_service.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct ThriftArgs
{
1: i32 a,
2: i32 b,
}


struct ThriftReply
{
1: i32 c,
}
36 changes: 36 additions & 0 deletions client/xclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,44 @@ import (
"time"

"github.com/smallnest/rpcx/server"
"github.com/smallnest/rpcx/share"
"github.com/smallnest/rpcx/protocol"
"github.com/smallnest/rpcx/_testutils"
)


func TestXClient_Thrift(t *testing.T) {
opt := Option{
Retries: 3,
RPCPath: share.DefaultRPCPath,
ConnectTimeout: 10 * time.Second,
SerializeType: protocol.Thrift,
CompressType: protocol.None,
BackupLatency: 10 * time.Millisecond,
}

d := NewPeer2PeerDiscovery("tcp@127.0.0.1:8999", "desc=a test service")
xclient := NewXClient("Arith", Failtry, RandomSelect, d, opt)

defer xclient.Close()

args := testutils.ThriftArgs_{}
args.A = 200
args.B = 100

reply := testutils.ThriftReply{}

err := xclient.Call(context.Background(), "ThriftMul", &args, &reply)
if err != nil {
t.Fatalf("failed to call: %v", err)
}

if reply.C != 20000 {
t.Fatalf("expect 20000 but got %d", reply.C)
}
}


func TestXClient_IT(t *testing.T) {
s := server.NewServer()
s.RegisterName("Arith", new(Arith), "")
Expand Down
26 changes: 26 additions & 0 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
proto "github.com/gogo/protobuf/proto"
pb "github.com/golang/protobuf/proto"
"github.com/vmihailenco/msgpack"

"git.apache.org/thrift.git/lib/go/thrift"
)

// Codec defines the interface that decode/encode payload.
Expand Down Expand Up @@ -99,3 +101,27 @@ func (c MsgpackCodec) Decode(data []byte, i interface{}) error {
err := dec.Decode(i)
return err
}

type ThriftCodec struct{}

func (c ThriftCodec) Encode(i interface{}) ([]byte, error) {
b := thrift.NewTMemoryBufferLen(1024)
p := thrift.NewTBinaryProtocolFactoryDefault().GetProtocol(b)
t := &thrift.TSerializer{
Transport: b,
Protocol: p,
}
t.Transport.Close()
return t.Write(i.(thrift.TStruct))
}

func (c ThriftCodec) Decode(data []byte, i interface{}) error {
t := thrift.NewTMemoryBufferLen(1024)
p := thrift.NewTBinaryProtocolFactoryDefault().GetProtocol(t)
d := &thrift.TDeserializer{
Transport: t,
Protocol: p,
}
d.Transport.Close()
return d.Read(i.(thrift.TStruct), data)
}
3 changes: 3 additions & 0 deletions protocol/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ const (
ProtoBuffer
// MsgPack for payload
MsgPack
// Thrift
// Thrift for payload
Thrift
)

// Message is the generic type of Request and Response.
Expand Down
Loading

0 comments on commit 1cae50b

Please sign in to comment.