Skip to content

Commit

Permalink
adopt the newly protobuf package and fix some docs type
Browse files Browse the repository at this point in the history
  • Loading branch information
gsw945 authored and lonng committed Jul 5, 2021
1 parent 57f7ee2 commit 3b95f60
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ go:
- 1.9.x
- 1.10.x
- 1.11.x
- 1.12.x
- 1.13.x
- 1.14.x
- 1.15.x
- 1.16.x

script:
- go test -v ./...
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,14 @@ go get -u google.golang.org/grpc
# download form: https://github.com/protocolbuffers/protobuf/releases
# protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
# delve
go install github.com/go-delve/delve/cmd/dlv@latest
```

## Test
```bash
go test -v ./...
```

## Benchmark
Expand All @@ -133,9 +139,8 @@ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
# Device: i5-6500 3.2GHz 4 Core/1000-Concurrent   => IOPS 11W(Average)
# Other: ...

cd $GOPATH/src/github.com/lonng/nano/benchmark/io
cd ./benchmark/io
go test -v -tags "benchmark"
go test -tags "benchmark" -v benchmark\io\io_test.go
```

## License
Expand Down
21 changes: 16 additions & 5 deletions benchmark/io/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
type TestHandler struct {
component.Base
metrics int32
group *nano.Group
}

func (h *TestHandler) AfterInit() {
Expand All @@ -41,16 +42,26 @@ func (h *TestHandler) AfterInit() {
}()
}

func NewTestHandler() *TestHandler {
return &TestHandler{
group: nano.NewGroup("handler"),
}
}

func (h *TestHandler) Ping(s *session.Session, data *testdata.Ping) error {
atomic.AddInt32(&h.metrics, 1)
return s.Push("pong", &testdata.Pong{Content: data.Content})
}

func server() {
nano.Register(&TestHandler{})
nano.SetSerializer(protobuf.NewSerializer())

nano.Listen(addr)
components := &component.Components{}
components.Register(NewTestHandler())

nano.Listen(addr,
nano.WithDebugMode(),
nano.WithSerializer(protobuf.NewSerializer()),
nano.WithComponents(components),
)
}

func client() {
Expand All @@ -68,7 +79,7 @@ func client() {
c.On("pong", func(data interface{}) {})

<-chReady
for {
for /*i := 0; i < 1; i++*/ {
c.Notify("TestHandler.Ping", &testdata.Ping{})
time.Sleep(10 * time.Millisecond)
}
Expand Down
24 changes: 15 additions & 9 deletions cluster/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ func (s *nodeSuite) TestNodeStartup(c *C) {
masterComps := &component.Components{}
masterComps.Register(&MasterComponent{})
masterNode := &cluster.Node{
IsMaster: true,
Options: cluster.Options{
IsMaster: true,
Components: masterComps,
},
ServiceAddr: "127.0.0.1:4450",
Components: masterComps,
}
err := masterNode.Startup()
c.Assert(err, IsNil)
Expand All @@ -66,10 +68,12 @@ func (s *nodeSuite) TestNodeStartup(c *C) {
member1Comps := &component.Components{}
member1Comps.Register(&GateComponent{})
memberNode1 := &cluster.Node{
AdvertiseAddr: "127.0.0.1:4450",
ServiceAddr: "127.0.0.1:14451",
ClientAddr: "127.0.0.1:14452",
Components: member1Comps,
Options: cluster.Options{
AdvertiseAddr: "127.0.0.1:4450",
ClientAddr: "127.0.0.1:14452",
Components: member1Comps,
},
ServiceAddr: "127.0.0.1:14451",
}
err = memberNode1.Startup()
c.Assert(err, IsNil)
Expand All @@ -82,9 +86,11 @@ func (s *nodeSuite) TestNodeStartup(c *C) {
member2Comps := &component.Components{}
member2Comps.Register(&GameComponent{})
memberNode2 := &cluster.Node{
AdvertiseAddr: "127.0.0.1:4450",
ServiceAddr: "127.0.0.1:24451",
Components: member2Comps,
Options: cluster.Options{
AdvertiseAddr: "127.0.0.1:4450",
Components: member2Comps,
},
ServiceAddr: "127.0.0.1:24451",
}
err = memberNode2.Startup()
c.Assert(err, IsNil)
Expand Down
15 changes: 11 additions & 4 deletions docs/get_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,22 @@ func (r *Room) Message(s *session.Session, msg *UserMessage) error {
}

func main() {
nano.Register(NewRoom())
nano.SetSerializer(json.NewSerializer())
nano.EnableDebug()
components := &component.Components{}
components.Register(NewRoom())

log.SetFlags(log.LstdFlags | log.Llongfile)

http.Handle("/web/", http.StripPrefix("/web/", http.FileServer(http.Dir("web"))))

nano.SetCheckOriginFunc(func(_ *http.Request) bool { return true })
nano.Listen(":3250", nano.WithIsWebsocket(true))
nano.Listen(":3250",
nano.WithIsWebsocket(true),
nano.WithCheckOriginFunc(func(_ *http.Request) bool { return true }),
nano.WithWSPath("/ws"),
nano.WithDebugMode(),
nano.WithSerializer(json.NewSerializer()), // override default serializer
nano.WithComponents(components),
)
}
```

Expand Down
15 changes: 11 additions & 4 deletions docs/get_started_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,22 @@ func (r *Room) Message(s *session.Session, msg *UserMessage) error {
}

func main() {
nano.Register(NewRoom())
nano.SetSerializer(json.NewSerializer())
nano.EnableDebug()
components := &component.Components{}
components.Register(NewRoom())

log.SetFlags(log.LstdFlags | log.Llongfile)

http.Handle("/web/", http.StripPrefix("/web/", http.FileServer(http.Dir("web"))))

nano.SetCheckOriginFunc(func(_ *http.Request) bool { return true })
nano.Listen(":3250", nano.WithIsWebsocket(true))
nano.Listen(":3250",
nano.WithIsWebsocket(true),
nano.WithCheckOriginFunc(func(_ *http.Request) bool { return true }),
nano.WithWSPath("/ws"),
nano.WithDebugMode(),
nano.WithSerializer(json.NewSerializer()), // override default serializer
nano.WithComponents(components),
)
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/route_compression_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ nano.request('Room.Join',
也就是说不再以字符串作为通信格式了,直接发送有效的二进制数据将会更好地减少额外开销,`nano`可以使用Protobuf
作为二进制协议, 可以通过:
```go
nano.SetSerializer(protobuf.NewSerializer())
nano.WithSerializer(protobuf.NewSerializer())
```
让nano选择Protobuf作为二进制协议

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.12

require (
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/pingcap/check v0.0.0-20200212061837-5e12011dc712 // indirect
github.com/pingcap/errors v0.11.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
Expand Down
4 changes: 2 additions & 2 deletions internal/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,15 @@ func Decode(data []byte) (*Message, error) {
m.compressed = false
rl := data[offset]
offset++
if offset+int(rl) >= len(data) {
if offset+int(rl) > len(data) {
return nil, ErrWrongMessage
}
m.Route = string(data[offset:(offset + int(rl))])
offset += int(rl)
}
}

if offset >= len(data) {
if offset > len(data) {
return nil, ErrWrongMessage
}
m.Data = data[offset:]
Expand Down
6 changes: 3 additions & 3 deletions serialize/protobuf/protobuf_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package protobuf

import (
"reflect"
"google.golang.org/protobuf/proto"
"testing"

"github.com/lonng/nano/benchmark/testdata"
Expand All @@ -20,8 +20,8 @@ func TestProtobufSerialezer_Serialize(t *testing.T) {
if err := s.Unmarshal(b, m1); err != nil {
t.Fatalf("unmarshal failed: %v", err)
}

if !reflect.DeepEqual(m, m1) {
// refer: https://developers.google.com/protocol-buffers/docs/reference/go/faq#deepequal
if !proto.Equal(m, m1) {
t.Fail()
}
}
Expand Down

0 comments on commit 3b95f60

Please sign in to comment.