Skip to content

Commit

Permalink
feat: add thrift proto
Browse files Browse the repository at this point in the history
Change-Id: I182b21fa941f093781591e962fb37bea34e52137
  • Loading branch information
andeya committed Dec 11, 2018
1 parent aa3e314 commit 41fb685
Show file tree
Hide file tree
Showing 7 changed files with 641 additions and 0 deletions.
93 changes: 93 additions & 0 deletions proto/thriftproto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
## thriftproto

thriftproto is implemented thrift communication protocol.

### Message IDL

```thrift
struct payload {
1: binary meta,
2: binary xferPipe,
3: i32 bodyCodec,
4: binary body
}
```

### Usage

`import "github.com/henrylee2cn/teleport/proto/thriftproto"`

#### Test

```go
package thriftproto_test

import (
"testing"
"time"

tp "github.com/henrylee2cn/teleport"
"github.com/henrylee2cn/teleport/proto/thriftproto"
"github.com/henrylee2cn/teleport/xfer/gzip"
)

type Home struct {
tp.CallCtx
}

func (h *Home) Test(arg *map[string]string) (map[string]interface{}, *tp.Rerror) {
h.Session().Push("Push.Test", map[string]string{
"your_id": string(h.PeekMeta("peer_id")),
})
return map[string]interface{}{
"arg": *arg,
}, nil
}

func TestTProto(t *testing.T) {
gzip.Reg('g', "gizp-5", 5)

// server
srv := tp.NewPeer(tp.PeerConfig{ListenPort: 9090})
srv.RouteCall(new(Home))
go srv.ListenAndServe(thriftproto.NewTProtoFactory())
time.Sleep(1e9)

// client
cli := tp.NewPeer(tp.PeerConfig{})
cli.RoutePush(new(Push))
sess, err := cli.Dial(":9090", thriftproto.NewTProtoFactory())
if err != nil {
t.Error(err)
}
var result interface{}
rerr := sess.Call("Home.Test",
map[string]string{
"author": "henrylee2cn",
},
&result,
tp.WithAddMeta("peer_id", "110"),
tp.WithXferPipe('g'),
).Rerror()
if rerr != nil {
t.Error(rerr)
}
t.Logf("result:%v", result)
time.Sleep(2e9)
}

type Push struct {
tp.PushCtx
}

func (p *Push) Test(arg *map[string]string) *tp.Rerror {
tp.Infof("receive push(%s):\narg: %#v\n", p.IP(), arg)
return nil
}
```

test command:

```sh
go test -v -run=TestTProto
```
7 changes: 7 additions & 0 deletions proto/thriftproto/gen-go/payload/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 proto/thriftproto/gen-go/payload/payload-consts.go

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

219 changes: 219 additions & 0 deletions proto/thriftproto/gen-go/payload/payload.go

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

15 changes: 15 additions & 0 deletions proto/thriftproto/payload.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace go payload
namespace perl payload
namespace py payload
namespace cpp payload
namespace rb payload
namespace java payload

// thrift-0.11.0

struct payload {
1: binary meta,
2: binary xferPipe,
3: i32 bodyCodec,
4: binary body
}
Loading

0 comments on commit 41fb685

Please sign in to comment.