Skip to content

Commit

Permalink
style: optimize code
Browse files Browse the repository at this point in the history
Change-Id: I3712660dcab1ceaf27fd1a90e4950f3a60e9ca6e
  • Loading branch information
andeya committed Dec 11, 2018
1 parent e235e3e commit 30c6f51
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 32 deletions.
4 changes: 2 additions & 2 deletions proto/jsonproto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func TestJsonProto(t *testing.T) {
// Server
srv := tp.NewPeer(tp.PeerConfig{ListenPort: 9090})
srv.RouteCall(new(Home))
go srv.ListenAndServe(jsonproto.NewJsonProtoFunc)
go srv.ListenAndServe(jsonproto.NewJSONProtoFunc())
time.Sleep(1e9)

// Client
cli := tp.NewPeer(tp.PeerConfig{})
cli.RoutePush(new(Push))
sess, err := cli.Dial(":9090", jsonproto.NewJsonProtoFunc)
sess, err := cli.Dial(":9090", jsonproto.NewJSONProtoFunc())
if err != nil {
t.Error(err)
}
Expand Down
14 changes: 8 additions & 6 deletions proto/jsonproto/jsonproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ import (
"github.com/henrylee2cn/teleport/utils"
)

// NewJsonProtoFunc is creation function of JSON socket protocol.
// NewJSONProtoFunc is creation function of JSON socket protocol.
// Message data format: {length bytes}{xfer_pipe length byte}{xfer_pipe bytes}{JSON bytes}
// Message data demo: `830{"seq":%q,"mtype":%d,"serviceMethod":%q,"meta":%q,"bodyCodec":%d,"body":"%s"}`
var NewJsonProtoFunc = func(rw tp.IOWithReadBuffer) tp.Proto {
return &jsonproto{
id: 'j',
name: "json",
rw: rw,
func NewJSONProtoFunc() tp.ProtoFunc {
return func(rw tp.IOWithReadBuffer) tp.Proto {
return &jsonproto{
id: 'j',
name: "json",
rw: rw,
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions proto/jsonproto/jsonproto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func TestJsonProto(t *testing.T) {
// Server
srv := tp.NewPeer(tp.PeerConfig{ListenPort: 9090})
srv.RouteCall(new(Home))
go srv.ListenAndServe(jsonproto.NewJsonProtoFunc)
go srv.ListenAndServe(jsonproto.NewJSONProtoFunc())
time.Sleep(1e9)

// Client
cli := tp.NewPeer(tp.PeerConfig{})
cli.RoutePush(new(Push))
sess, err := cli.Dial(":9090", jsonproto.NewJsonProtoFunc)
sess, err := cli.Dial(":9090", jsonproto.NewJSONProtoFunc())
if err != nil {
t.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions proto/pbproto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func TestPbProto(t *testing.T) {
// server
srv := tp.NewPeer(tp.PeerConfig{ListenPort: 9090})
srv.RouteCall(new(Home))
go srv.ListenAndServe(pbproto.NewPbProtoFunc)
go srv.ListenAndServe(pbproto.NewPbProtoFunc())
time.Sleep(1e9)

// client
cli := tp.NewPeer(tp.PeerConfig{})
cli.RoutePush(new(Push))
sess, err := cli.Dial(":9090", pbproto.NewPbProtoFunc)
sess, err := cli.Dial(":9090", pbproto.NewPbProtoFunc())
if err != nil {
t.Error(err)
}
Expand Down
12 changes: 7 additions & 5 deletions proto/pbproto/pbproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import (
)

// NewPbProtoFunc is creation function of PROTOBUF socket protocol.
var NewPbProtoFunc = func(rw tp.IOWithReadBuffer) tp.Proto {
return &pbproto{
id: 'p',
name: "protobuf",
rw: rw,
func NewPbProtoFunc() tp.ProtoFunc {
return func(rw tp.IOWithReadBuffer) tp.Proto {
return &pbproto{
id: 'p',
name: "protobuf",
rw: rw,
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions proto/pbproto/pbproto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func TestPbProto(t *testing.T) {
// server
srv := tp.NewPeer(tp.PeerConfig{ListenPort: 9090})
srv.RouteCall(new(Home))
go srv.ListenAndServe(pbproto.NewPbProtoFunc)
go srv.ListenAndServe(pbproto.NewPbProtoFunc())
time.Sleep(1e9)

// client
cli := tp.NewPeer(tp.PeerConfig{})
cli.RoutePush(new(Push))
sess, err := cli.Dial(":9090", pbproto.NewPbProtoFunc)
sess, err := cli.Dial(":9090", pbproto.NewPbProtoFunc())
if err != nil {
t.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions proto/rawproto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (h *Home) Test(arg *map[string]string) (map[string]interface{}, *tp.Rerror)
}, nil
}

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

// server
Expand Down Expand Up @@ -98,5 +98,5 @@ func (p *Push) Test(arg *map[string]string) *tp.Rerror {
test command:

```sh
go test -v -run=TestPbProto
go test -v -run=TestRawProto
```
5 changes: 4 additions & 1 deletion proto/rawproto/rawproto.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rawproto

import (
tp "github.com/henrylee2cn/teleport"
"github.com/henrylee2cn/teleport/socket"
)

Expand All @@ -27,4 +28,6 @@ import (
// NOTE:
// it is the default protocol.
// id:'r', name:"raw"
var NewRawProtoFunc = socket.NewRawProtoFunc
func NewRawProtoFunc() tp.ProtoFunc {
return socket.RawProtoFunc
}
2 changes: 1 addition & 1 deletion proto/rawproto/rawproto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (h *Home) Test(arg *map[string]string) (map[string]interface{}, *tp.Rerror)
}, nil
}

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

// server
Expand Down
4 changes: 2 additions & 2 deletions proto/thriftproto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func TestTProto(t *testing.T) {
// server
srv := tp.NewPeer(tp.PeerConfig{ListenPort: 9090})
srv.RouteCall(new(Home))
go srv.ListenAndServe(thriftproto.NewTProtoFactory())
go srv.ListenAndServe(thriftproto.NewTProtoFunc())
time.Sleep(1e9)

// client
cli := tp.NewPeer(tp.PeerConfig{})
cli.RoutePush(new(Push))
sess, err := cli.Dial(":9090", thriftproto.NewTProtoFactory())
sess, err := cli.Dial(":9090", thriftproto.NewTProtoFunc())
if err != nil {
t.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions proto/thriftproto/thriftproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func init() {
tp.SetServiceMethodMapper(tp.RPCServiceMethodMapper)
}

// NewTProtoFactory creates tp.ProtoFunc of Thrift protocol.
// NewTProtoFunc creates tp.ProtoFunc of Thrift protocol.
// NOTE:
// If @factory is not provided, use the default binary protocol.
func NewTProtoFactory(factory ...thrift.TProtocolFactory) tp.ProtoFunc {
func NewTProtoFunc(factory ...thrift.TProtocolFactory) tp.ProtoFunc {
var fa thrift.TProtocolFactory
if len(factory) > 0 {
fa = factory[0]
Expand Down
4 changes: 2 additions & 2 deletions proto/thriftproto/thriftproto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func TestTProto(t *testing.T) {
// server
srv := tp.NewPeer(tp.PeerConfig{ListenPort: 9090})
srv.RouteCall(new(Home))
go srv.ListenAndServe(thriftproto.NewTProtoFactory())
go srv.ListenAndServe(thriftproto.NewTProtoFunc())
time.Sleep(1e9)

// client
cli := tp.NewPeer(tp.PeerConfig{})
cli.RoutePush(new(Push))
sess, err := cli.Dial(":9090", thriftproto.NewTProtoFactory())
sess, err := cli.Dial(":9090", thriftproto.NewTProtoFunc())
if err != nil {
t.Error(err)
}
Expand Down
6 changes: 3 additions & 3 deletions socket/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type (
)

// default builder of socket communication protocol.
var defaultProtoFunc = NewRawProtoFunc
var defaultProtoFunc = RawProtoFunc

// DefaultProtoFunc gets the default builder of socket communication protocol
func DefaultProtoFunc() ProtoFunc {
Expand Down Expand Up @@ -90,9 +90,9 @@ type rawProto struct {
rMu sync.Mutex
}

// NewRawProtoFunc is creation function of fast socket protocol.
// RawProtoFunc is creation function of fast socket protocol.
// NOTE: it is the default protocol.
var NewRawProtoFunc = func(rw IOWithReadBuffer) Proto {
var RawProtoFunc = func(rw IOWithReadBuffer) Proto {
return &rawProto{
id: 'r',
name: "raw",
Expand Down

0 comments on commit 30c6f51

Please sign in to comment.