Skip to content

Commit

Permalink
Merge branch 'main' into custom_packet_message_codec
Browse files Browse the repository at this point in the history
# Conflicts:
#	builder.go
  • Loading branch information
pipiaha committed May 25, 2023
2 parents dbd27a6 + 04e8c01 commit 6f24573
Show file tree
Hide file tree
Showing 51 changed files with 3,543 additions and 969 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.17'
go-version: '1.20'
- name: Checkout
uses: actions/checkout@v2
- name: Restore cache
Expand All @@ -36,7 +36,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.17'
go-version: '1.20'
- name: Checkout
uses: actions/checkout@v2
- name: Restore cache
Expand Down Expand Up @@ -64,7 +64,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.17'
go-version: '1.20'
- name: Checkout
uses: actions/checkout@v2
- name: Restore cache
Expand All @@ -84,7 +84,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.17'
go-version: '1.20'
- name: Checkout
uses: actions/checkout@v2
- name: Restore cache
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
# Binaries for programs and plugins
*.exe
*.dll
Expand Down
30 changes: 29 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
TESTABLE_PACKAGES = `go list ./... | grep -v examples | grep -v constants | grep -v mocks | grep -v helpers | grep -v interfaces | grep -v protos | grep -v e2e | grep -v benchmark`
ifeq ($(OS), Windows_NT)
BIN := pitaya-cli.exe
MKFOLDER := if not exist "build" mkdir build
GREP_CMD := findstr /V
else
BIN := pitaya-cli
MKFOLDER := mkdir -p build
GREP_CMD := grep -v
endif

TESTABLE_PACKAGES = `go list ./... | $(GREP_CMD) examples | $(GREP_CMD) constants | $(GREP_CMD) mocks | $(GREP_CMD) helpers | $(GREP_CMD) interfaces | $(GREP_CMD) protos | $(GREP_CMD) e2e | $(GREP_CMD) benchmark`

setup: init-submodules
@go get ./...

build-cli:
@$(MKFOLDER)
@go build -o build/$(BIN) github.com/topfreegames/pitaya/v2/pitaya-cli
@echo "build pitaya-cli at ./build/$(BIN)"

init-submodules:
@git submodule init

Expand All @@ -14,9 +29,19 @@ setup-protobuf-macos:
@brew install protobuf
@go get github.com/golang/protobuf/protoc-gen-go

run-jaeger-aio:
@docker-compose -f ./examples/testing/docker-compose-jaeger.yml up -d
@echo "Access jaeger UI @ http://localhost:16686"

run-chat-example:
@cd examples/testing && docker-compose up -d etcd nats && cd ../demo/chat/ && go run main.go

run-cluster-example-frontend-tracing:
@PITAYA_METRICS_PROMETHEUS_PORT=9090 JAEGER_SAMPLER_PARAM=1 JAEGER_DISABLED=false JAEGER_SERVICE_NAME=example-frontend JAEGER_AGENT_PORT=6832 go run examples/demo/cluster/main.go

run-cluster-example-backend-tracing:
@PITAYA_METRICS_PROMETHEUS_PORT=9091 JAEGER_SAMPLER_PARAM=1 JAEGER_DISABLED=false JAEGER_SERVICE_NAME=example-backend JAEGER_AGENT_PORT=6832 go run examples/demo/cluster/main.go --port 3251 --type room --frontend=false

run-cluster-example-frontend:
@PITAYA_METRICS_PROMETHEUS_PORT=9090 go run examples/demo/cluster/main.go

Expand Down Expand Up @@ -74,6 +99,9 @@ ensure-e2e-deps-grpc:
kill-testing-deps:
@cd ./examples/testing && docker-compose down; true

kill-jaeger:
@docker-compose -f ./examples/testing/docker-compose-jaeger.yml down; true

e2e-test: e2e-test-nats e2e-test-grpc

e2e-test-nats: ensure-testing-deps ensure-testing-bin
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ run the room backend server from the cluster_grpc example
make run-cluster-grpc-example-room
```

Now there should be 2 pitaya servers running, a frontend connector and a backend room. To send requests, use a REPL client for pitaya [pitaya-cli](https://github.com/topfreegames/pitaya-cli).
Now there should be 2 pitaya servers running, a frontend connector and a backend room. To send requests, use a REPL client for pitaya [pitaya-cli](https://github.com/topfreegames/pitaya/tree/main/cli).

```
$ pitaya-cli
Expand Down Expand Up @@ -97,7 +97,7 @@ If you have found a security vulnerability, please email [email protected]
+ [libpitaya](https://github.com/topfreegames/libpitaya)
+ [pitaya-admin](https://github.com/topfreegames/pitaya-admin)
+ [pitaya-bot](https://github.com/topfreegames/pitaya-bot)
+ [pitaya-cli](https://github.com/topfreegames/pitaya-cli)
+ [pitaya-cli](https://github.com/topfreegames/pitaya/tree/main/cli)
+ [pitaya-protos](https://github.com/topfreegames/pitaya-protos)

- Documents
Expand Down
66 changes: 54 additions & 12 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ var (
// hbd contains the heartbeat packet data
hbd []byte
// hrd contains the handshake response data
hrd []byte
hrd []byte
// herd contains the handshake error response data
herd []byte
once sync.Once
)

Expand Down Expand Up @@ -110,6 +112,7 @@ type (
Handle()
IPVersion() string
SendHandshakeResponse() error
SendHandshakeErrorResponse() error
SendRequest(ctx context.Context, serverID, route string, v interface{}) (*protos.Response, error)
AnswerWithError(ctx context.Context, mid uint, err error)
}
Expand Down Expand Up @@ -180,6 +183,7 @@ func newAgent(

once.Do(func() {
hbdEncode(heartbeatTime, packetEncoder, messageEncoder.IsCompressionEnabled(), serializerName)
herdEncode(heartbeatTime, packetEncoder, messageEncoder.IsCompressionEnabled(), serializerName)
})

a := &agentImpl{
Expand Down Expand Up @@ -475,6 +479,14 @@ func (a *agentImpl) onSessionClosed(s session.Session) {
// SendHandshakeResponse sends a handshake response
func (a *agentImpl) SendHandshakeResponse() error {
_, err := a.conn.Write(hrd)

return err
}

func (a *agentImpl) SendHandshakeErrorResponse() error {
a.SetStatus(constants.StatusClosed)
_, err := a.conn.Write(herd)

return err
}

Expand Down Expand Up @@ -543,33 +555,63 @@ func hbdEncode(heartbeatTimeout time.Duration, packetEncoder codec.PacketEncoder
"serializer": serializerName,
},
}
data, err := gojson.Marshal(hData)

data, err := encodeAndCompress(hData, dataCompression)
if err != nil {
panic(err)
}

if dataCompression {
compressedData, err := compression.DeflateData(data)
if err != nil {
panic(err)
}
hrd, err = packetEncoder.Encode(packet.Handshake, data)
if err != nil {
panic(err)
}

if len(compressedData) < len(data) {
data = compressedData
}
hbd, err = packetEncoder.Encode(packet.Heartbeat, nil)
if err != nil {
panic(err)
}
}

hrd, err = packetEncoder.Encode(packet.Handshake, data)
func herdEncode(heartbeatTimeout time.Duration, packetEncoder codec.PacketEncoder, dataCompression bool, serializerName string) {
hErrData := map[string]interface{}{
"code": 400,
"sys": map[string]interface{}{
"heartbeat": heartbeatTimeout.Seconds(),
"dict": message.GetDictionary(),
"serializer": serializerName,
},
}

errData, err := encodeAndCompress(hErrData, dataCompression)
if err != nil {
panic(err)
}

hbd, err = packetEncoder.Encode(packet.Heartbeat, nil)
herd, err = packetEncoder.Encode(packet.Handshake, errData)
if err != nil {
panic(err)
}
}

func encodeAndCompress(data interface{}, dataCompression bool) ([]byte, error) {
encData, err := gojson.Marshal(data)
if err != nil {
return nil, err
}

if dataCompression {
compressedData, err := compression.DeflateData(encData)
if err != nil {
return nil, err
}

if len(compressedData) < len(encData) {
encData = compressedData
}
}
return encData, nil
}

func (a *agentImpl) reportChannelSize() {
chSendCapacity := a.messagesBufferSize - len(a.chSend)
if chSendCapacity == 0 {
Expand Down
2 changes: 1 addition & 1 deletion agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestNewAgent(t *testing.T) {
func(typ packet.Type, d []byte) {
// cannot compare inside the expect because they are equivalent but not equal
assert.EqualValues(t, packet.Handshake, typ)
})
}).Times(2)
mockEncoder.EXPECT().Encode(gomock.Any(), gomock.Nil()).Do(
func(typ packet.Type, d []byte) {
assert.EqualValues(t, packet.Heartbeat, typ)
Expand Down
Loading

0 comments on commit 6f24573

Please sign in to comment.