From e06b553476f87bf3e480c660c0993459dfc8d94d Mon Sep 17 00:00:00 2001 From: alexstocks Date: Sun, 17 Mar 2024 20:00:55 +0800 Subject: [PATCH 01/10] upgrade as apache/dubbo-getty v1.4.10 --- .github/workflows/github-actions.yml | 4 +- .../tcp-echo/client/assembly/common/build.sh | 7 +- .../tcp-echo/server/assembly/common/build.sh | 7 +- .../udp-echo/client/assembly/common/build.sh | 7 +- .../udp-echo/server/assembly/common/build.sh | 7 +- .../ws-echo/client/assembly/common/build.sh | 7 +- .../ws-echo/server/assembly/common/build.sh | 7 +- .../wss-echo/client/assembly/common/build.sh | 7 +- .../wss-echo/server/assembly/common/build.sh | 7 +- go.mod | 30 ++++++-- go.sum | 4 - transport/client.go | 17 ++--- transport/connection.go | 74 ++++++++----------- transport/server.go | 12 ++- transport/session.go | 28 +++++-- transport/tls.go | 8 +- 16 files changed, 132 insertions(+), 101 deletions(-) diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index f114c7bc..74f46e5a 100644 --- a/.github/workflows/github-actions.yml +++ b/.github/workflows/github-actions.yml @@ -15,7 +15,7 @@ jobs: # If you want to matrix build , you can append the following list. matrix: go_version: - - 1.13 + - "1.20" os: - ubuntu-latest @@ -54,7 +54,7 @@ jobs: /tmp/tools/license/license-header-checker -v -a -r -i vendor /tmp/tools/license/license.txt . go && [[ -z `git status -s` ]] - name: Install go ci lint - run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.27.0 + run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.56.0 - name: Run Linter run: golangci-lint run --timeout=10m -v --disable-all --enable=govet --enable=staticcheck --enable=ineffassign --enable=misspell diff --git a/examples/echo/tcp-echo/client/assembly/common/build.sh b/examples/echo/tcp-echo/client/assembly/common/build.sh index 00763725..2652bafb 100644 --- a/examples/echo/tcp-echo/client/assembly/common/build.sh +++ b/examples/echo/tcp-echo/client/assembly/common/build.sh @@ -15,7 +15,8 @@ PROJECT_HOME=`pwd` TARGET_FOLDER=${PROJECT_HOME}/target/${GOOS} TARGET_SBIN_NAME=${TARGET_EXEC_NAME} -version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +#version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +version="1.0" if [[ ${GOOS} == "windows" ]]; then TARGET_SBIN_NAME=${TARGET_SBIN_NAME}.exe fi @@ -24,12 +25,12 @@ if [[ $PROFILE == "dev" || $PROFILE == "test" ]]; then # GFLAGS=-gcflags "-N -l" -race -x -v # -x会把go build的详细过程输出 # GFLAGS=-gcflags "-N -l" -race -v # GFLAGS="-gcflags \"-N -l\" -v" - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -o ${TARGET_NAME} && cd - else # -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了,这个等价于普通C/C++程序被strip的效果), # -w去掉DWARF调试信息,得到的程序就不能用gdb调试了。-s和-w也可以分开使用,一般来说如果不打算用gdb调试, # -w基本没啥损失。-s的损失就有点大了。 - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -o ${TARGET_NAME} && cd - fi TAR_NAME=${TARGET_EXEC_NAME}-${version}-`date "+%Y%m%d-%H%M"`-${PROFILE} diff --git a/examples/echo/tcp-echo/server/assembly/common/build.sh b/examples/echo/tcp-echo/server/assembly/common/build.sh index 00763725..2652bafb 100644 --- a/examples/echo/tcp-echo/server/assembly/common/build.sh +++ b/examples/echo/tcp-echo/server/assembly/common/build.sh @@ -15,7 +15,8 @@ PROJECT_HOME=`pwd` TARGET_FOLDER=${PROJECT_HOME}/target/${GOOS} TARGET_SBIN_NAME=${TARGET_EXEC_NAME} -version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +#version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +version="1.0" if [[ ${GOOS} == "windows" ]]; then TARGET_SBIN_NAME=${TARGET_SBIN_NAME}.exe fi @@ -24,12 +25,12 @@ if [[ $PROFILE == "dev" || $PROFILE == "test" ]]; then # GFLAGS=-gcflags "-N -l" -race -x -v # -x会把go build的详细过程输出 # GFLAGS=-gcflags "-N -l" -race -v # GFLAGS="-gcflags \"-N -l\" -v" - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -o ${TARGET_NAME} && cd - else # -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了,这个等价于普通C/C++程序被strip的效果), # -w去掉DWARF调试信息,得到的程序就不能用gdb调试了。-s和-w也可以分开使用,一般来说如果不打算用gdb调试, # -w基本没啥损失。-s的损失就有点大了。 - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -o ${TARGET_NAME} && cd - fi TAR_NAME=${TARGET_EXEC_NAME}-${version}-`date "+%Y%m%d-%H%M"`-${PROFILE} diff --git a/examples/echo/udp-echo/client/assembly/common/build.sh b/examples/echo/udp-echo/client/assembly/common/build.sh index 00763725..2652bafb 100644 --- a/examples/echo/udp-echo/client/assembly/common/build.sh +++ b/examples/echo/udp-echo/client/assembly/common/build.sh @@ -15,7 +15,8 @@ PROJECT_HOME=`pwd` TARGET_FOLDER=${PROJECT_HOME}/target/${GOOS} TARGET_SBIN_NAME=${TARGET_EXEC_NAME} -version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +#version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +version="1.0" if [[ ${GOOS} == "windows" ]]; then TARGET_SBIN_NAME=${TARGET_SBIN_NAME}.exe fi @@ -24,12 +25,12 @@ if [[ $PROFILE == "dev" || $PROFILE == "test" ]]; then # GFLAGS=-gcflags "-N -l" -race -x -v # -x会把go build的详细过程输出 # GFLAGS=-gcflags "-N -l" -race -v # GFLAGS="-gcflags \"-N -l\" -v" - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -o ${TARGET_NAME} && cd - else # -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了,这个等价于普通C/C++程序被strip的效果), # -w去掉DWARF调试信息,得到的程序就不能用gdb调试了。-s和-w也可以分开使用,一般来说如果不打算用gdb调试, # -w基本没啥损失。-s的损失就有点大了。 - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -o ${TARGET_NAME} && cd - fi TAR_NAME=${TARGET_EXEC_NAME}-${version}-`date "+%Y%m%d-%H%M"`-${PROFILE} diff --git a/examples/echo/udp-echo/server/assembly/common/build.sh b/examples/echo/udp-echo/server/assembly/common/build.sh index 00763725..2652bafb 100644 --- a/examples/echo/udp-echo/server/assembly/common/build.sh +++ b/examples/echo/udp-echo/server/assembly/common/build.sh @@ -15,7 +15,8 @@ PROJECT_HOME=`pwd` TARGET_FOLDER=${PROJECT_HOME}/target/${GOOS} TARGET_SBIN_NAME=${TARGET_EXEC_NAME} -version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +#version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +version="1.0" if [[ ${GOOS} == "windows" ]]; then TARGET_SBIN_NAME=${TARGET_SBIN_NAME}.exe fi @@ -24,12 +25,12 @@ if [[ $PROFILE == "dev" || $PROFILE == "test" ]]; then # GFLAGS=-gcflags "-N -l" -race -x -v # -x会把go build的详细过程输出 # GFLAGS=-gcflags "-N -l" -race -v # GFLAGS="-gcflags \"-N -l\" -v" - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -o ${TARGET_NAME} && cd - else # -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了,这个等价于普通C/C++程序被strip的效果), # -w去掉DWARF调试信息,得到的程序就不能用gdb调试了。-s和-w也可以分开使用,一般来说如果不打算用gdb调试, # -w基本没啥损失。-s的损失就有点大了。 - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -o ${TARGET_NAME} && cd - fi TAR_NAME=${TARGET_EXEC_NAME}-${version}-`date "+%Y%m%d-%H%M"`-${PROFILE} diff --git a/examples/echo/ws-echo/client/assembly/common/build.sh b/examples/echo/ws-echo/client/assembly/common/build.sh index 00763725..2652bafb 100644 --- a/examples/echo/ws-echo/client/assembly/common/build.sh +++ b/examples/echo/ws-echo/client/assembly/common/build.sh @@ -15,7 +15,8 @@ PROJECT_HOME=`pwd` TARGET_FOLDER=${PROJECT_HOME}/target/${GOOS} TARGET_SBIN_NAME=${TARGET_EXEC_NAME} -version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +#version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +version="1.0" if [[ ${GOOS} == "windows" ]]; then TARGET_SBIN_NAME=${TARGET_SBIN_NAME}.exe fi @@ -24,12 +25,12 @@ if [[ $PROFILE == "dev" || $PROFILE == "test" ]]; then # GFLAGS=-gcflags "-N -l" -race -x -v # -x会把go build的详细过程输出 # GFLAGS=-gcflags "-N -l" -race -v # GFLAGS="-gcflags \"-N -l\" -v" - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -o ${TARGET_NAME} && cd - else # -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了,这个等价于普通C/C++程序被strip的效果), # -w去掉DWARF调试信息,得到的程序就不能用gdb调试了。-s和-w也可以分开使用,一般来说如果不打算用gdb调试, # -w基本没啥损失。-s的损失就有点大了。 - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -o ${TARGET_NAME} && cd - fi TAR_NAME=${TARGET_EXEC_NAME}-${version}-`date "+%Y%m%d-%H%M"`-${PROFILE} diff --git a/examples/echo/ws-echo/server/assembly/common/build.sh b/examples/echo/ws-echo/server/assembly/common/build.sh index 00763725..2652bafb 100644 --- a/examples/echo/ws-echo/server/assembly/common/build.sh +++ b/examples/echo/ws-echo/server/assembly/common/build.sh @@ -15,7 +15,8 @@ PROJECT_HOME=`pwd` TARGET_FOLDER=${PROJECT_HOME}/target/${GOOS} TARGET_SBIN_NAME=${TARGET_EXEC_NAME} -version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +#version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +version="1.0" if [[ ${GOOS} == "windows" ]]; then TARGET_SBIN_NAME=${TARGET_SBIN_NAME}.exe fi @@ -24,12 +25,12 @@ if [[ $PROFILE == "dev" || $PROFILE == "test" ]]; then # GFLAGS=-gcflags "-N -l" -race -x -v # -x会把go build的详细过程输出 # GFLAGS=-gcflags "-N -l" -race -v # GFLAGS="-gcflags \"-N -l\" -v" - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -o ${TARGET_NAME} && cd - else # -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了,这个等价于普通C/C++程序被strip的效果), # -w去掉DWARF调试信息,得到的程序就不能用gdb调试了。-s和-w也可以分开使用,一般来说如果不打算用gdb调试, # -w基本没啥损失。-s的损失就有点大了。 - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -o ${TARGET_NAME} && cd - fi TAR_NAME=${TARGET_EXEC_NAME}-${version}-`date "+%Y%m%d-%H%M"`-${PROFILE} diff --git a/examples/echo/wss-echo/client/assembly/common/build.sh b/examples/echo/wss-echo/client/assembly/common/build.sh index 00763725..2652bafb 100644 --- a/examples/echo/wss-echo/client/assembly/common/build.sh +++ b/examples/echo/wss-echo/client/assembly/common/build.sh @@ -15,7 +15,8 @@ PROJECT_HOME=`pwd` TARGET_FOLDER=${PROJECT_HOME}/target/${GOOS} TARGET_SBIN_NAME=${TARGET_EXEC_NAME} -version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +#version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +version="1.0" if [[ ${GOOS} == "windows" ]]; then TARGET_SBIN_NAME=${TARGET_SBIN_NAME}.exe fi @@ -24,12 +25,12 @@ if [[ $PROFILE == "dev" || $PROFILE == "test" ]]; then # GFLAGS=-gcflags "-N -l" -race -x -v # -x会把go build的详细过程输出 # GFLAGS=-gcflags "-N -l" -race -v # GFLAGS="-gcflags \"-N -l\" -v" - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -o ${TARGET_NAME} && cd - else # -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了,这个等价于普通C/C++程序被strip的效果), # -w去掉DWARF调试信息,得到的程序就不能用gdb调试了。-s和-w也可以分开使用,一般来说如果不打算用gdb调试, # -w基本没啥损失。-s的损失就有点大了。 - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -o ${TARGET_NAME} && cd - fi TAR_NAME=${TARGET_EXEC_NAME}-${version}-`date "+%Y%m%d-%H%M"`-${PROFILE} diff --git a/examples/echo/wss-echo/server/assembly/common/build.sh b/examples/echo/wss-echo/server/assembly/common/build.sh index 00763725..2652bafb 100644 --- a/examples/echo/wss-echo/server/assembly/common/build.sh +++ b/examples/echo/wss-echo/server/assembly/common/build.sh @@ -15,7 +15,8 @@ PROJECT_HOME=`pwd` TARGET_FOLDER=${PROJECT_HOME}/target/${GOOS} TARGET_SBIN_NAME=${TARGET_EXEC_NAME} -version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +#version=`cat app/version.go | grep Version | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'` +version="1.0" if [[ ${GOOS} == "windows" ]]; then TARGET_SBIN_NAME=${TARGET_SBIN_NAME}.exe fi @@ -24,12 +25,12 @@ if [[ $PROFILE == "dev" || $PROFILE == "test" ]]; then # GFLAGS=-gcflags "-N -l" -race -x -v # -x会把go build的详细过程输出 # GFLAGS=-gcflags "-N -l" -race -v # GFLAGS="-gcflags \"-N -l\" -v" - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -gcflags "-N -l" -x -v -o ${TARGET_NAME} && cd - else # -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了,这个等价于普通C/C++程序被strip的效果), # -w去掉DWARF调试信息,得到的程序就不能用gdb调试了。-s和-w也可以分开使用,一般来说如果不打算用gdb调试, # -w基本没啥损失。-s的损失就有点大了。 - cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -i -o ${TARGET_NAME} && cd - + cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "-w" -x -v -o ${TARGET_NAME} && cd - fi TAR_NAME=${TARGET_EXEC_NAME}-${version}-`date "+%Y%m%d-%H%M"`-${PROFILE} diff --git a/go.mod b/go.mod index 1a595e13..c3ab61c4 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,12 @@ module github.com/AlexStocks/getty -go 1.14 +go 1.20 require ( github.com/AlexStocks/goext v0.3.2 - github.com/AlexStocks/log4go v1.0.7 // indirect github.com/dubbogo/gost v1.13.1 - github.com/fatih/camelcase v1.0.0 // indirect - github.com/fatih/structs v1.1.0 // indirect github.com/golang/snappy v0.0.1 github.com/gorilla/websocket v1.4.2 - github.com/juju/errors v0.0.0-20220331221717-b38fca44723b // indirect github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.7.0 @@ -18,3 +14,27 @@ require ( go.uber.org/zap v1.21.0 gopkg.in/yaml.v2 v2.4.0 ) + +require ( + github.com/AlexStocks/log4go v1.0.7 // indirect + github.com/BurntSushi/toml v0.3.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fatih/camelcase v1.0.0 // indirect + github.com/fatih/structs v1.1.0 // indirect + github.com/go-ole/go-ole v1.2.6 // indirect + github.com/juju/errors v0.0.0-20220331221717-b38fca44723b // indirect + github.com/k0kubun/pp v3.0.1+incompatible // indirect + github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect + github.com/mailru/easyjson v0.7.1 // indirect + github.com/mattn/go-colorable v0.0.9 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect + github.com/shirou/gopsutil/v3 v3.22.2 // indirect + github.com/tklauser/go-sysconf v0.3.10 // indirect + github.com/tklauser/numcpus v0.4.0 // indirect + github.com/yusufpapurcu/wmi v1.2.2 // indirect + go.uber.org/multierr v1.6.0 // indirect + golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect +) diff --git a/go.sum b/go.sum index 9a28a6e3..0b8c6d29 100644 --- a/go.sum +++ b/go.sum @@ -432,7 +432,6 @@ golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHl golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs= golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k= golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= @@ -442,7 +441,6 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -612,7 +610,6 @@ golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20201014170642-d1624618ad65/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -706,7 +703,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/transport/client.go b/transport/client.go index 4192d12a..71e78611 100644 --- a/transport/client.go +++ b/transport/client.go @@ -22,8 +22,8 @@ import ( "crypto/x509" "encoding/pem" "fmt" - "io/ioutil" "net" + "os" "strings" "sync" "sync/atomic" @@ -32,16 +32,11 @@ import ( import ( log "github.com/AlexStocks/getty/util" -) - -import ( - "github.com/dubbogo/gost/bytes" + gxbytes "github.com/dubbogo/gost/bytes" "github.com/dubbogo/gost/net" gxsync "github.com/dubbogo/gost/sync" gxtime "github.com/dubbogo/gost/time" - "github.com/gorilla/websocket" - perrors "github.com/pkg/errors" ) @@ -190,8 +185,8 @@ func (c *client) dialUDP() Session { buf []byte ) - bufp = gxbytes.GetBytes(128) - defer gxbytes.PutBytes(bufp) + bufp = gxbytes.AcquireBytes(128) + defer gxbytes.ReleaseBytes(bufp) buf = *bufp localAddr = &net.UDPAddr{IP: net.IPv4zero, Port: 0} peerAddr, _ = net.ResolveUDPAddr("udp", c.addr) @@ -285,9 +280,9 @@ func (c *client) dialWSS() Session { } if c.cert != "" { - certPEMBlock, err := ioutil.ReadFile(c.cert) + certPEMBlock, err := os.ReadFile(c.cert) if err != nil { - panic(fmt.Sprintf("ioutil.ReadFile(cert:%s) = error:%+v", c.cert, perrors.WithStack(err))) + panic(fmt.Sprintf("os.ReadFile(cert:%s) = error:%+v", c.cert, perrors.WithStack(err))) } var cert tls.Certificate diff --git a/transport/connection.go b/transport/connection.go index 6f46e886..b989f068 100644 --- a/transport/connection.go +++ b/transport/connection.go @@ -268,17 +268,14 @@ func (t *gettyTCPConn) recv(p []byte) (int, error) { // set read timeout deadline if t.compress == CompressNone && t.rTimeout.Load() > 0 { - // Optimization: update read deadline only if more than 25% - // of the last read deadline exceeded. - // See https://github.com/golang/go/issues/15133 for details. + // Set Deadline every time, since golang has fixed the performance issue + // See https://github.com/golang/go/issues/15133#issuecomment-271571395 for details currentTime = time.Now() - if currentTime.Sub(t.rLastDeadline.Load()) > t.rTimeout.Load()>>2 { - if err = t.conn.SetReadDeadline(currentTime.Add(t.rTimeout.Load())); err != nil { - // just a timeout error - return 0, perrors.WithStack(err) - } - t.rLastDeadline.Store(currentTime) + if err = t.conn.SetReadDeadline(currentTime.Add(t.rTimeout.Load())); err != nil { + // just a timeout error + return 0, perrors.WithStack(err) } + t.rLastDeadline.Store(currentTime) } length, err = t.reader.Read(p) @@ -298,16 +295,13 @@ func (t *gettyTCPConn) send(pkg interface{}) (int, error) { ) if t.compress == CompressNone && t.wTimeout.Load() > 0 { - // Optimization: update write deadline only if more than 25% - // of the last write deadline exceeded. - // See https://github.com/golang/go/issues/15133 for details. + // Set Deadline every time, since golang has fixed the performance issue + // See https://github.com/golang/go/issues/15133#issuecomment-271571395 for details currentTime = time.Now() - if currentTime.Sub(t.wLastDeadline.Load()) > t.wTimeout.Load()>>2 { - if err = t.conn.SetWriteDeadline(currentTime.Add(t.wTimeout.Load())); err != nil { - return 0, perrors.WithStack(err) - } - t.wLastDeadline.Store(currentTime) + if err = t.conn.SetWriteDeadline(currentTime.Add(t.wTimeout.Load())); err != nil { + return 0, perrors.WithStack(err) } + t.wLastDeadline.Store(currentTime) } if buffers, ok := pkg.([][]byte); ok { @@ -419,16 +413,13 @@ func (u *gettyUDPConn) SetCompressType(c CompressType) { // udp connection read func (u *gettyUDPConn) recv(p []byte) (int, *net.UDPAddr, error) { if u.rTimeout.Load() > 0 { - // Optimization: update read deadline only if more than 25% - // of the last read deadline exceeded. - // See https://github.com/golang/go/issues/15133 for details. + // Set Deadline every time, since golang has fixed the performance issue + // See https://github.com/golang/go/issues/15133#issuecomment-271571395 for details currentTime := time.Now() - if currentTime.Sub(u.rLastDeadline.Load()) > u.rTimeout.Load()>>2 { - if err := u.conn.SetReadDeadline(currentTime.Add(u.rTimeout.Load())); err != nil { - return 0, nil, perrors.WithStack(err) - } - u.rLastDeadline.Store(currentTime) + if err := u.conn.SetReadDeadline(currentTime.Add(u.rTimeout.Load())); err != nil { + return 0, nil, perrors.WithStack(err) } + u.rLastDeadline.Store(currentTime) } length, addr, err := u.conn.ReadFromUDP(p) // connected udp also can get return @addr @@ -466,16 +457,13 @@ func (u *gettyUDPConn) send(udpCtx interface{}) (int, error) { } if u.wTimeout.Load() > 0 { - // Optimization: update write deadline only if more than 25% - // of the last write deadline exceeded. - // See https://github.com/golang/go/issues/15133 for details. + // Set Deadline every time, since golang has fixed the performance issue + // See https://github.com/golang/go/issues/15133#issuecomment-271571395 for details currentTime = time.Now() - if currentTime.Sub(u.wLastDeadline.Load()) > u.wTimeout.Load()>>2 { - if err = u.conn.SetWriteDeadline(currentTime.Add(u.wTimeout.Load())); err != nil { - return 0, perrors.WithStack(err) - } - u.wLastDeadline.Store(currentTime) + if err = u.conn.SetWriteDeadline(currentTime.Add(u.wTimeout.Load())); err != nil { + return 0, perrors.WithStack(err) } + u.wLastDeadline.Store(currentTime) } if length, _, err = u.conn.WriteMsgUDP(buf, nil, peerAddr); err == nil { @@ -553,7 +541,12 @@ func (w *gettyWSConn) handlePing(message string) error { err := w.writePong([]byte(message)) if err == websocket.ErrCloseSent { err = nil - } else if e, ok := err.(net.Error); ok && e.Temporary() { + + // change the error checking from "e.Temporary()" to "e.Timeout()". + // as per https://github.com/golang/go/issues/45729, + // Timeout() correctly captures subset of Temporary() errors that could be retried. + // The rest of Temporary() errors should not be retried anyway (like syscall errors, out of file descriptors) + } else if e, ok := err.(net.Error); ok && e.Timeout() { err = nil } if err == nil { @@ -591,16 +584,13 @@ func (w *gettyWSConn) updateWriteDeadline() error { ) if w.wTimeout.Load() > 0 { - // Optimization: update write deadline only if more than 25% - // of the last write deadline exceeded. - // See https://github.com/golang/go/issues/15133 for details. + // Set Deadline every time, since golang has fixed the performance issue + // See https://github.com/golang/go/issues/15133#issuecomment-271571395 for details currentTime = time.Now() - if currentTime.Sub(w.wLastDeadline.Load()) > w.wTimeout.Load()>>2 { - if err = w.conn.SetWriteDeadline(currentTime.Add(w.wTimeout.Load())); err != nil { - return perrors.WithStack(err) - } - w.wLastDeadline.Store(currentTime) + if err = w.conn.SetWriteDeadline(currentTime.Add(w.wTimeout.Load())); err != nil { + return perrors.WithStack(err) } + w.wLastDeadline.Store(currentTime) } return nil diff --git a/transport/server.go b/transport/server.go index 6c9c641c..be4286c1 100644 --- a/transport/server.go +++ b/transport/server.go @@ -22,9 +22,9 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" "net" "net/http" + "os" "strings" "sync" "time" @@ -300,7 +300,11 @@ func (s *server) runTCPEventLoop(newSession NewSessionCallback) { } client, err = s.accept(newSession) if err != nil { - if netErr, ok := perrors.Cause(err).(net.Error); ok && netErr.Temporary() { + // change the error checking from "netErr.Temporary()" to "netErr.Timeout()". + // as per https://github.com/golang/go/issues/45729, + // Timeout() correctly captures subset of Temporary() errors that could be retried. + // The rest of Temporary() errors should not be retried anyway (like syscall errors, out of file descriptors) + if netErr, ok := perrors.Cause(err).(net.Error); ok && netErr.Timeout() { if delay == 0 { delay = 5 * time.Millisecond } else { @@ -453,9 +457,9 @@ func (s *server) runWSSEventLoop(newSession NewSessionCallback) { } if s.caCert != "" { - certPem, err = ioutil.ReadFile(s.caCert) + certPem, err = os.ReadFile(s.caCert) if err != nil { - panic(fmt.Errorf("ioutil.ReadFile(certFile{%s}) = err:%+v", s.caCert, perrors.WithStack(err))) + panic(fmt.Errorf("os.ReadFile(certFile{%s}) = err:%+v", s.caCert, perrors.WithStack(err))) } certPool = x509.NewCertPool() if ok := certPool.AppendCertsFromPEM(certPem); !ok { diff --git a/transport/session.go b/transport/session.go index ee971a47..8d585aa5 100644 --- a/transport/session.go +++ b/transport/session.go @@ -20,23 +20,21 @@ package getty import ( "bytes" "context" + "crypto/tls" "fmt" "io" "net" "runtime" "sync" "time" -) -import ( log "github.com/AlexStocks/getty/util" -) -import ( gxbytes "github.com/dubbogo/gost/bytes" + gxcontext "github.com/dubbogo/gost/context" - gxtime "github.com/dubbogo/gost/time" + gxtime "github.com/dubbogo/gost/time" "github.com/gorilla/websocket" perrors "github.com/pkg/errors" @@ -53,6 +51,8 @@ const ( MaxWheelTimeSpan = 900e9 maxPacketLen = 16 * 1024 + defaultTLSHandshakeTimeout = time.Second * 3 + defaultSessionName = "session" defaultTCPSessionName = "tcp-session" defaultUDPSessionName = "udp-session" @@ -566,6 +566,12 @@ func (s *session) run() { func (s *session) addTask(pkg interface{}) { f := func() { + // If the session is closed, there is no need to perform CPU-intensive operations. + if s.IsClosed() { + log.Errorf("[Id:%d, name=%s, endpoint=%s] Session is closed", s.ID(), s.name, s.EndPoint()) + return + } + s.listener.OnMessage(s, pkg) s.incReadPkgNum() } @@ -635,6 +641,18 @@ func (s *session) handleTCPPackage() error { pktBuf = gxbytes.NewBuffer(nil) conn = s.Connection.(*gettyTCPConn) + if tlsConn, ok := conn.conn.(*tls.Conn); ok { + tlsHandshaketime := defaultTLSHandshakeTimeout + if s.readTimeout() > 0 { + tlsHandshaketime = s.readTimeout() + } + ctx, cancel := context.WithTimeout(context.Background(), tlsHandshaketime) + defer cancel() + if err := tlsConn.HandshakeContext(ctx); err != nil { + log.Errorf("[tlsConn.HandshakeContext] = error:%+v", err) + return perrors.Wrap(err, "tlsConn.HandshakeContext") + } + } for { if s.IsClosed() { err = nil diff --git a/transport/tls.go b/transport/tls.go index d736ed90..4fec026a 100644 --- a/transport/tls.go +++ b/transport/tls.go @@ -21,7 +21,7 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" + "os" ) import ( @@ -66,9 +66,9 @@ func (s *ServerTlsConfigBuilder) BuildTlsConfig() (*tls.Config, error) { } if s.ServerTrustCertCollectionPath != "" { - certPem, err = ioutil.ReadFile(s.ServerTrustCertCollectionPath) + certPem, err = os.ReadFile(s.ServerTrustCertCollectionPath) if err != nil { - log.Error(fmt.Errorf("ioutil.ReadFile(certFile{%s}) = err:%+v", s.ServerTrustCertCollectionPath, perrors.WithStack(err))) + log.Error(fmt.Errorf("os.ReadFile(certFile{%s}) = err:%+v", s.ServerTrustCertCollectionPath, perrors.WithStack(err))) return nil, err } certPool = x509.NewCertPool() @@ -98,7 +98,7 @@ func (c *ClientTlsConfigBuilder) BuildTlsConfig() (*tls.Config, error) { log.Error(fmt.Sprintf("Unable to load X509 Key Pair %v", err)) return nil, err } - certBytes, err := ioutil.ReadFile(c.ClientTrustCertCollectionPath) + certBytes, err := os.ReadFile(c.ClientTrustCertCollectionPath) if err != nil { log.Error(fmt.Sprintf("Unable to read pem file: %s", c.ClientTrustCertCollectionPath)) return nil, err From 42af19198149a7d94f514bc50b2513a010c16ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xinfan=2Ewu=28=E5=90=B4=E6=AD=86=E5=B8=86=29?= Date: Wed, 29 May 2024 17:13:55 +0800 Subject: [PATCH 02/10] feat:update getty to dubbo-getty latest version --- .github/workflows/github-actions.yml | 2 +- transport/client.go | 8 ++-- transport/client_test.go | 14 +++--- transport/connection.go | 71 ++++++++++++++++------------ transport/session.go | 61 ++++++++++++------------ 5 files changed, 87 insertions(+), 69 deletions(-) diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index 74f46e5a..df698ec2 100644 --- a/.github/workflows/github-actions.yml +++ b/.github/workflows/github-actions.yml @@ -2,7 +2,7 @@ name: CI on: push: - branches: [ master ] + branches: [ feature/dubbo-getty-v1.4.10 ] pull_request: branches: "*" diff --git a/transport/client.go b/transport/client.go index 71e78611..ceed74fb 100644 --- a/transport/client.go +++ b/transport/client.go @@ -51,7 +51,8 @@ var ( sessionClientKey = "session-client-owner" connectPingPackage = []byte("connect-ping") - clientID = EndPointID(0) + clientID = EndPointID(0) + ignoreReconnectKey = "ignore-reconnect" ) type Client interface { @@ -185,8 +186,8 @@ func (c *client) dialUDP() Session { buf []byte ) - bufp = gxbytes.AcquireBytes(128) - defer gxbytes.ReleaseBytes(bufp) + bufp = gxbytes.GetBytes(128) + defer gxbytes.PutBytes(bufp) buf = *bufp localAddr = &net.UDPAddr{IP: net.IPv4zero, Port: 0} peerAddr, _ = net.ResolveUDPAddr("udp", c.addr) @@ -396,6 +397,7 @@ func (c *client) connect() { c.ssMap[ss] = struct{}{} c.Unlock() ss.SetAttribute(sessionClientKey, c) + ss.SetAttribute(ignoreReconnectKey, false) break } // don't distinguish between tcp connection and websocket connection. Because diff --git a/transport/client_test.go b/transport/client_test.go index aad3b8bd..50f4cda6 100644 --- a/transport/client_test.go +++ b/transport/client_test.go @@ -130,8 +130,8 @@ func TestTCPClient(t *testing.T) { assert.Equal(t, 1, msgHandler.SessionNumber()) ss := msgHandler.array[0] - ss.setSession(ss) - _, err = ss.send([]byte("hello")) + ss.SetSession(ss) + _, err = ss.Send([]byte("hello")) assert.Nil(t, err) active := ss.GetActive() assert.NotNil(t, active) @@ -198,7 +198,7 @@ func TestTCPClient(t *testing.T) { beforeWritePkgNum.Add(2) assert.Equal(t, beforeWriteBytes, conn.writeBytes) assert.Equal(t, beforeWritePkgNum, conn.writePkgNum) - assert.Equal(t, time.Duration(3000000000), ss.readTimeout()) + assert.Equal(t, time.Duration(3000000000), ss.ReadTimeout()) clt.Close() assert.True(t, clt.IsClosed()) } @@ -240,8 +240,8 @@ func TestUDPClient(t *testing.T) { assert.Equal(t, 1, msgHandler.SessionNumber()) ss := msgHandler.array[0] - ss.setSession(ss) - _, err = ss.send([]byte("hello")) + ss.SetSession(ss) + _, err = ss.Send([]byte("hello")) assert.NotNil(t, err) active := ss.GetActive() assert.NotNil(t, active) @@ -341,8 +341,8 @@ func TestNewWSClient(t *testing.T) { l, err := conn.send("hello") assert.NotNil(t, err) assert.True(t, l == 0) - ss.setSession(ss) - _, err = ss.send([]byte("hello")) + ss.SetSession(ss) + _, err = ss.Send([]byte("hello")) assert.Nil(t, err) active := ss.GetActive() assert.NotNil(t, active) diff --git a/transport/connection.go b/transport/connection.go index b989f068..b106b8a2 100644 --- a/transport/connection.go +++ b/transport/connection.go @@ -49,24 +49,28 @@ type Connection interface { SetCompressType(CompressType) LocalAddr() string RemoteAddr() string - incReadPkgNum() - incWritePkgNum() + // IncReadPkgNum increases connection's read pkg number + IncReadPkgNum() + // IncWritePkgNum increases connection's write pkg number + IncWritePkgNum() // UpdateActive update session's active time UpdateActive() // GetActive get session's active time GetActive() time.Time - readTimeout() time.Duration + // ReadTimeout gets deadline for the future read calls. + ReadTimeout() time.Duration // SetReadTimeout sets deadline for the future read calls. SetReadTimeout(time.Duration) - writeTimeout() time.Duration - // SetWriteTimeout sets deadline for the future read calls. + // WriteTimeout gets deadline for the future write calls. + WriteTimeout() time.Duration + // SetWriteTimeout sets deadline for the future write calls. SetWriteTimeout(time.Duration) - send(interface{}) (int, error) - // don't distinguish between tcp connection and websocket connection. Because - // gorilla/websocket/conn.go:(Conn)Close also invoke net.Conn.Close - close(int) - // set related session - setSession(Session) + // Send pkg data to peer + Send(interface{}) (int, error) + // CloseConn close connection + CloseConn(int) + // SetSession sets related session + SetSession(Session) } // /////////////////////////////////////// @@ -104,11 +108,11 @@ func (c *gettyConn) RemoteAddr() string { return c.peer } -func (c *gettyConn) incReadPkgNum() { +func (c *gettyConn) IncReadPkgNum() { c.readPkgNum.Add(1) } -func (c *gettyConn) incWritePkgNum() { +func (c *gettyConn) IncWritePkgNum() { c.writePkgNum.Add(1) } @@ -126,11 +130,11 @@ func (c *gettyConn) send(interface{}) (int, error) { func (c *gettyConn) close(int) {} -func (c gettyConn) readTimeout() time.Duration { +func (c gettyConn) ReadTimeout() time.Duration { return c.rTimeout.Load() } -func (c *gettyConn) setSession(ss Session) { +func (c *gettyConn) SetSession(ss Session) { c.ss = ss } @@ -149,7 +153,7 @@ func (c *gettyConn) SetReadTimeout(rTimeout time.Duration) { } } -func (c gettyConn) writeTimeout() time.Duration { +func (c gettyConn) WriteTimeout() time.Duration { return c.wTimeout.Load() } @@ -284,7 +288,7 @@ func (t *gettyTCPConn) recv(p []byte) (int, error) { } // tcp connection write -func (t *gettyTCPConn) send(pkg interface{}) (int, error) { +func (t *gettyTCPConn) Send(pkg interface{}) (int, error) { var ( err error currentTime time.Time @@ -331,7 +335,7 @@ func (t *gettyTCPConn) send(pkg interface{}) (int, error) { } // close tcp connection -func (t *gettyTCPConn) close(waitSec int) { +func (t *gettyTCPConn) CloseConn(waitSec int) { // if tcpConn, ok := t.conn.(*net.TCPConn); ok { // tcpConn.SetLinger(0) // } @@ -432,7 +436,7 @@ func (u *gettyUDPConn) recv(p []byte) (int, *net.UDPAddr, error) { } // write udp packet, @ctx should be of type UDPContext -func (u *gettyUDPConn) send(udpCtx interface{}) (int, error) { +func (u *gettyUDPConn) Send(udpCtx interface{}) (int, error) { var ( err error currentTime time.Time @@ -476,7 +480,7 @@ func (u *gettyUDPConn) send(udpCtx interface{}) (int, error) { } // close udp connection -func (u *gettyUDPConn) close(_ int) { +func (u *gettyUDPConn) CloseConn(_ int) { if u.conn != nil { u.conn.Close() u.conn = nil @@ -490,6 +494,7 @@ func (u *gettyUDPConn) close(_ int) { type gettyWSConn struct { gettyConn conn *websocket.Conn + lock sync.Mutex } // create websocket connection @@ -541,7 +546,6 @@ func (w *gettyWSConn) handlePing(message string) error { err := w.writePong([]byte(message)) if err == websocket.ErrCloseSent { err = nil - // change the error checking from "e.Temporary()" to "e.Timeout()". // as per https://github.com/golang/go/issues/45729, // Timeout() correctly captures subset of Temporary() errors that could be retried. @@ -570,7 +574,7 @@ func (w *gettyWSConn) recv() ([]byte, error) { w.readBytes.Add((uint32)(len(b))) } else { if websocket.IsUnexpectedCloseError(e, websocket.CloseGoingAway) { - log.Warnf("websocket unexpected close error: %v", e) + log.Warnf("websocket unexpected CloseConn error: %v", e) } } @@ -597,7 +601,7 @@ func (w *gettyWSConn) updateWriteDeadline() error { } // websocket connection write -func (w *gettyWSConn) send(pkg interface{}) (int, error) { +func (w *gettyWSConn) Send(pkg interface{}) (int, error) { var ( err error ok bool @@ -609,7 +613,7 @@ func (w *gettyWSConn) send(pkg interface{}) (int, error) { } w.updateWriteDeadline() - if err = w.conn.WriteMessage(websocket.BinaryMessage, p); err == nil { + if err = w.threadSafeWriteMessage(websocket.BinaryMessage, p); err == nil { w.writeBytes.Add((uint32)(len(p))) w.writePkgNum.Add(1) } @@ -618,19 +622,18 @@ func (w *gettyWSConn) send(pkg interface{}) (int, error) { func (w *gettyWSConn) writePing() error { w.updateWriteDeadline() - return perrors.WithStack(w.conn.WriteMessage(websocket.PingMessage, []byte{})) + return perrors.WithStack(w.threadSafeWriteMessage(websocket.PingMessage, []byte{})) } func (w *gettyWSConn) writePong(message []byte) error { w.updateWriteDeadline() - return perrors.WithStack(w.conn.WriteMessage(websocket.PongMessage, message)) + return perrors.WithStack(w.threadSafeWriteMessage(websocket.PongMessage, message)) } // close websocket connection -func (w *gettyWSConn) close(waitSec int) { +func (w *gettyWSConn) CloseConn(waitSec int) { w.updateWriteDeadline() - bytes := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "bye-bye!!!") - w.conn.WriteMessage(websocket.CloseMessage, bytes) + w.threadSafeWriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "bye-bye!!!")) conn := w.conn.UnderlyingConn() if tcpConn, ok := conn.(*net.TCPConn); ok { tcpConn.SetLinger(waitSec) @@ -639,3 +642,13 @@ func (w *gettyWSConn) close(waitSec int) { } w.conn.Close() } + +// uses a mutex to ensure that only one thread can send a message at a time, preventing race conditions. +func (w *gettyWSConn) threadSafeWriteMessage(messageType int, data []byte) error { + w.lock.Lock() + defer w.lock.Unlock() + if err := w.conn.WriteMessage(messageType, data); err != nil { + return err + } + return nil +} \ No newline at end of file diff --git a/transport/session.go b/transport/session.go index 8d585aa5..03128e4c 100644 --- a/transport/session.go +++ b/transport/session.go @@ -151,7 +151,7 @@ func newSession(endPoint EndPoint, conn Connection) *session { attrs: gxcontext.NewValuesContext(context.Background()), } - ss.Connection.setSession(ss) + ss.Connection.SetSession(ss) ss.SetWriteTimeout(netIOTimeout) ss.SetReadTimeout(netIOTimeout) @@ -369,7 +369,7 @@ func (s *session) sessionToken() string { s.name, s.EndPoint().EndPointType(), s.ID(), s.LocalAddr(), s.RemoteAddr()) } -func (s *session) WritePkg(pkg interface{}, timeout time.Duration) (int, int, error) { +func (s *session) WritePkg(pkg interface{}, timeout time.Duration) (pkgBytesLenth int, successCount int, err error) { if pkg == nil { return 0, 0, fmt.Errorf("@pkg is nil") } @@ -382,7 +382,8 @@ func (s *session) WritePkg(pkg interface{}, timeout time.Duration) (int, int, er const size = 64 << 10 rBuf := make([]byte, size) rBuf = rBuf[:runtime.Stack(rBuf, false)] - log.Errorf("[session.WritePkg] panic session %s: err=%s\n%s", s.sessionToken(), r, rBuf) + err = perrors.WithStack(fmt.Errorf("[session.WritePkg] panic session %s: err=%v\n%s", s.sessionToken(), r, rBuf)) + log.Error(err) } }() @@ -408,13 +409,12 @@ func (s *session) WritePkg(pkg interface{}, timeout time.Duration) (int, int, er if 0 < timeout { s.Connection.SetWriteTimeout(timeout) } - var succssCount int - succssCount, err = s.Connection.send(pkg) + successCount, err = s.Connection.Send(pkg) if err != nil { log.Warnf("%s, [session.WritePkg] @s.Connection.Write(pkg:%#v) = err:%+v", s.Stat(), pkg, err) - return len(pkgBytes), succssCount, perrors.WithStack(err) + return len(pkgBytes), successCount, perrors.WithStack(err) } - return len(pkgBytes), succssCount, nil + return len(pkgBytes), successCount, nil } // WriteBytes for codecs @@ -433,7 +433,7 @@ func (s *session) WriteBytes(pkg []byte) (int, error) { } for leftPackageSize > maxPacketLen { - _, err := s.Connection.send(pkg[writeSize:(writeSize + maxPacketLen)]) + _, err := s.Connection.Send(pkg[writeSize:(writeSize + maxPacketLen)]) if err != nil { return writeSize, perrors.Wrapf(err, "s.Connection.Write(pkg len:%d)", len(pkg)) } @@ -445,7 +445,7 @@ func (s *session) WriteBytes(pkg []byte) (int, error) { return writeSize, nil } - _, err := s.Connection.send(pkg[writeSize:]) + _, err := s.Connection.Send(pkg[writeSize:]) if err != nil { return writeSize, perrors.Wrapf(err, "s.Connection.Write(pkg len:%d)", len(pkg)) } @@ -466,7 +466,7 @@ func (s *session) WriteBytesArray(pkgs ...[]byte) (int, error) { if _, ok := s.Connection.(*gettyTCPConn); ok { s.packetLock.RLock() defer s.packetLock.RUnlock() - lg, err := s.Connection.send(pkgs) + lg, err := s.Connection.Send(pkgs) if err != nil { return 0, perrors.Wrapf(err, "s.Connection.Write(pkgs num:%d)", len(pkgs)) } @@ -505,7 +505,7 @@ func (s *session) WriteBytesArray(pkgs ...[]byte) (int, error) { num := len(pkgs) - 1 for i := 0; i < num; i++ { - s.incWritePkgNum() + s.IncWritePkgNum() } return wlg, nil @@ -573,7 +573,7 @@ func (s *session) addTask(pkg interface{}) { } s.listener.OnMessage(s, pkg) - s.incReadPkgNum() + s.IncReadPkgNum() } if taskPool := s.EndPoint().GetTaskPool(); taskPool != nil { taskPool.AddTaskAlways(f) @@ -643,8 +643,8 @@ func (s *session) handleTCPPackage() error { conn = s.Connection.(*gettyTCPConn) if tlsConn, ok := conn.conn.(*tls.Conn); ok { tlsHandshaketime := defaultTLSHandshakeTimeout - if s.readTimeout() > 0 { - tlsHandshaketime = s.readTimeout() + if s.ReadTimeout() > 0 { + tlsHandshaketime = s.ReadTimeout() } ctx, cancel := context.WithTimeout(context.Background(), tlsHandshaketime) defer cancel() @@ -673,6 +673,8 @@ func (s *session) handleTCPPackage() error { } if perrors.Cause(err) == io.EOF { log.Infof("%s, session.conn read EOF, client send over, session exit", s.sessionToken()) + //when read EOF, means that the peer has closed the connection, stop to reconnect to maintain the connection pool. + s.SetAttribute(ignoreReconnectKey, true) err = nil exit = true if bufLen != 0 { @@ -856,12 +858,13 @@ func (s *session) stop() { // let read/Write timeout asap now := time.Now() if conn := s.Conn(); conn != nil { - conn.SetReadDeadline(now.Add(s.readTimeout())) - conn.SetWriteDeadline(now.Add(s.writeTimeout())) + conn.SetReadDeadline(now.Add(s.ReadTimeout())) + conn.SetWriteDeadline(now.Add(s.WriteTimeout())) } close(s.done) - c := s.GetAttribute(sessionClientKey) - if clt, ok := c.(*client); ok { + clt, cltFound := s.GetAttribute(sessionClientKey).(*client) + ignoreReconnect, flagFound := s.GetAttribute(ignoreReconnectKey).(bool) + if cltFound && flagFound && !ignoreReconnect { clt.reConnect() } }) @@ -881,7 +884,7 @@ func (s *session) gc() { go func() { if conn != nil { - conn.close(int(s.wait)) + conn.CloseConn(int(s.wait)) } }() } @@ -955,59 +958,59 @@ func (s *session) RemoteAddr() string { return "" } -func (s *session) incReadPkgNum() { +func (s *session) IncReadPkgNum() { if s == nil { return } s.lock.RLock() defer s.lock.RUnlock() if s.Connection != nil { - s.Connection.incReadPkgNum() + s.Connection.IncReadPkgNum() } } -func (s *session) incWritePkgNum() { +func (s *session) IncWritePkgNum() { if s == nil { return } s.lock.RLock() defer s.lock.RUnlock() if s.Connection != nil { - s.Connection.incWritePkgNum() + s.Connection.IncWritePkgNum() } } -func (s *session) send(pkg interface{}) (int, error) { +func (s *session) Send(pkg interface{}) (int, error) { if s == nil { return 0, nil } s.lock.RLock() defer s.lock.RUnlock() if s.Connection != nil { - return s.Connection.send(pkg) + return s.Connection.Send(pkg) } return 0, nil } -func (s *session) readTimeout() time.Duration { +func (s *session) ReadTimeout() time.Duration { if s == nil { return time.Duration(0) } s.lock.RLock() defer s.lock.RUnlock() if s.Connection != nil { - return s.Connection.readTimeout() + return s.Connection.ReadTimeout() } return time.Duration(0) } -func (s *session) setSession(ss Session) { +func (s *session) SetSession(ss Session) { if s == nil { return } s.lock.RLock() if s.Connection != nil { - s.Connection.setSession(ss) + s.Connection.SetSession(ss) } s.lock.RUnlock() } From 23efee8fc99be14e78d3614b25772894a1a4aa49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xinfan=2Ewu=28=E5=90=B4=E6=AD=86=E5=B8=86=29?= Date: Wed, 29 May 2024 17:26:18 +0800 Subject: [PATCH 03/10] fix ci error --- .github/workflows/github-actions.yml | 2 +- transport/connection.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index df698ec2..547dfd4b 100644 --- a/.github/workflows/github-actions.yml +++ b/.github/workflows/github-actions.yml @@ -4,7 +4,7 @@ on: push: branches: [ feature/dubbo-getty-v1.4.10 ] pull_request: - branches: "*" + branches: "master" jobs: diff --git a/transport/connection.go b/transport/connection.go index b106b8a2..930859ff 100644 --- a/transport/connection.go +++ b/transport/connection.go @@ -651,4 +651,4 @@ func (w *gettyWSConn) threadSafeWriteMessage(messageType int, data []byte) error return err } return nil -} \ No newline at end of file +} From 154ad97db4bf687fc0ba8e0d7db6f1904f19bcb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xinfan=2Ewu=28=E5=90=B4=E6=AD=86=E5=B8=86=29?= Date: Wed, 29 May 2024 20:40:54 +0800 Subject: [PATCH 04/10] feat:update to dubbo-getty-v1.4.12 --- transport/client.go | 18 ++++++++++++------ transport/client_test.go | 12 ++++++------ transport/session.go | 1 - 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/transport/client.go b/transport/client.go index ceed74fb..4b17f947 100644 --- a/transport/client.go +++ b/transport/client.go @@ -422,8 +422,10 @@ func (c *client) RunEventLoop(newSession NewSessionCallback) { // a for-loop connect to make sure the connection pool is valid func (c *client) reConnect() { - var num, max, times, interval int - + var ( + num, max, times, interval int + maxDuration int64 + ) max = c.number interval = c.reconnectInterval if interval == 0 { @@ -436,15 +438,18 @@ func (c *client) reConnect() { } num = c.sessionNum() - if max <= num { + if max <= num || max < times { + //Exit when the number of connection pools is sufficient or the reconnection times exceeds the connections numbers. break } c.connect() times++ - if maxTimes < times { - times = maxTimes + if times > maxTimes { + maxDuration = int64(maxTimes) * int64(interval) + } else { + maxDuration = int64(times) * int64(interval) } - <-gxtime.After(time.Duration(int64(times) * int64(interval))) + <-gxtime.After(time.Duration(maxDuration)) } } @@ -458,6 +463,7 @@ func (c *client) stop() { c.Lock() for s := range c.ssMap { s.RemoveAttribute(sessionClientKey) + s.RemoveAttribute(ignoreReconnectKey) s.Close() } c.ssMap = nil diff --git a/transport/client_test.go b/transport/client_test.go index 50f4cda6..05af4ad7 100644 --- a/transport/client_test.go +++ b/transport/client_test.go @@ -140,7 +140,7 @@ func TestTCPClient(t *testing.T) { assert.True(t, conn.compress == CompressNone) beforeWriteBytes := conn.writeBytes beforeWritePkgNum := conn.writePkgNum - l, err := conn.send([]byte("hello")) + l, err := conn.Send([]byte("hello")) assert.Nil(t, err) assert.True(t, l == 5) beforeWritePkgNum.Add(1) @@ -156,7 +156,7 @@ func TestTCPClient(t *testing.T) { assert.Equal(t, beforeWritePkgNum, conn.writePkgNum) var pkgs [][]byte pkgs = append(pkgs, []byte("hello"), []byte("hello")) - l, err = conn.send(pkgs) + l, err = conn.Send(pkgs) assert.Nil(t, err) assert.True(t, l == 10) beforeWritePkgNum.Add(2) @@ -275,11 +275,11 @@ func TestUDPClient(t *testing.T) { } t.Logf("udp context:%s", udpCtx) udpConn := ss.(*session).Connection.(*gettyUDPConn) - _, err = udpConn.send(udpCtx) + _, err = udpConn.Send(udpCtx) assert.NotNil(t, err) udpCtx.Pkg = []byte("hello") beforeWriteBytes := udpConn.writeBytes - _, err = udpConn.send(udpCtx) + _, err = udpConn.Send(udpCtx) beforeWriteBytes.Add(5) assert.Equal(t, beforeWriteBytes, udpConn.writeBytes) assert.Nil(t, err) @@ -338,7 +338,7 @@ func TestNewWSClient(t *testing.T) { assert.True(t, conn.compress == CompressNone) err := conn.handlePing("hello") assert.Nil(t, err) - l, err := conn.send("hello") + l, err := conn.Send("hello") assert.NotNil(t, err) assert.True(t, l == 0) ss.SetSession(ss) @@ -347,7 +347,7 @@ func TestNewWSClient(t *testing.T) { active := ss.GetActive() assert.NotNil(t, active) beforeWriteBytes := conn.writeBytes - _, err = conn.send([]byte("hello")) + _, err = conn.Send([]byte("hello")) assert.Nil(t, err) beforeWriteBytes.Add(5) assert.Equal(t, beforeWriteBytes, conn.writeBytes) diff --git a/transport/session.go b/transport/session.go index 03128e4c..4173dd71 100644 --- a/transport/session.go +++ b/transport/session.go @@ -571,7 +571,6 @@ func (s *session) addTask(pkg interface{}) { log.Errorf("[Id:%d, name=%s, endpoint=%s] Session is closed", s.ID(), s.name, s.EndPoint()) return } - s.listener.OnMessage(s, pkg) s.IncReadPkgNum() } From 96be0e534253883dd78ecfac2c06f069495038af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xinfan=2Ewu=28=E5=90=B4=E6=AD=86=E5=B8=86=29?= Date: Wed, 29 May 2024 22:29:07 +0800 Subject: [PATCH 05/10] feat:change action yaml --- .github/workflows/github-actions.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index 547dfd4b..74f46e5a 100644 --- a/.github/workflows/github-actions.yml +++ b/.github/workflows/github-actions.yml @@ -2,9 +2,9 @@ name: CI on: push: - branches: [ feature/dubbo-getty-v1.4.10 ] + branches: [ master ] pull_request: - branches: "master" + branches: "*" jobs: From f5015fe4e1f5d52e78abb6026cbc03cf11cd72b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xinfan=2Ewu=28=E5=90=B4=E6=AD=86=E5=B8=86=29?= Date: Wed, 29 May 2024 23:28:53 +0800 Subject: [PATCH 06/10] feat:change client_test.go --- transport/client_test.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/transport/client_test.go b/transport/client_test.go index 05af4ad7..5cadf02b 100644 --- a/transport/client_test.go +++ b/transport/client_test.go @@ -26,9 +26,7 @@ import ( "sync" "testing" "time" -) -import ( "github.com/gorilla/websocket" "github.com/stretchr/testify/assert" ) @@ -130,11 +128,11 @@ func TestTCPClient(t *testing.T) { assert.Equal(t, 1, msgHandler.SessionNumber()) ss := msgHandler.array[0] - ss.SetSession(ss) - _, err = ss.Send([]byte("hello")) - assert.Nil(t, err) - active := ss.GetActive() - assert.NotNil(t, active) + // ss.SetSession(ss) + // _, err = ss.Send([]byte("hello")) + // assert.Nil(t, err) + // active := ss.GetActive() + // assert.NotNil(t, active) ss.SetCompressType(CompressNone) conn := ss.(*session).Connection.(*gettyTCPConn) assert.True(t, conn.compress == CompressNone) @@ -240,11 +238,11 @@ func TestUDPClient(t *testing.T) { assert.Equal(t, 1, msgHandler.SessionNumber()) ss := msgHandler.array[0] - ss.SetSession(ss) - _, err = ss.Send([]byte("hello")) - assert.NotNil(t, err) - active := ss.GetActive() - assert.NotNil(t, active) + // ss.SetSession(ss) + // _, err = ss.Send([]byte("hello")) + // assert.NotNil(t, err) + // active := ss.GetActive() + // assert.NotNil(t, active) totalLen, sendLen, err = ss.WritePkg(nil, 0) assert.NotNil(t, err) assert.True(t, sendLen == 0) @@ -341,11 +339,11 @@ func TestNewWSClient(t *testing.T) { l, err := conn.Send("hello") assert.NotNil(t, err) assert.True(t, l == 0) - ss.SetSession(ss) - _, err = ss.Send([]byte("hello")) - assert.Nil(t, err) - active := ss.GetActive() - assert.NotNil(t, active) + // ss.SetSession(ss) + // _, err = ss.Send([]byte("hello")) + // assert.Nil(t, err) + // active := ss.GetActive() + // assert.NotNil(t, active) beforeWriteBytes := conn.writeBytes _, err = conn.Send([]byte("hello")) assert.Nil(t, err) From 9e4548a3513c5a960608aa83f5e56eb9d9dd27de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xinfan=2Ewu=28=E5=90=B4=E6=AD=86=E5=B8=86=29?= Date: Wed, 29 May 2024 23:57:32 +0800 Subject: [PATCH 07/10] feat:change ci file --- .github/workflows/github-actions.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index 74f46e5a..fc1d6e6b 100644 --- a/.github/workflows/github-actions.yml +++ b/.github/workflows/github-actions.yml @@ -15,7 +15,7 @@ jobs: # If you want to matrix build , you can append the following list. matrix: go_version: - - "1.20" + - '1.20' os: - ubuntu-latest From 0401b45756553b1551511d5eca4de00b578bc7a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xinfan=2Ewu=28=E5=90=B4=E6=AD=86=E5=B8=86=29?= Date: Thu, 30 May 2024 14:27:43 +0800 Subject: [PATCH 08/10] fix:update test file --- transport/client_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/transport/client_test.go b/transport/client_test.go index 5cadf02b..30462581 100644 --- a/transport/client_test.go +++ b/transport/client_test.go @@ -162,7 +162,9 @@ func TestTCPClient(t *testing.T) { assert.Equal(t, beforeWritePkgNum, conn.writePkgNum) assert.Equal(t, beforeWriteBytes, conn.writeBytes) ss.SetCompressType(CompressSnappy) - l, err = ss.WriteBytesArray(pkgs...) + var anotherPkgs [][]byte + anotherPkgs = append(anotherPkgs, []byte("hello"), []byte("hello")) + l, err = ss.WriteBytesArray(anotherPkgs...) assert.Nil(t, err) assert.True(t, l == 10) beforeWritePkgNum.Add(2) From 7e43e54c000936668fde2f2cdfbfe9e6eb34bdc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xinfan=2Ewu=28=E5=90=B4=E6=AD=86=E5=B8=86=29?= Date: Thu, 30 May 2024 15:09:22 +0800 Subject: [PATCH 09/10] fix:remove annotation in client_test.go --- transport/client_test.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/transport/client_test.go b/transport/client_test.go index 30462581..9f5aa87a 100644 --- a/transport/client_test.go +++ b/transport/client_test.go @@ -26,7 +26,9 @@ import ( "sync" "testing" "time" +) +import ( "github.com/gorilla/websocket" "github.com/stretchr/testify/assert" ) @@ -128,11 +130,11 @@ func TestTCPClient(t *testing.T) { assert.Equal(t, 1, msgHandler.SessionNumber()) ss := msgHandler.array[0] - // ss.SetSession(ss) - // _, err = ss.Send([]byte("hello")) - // assert.Nil(t, err) - // active := ss.GetActive() - // assert.NotNil(t, active) + ss.SetSession(ss) + _, err = ss.Send([]byte("hello")) + assert.Nil(t, err) + active := ss.GetActive() + assert.NotNil(t, active) ss.SetCompressType(CompressNone) conn := ss.(*session).Connection.(*gettyTCPConn) assert.True(t, conn.compress == CompressNone) @@ -240,11 +242,11 @@ func TestUDPClient(t *testing.T) { assert.Equal(t, 1, msgHandler.SessionNumber()) ss := msgHandler.array[0] - // ss.SetSession(ss) - // _, err = ss.Send([]byte("hello")) - // assert.NotNil(t, err) - // active := ss.GetActive() - // assert.NotNil(t, active) + ss.SetSession(ss) + _, err = ss.Send([]byte("hello")) + assert.NotNil(t, err) + active := ss.GetActive() + assert.NotNil(t, active) totalLen, sendLen, err = ss.WritePkg(nil, 0) assert.NotNil(t, err) assert.True(t, sendLen == 0) From 0e51a1ed27dbc5ad0907fb1a556b008e55e47815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xinfan=2Ewu=28=E5=90=B4=E6=AD=86=E5=B8=86=29?= Date: Thu, 30 May 2024 15:32:43 +0800 Subject: [PATCH 10/10] refactor:format import in session.go --- transport/session.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/transport/session.go b/transport/session.go index 4173dd71..b760b7c8 100644 --- a/transport/session.go +++ b/transport/session.go @@ -27,14 +27,17 @@ import ( "runtime" "sync" "time" +) +import ( log "github.com/AlexStocks/getty/util" +) +import ( gxbytes "github.com/dubbogo/gost/bytes" - gxcontext "github.com/dubbogo/gost/context" - gxtime "github.com/dubbogo/gost/time" + "github.com/gorilla/websocket" perrors "github.com/pkg/errors"