diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 701b3c2..e4871eb 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -1,46 +1,36 @@ name: Pull Request Check -on: [pull_request] +on: [ pull_request ] jobs: compliant: - runs-on: self-hosted + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Check License Header - uses: apache/skywalking-eyes@header@v0.4.0 + uses: apache/skywalking-eyes/header@v0.4.0 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: typos-action + - name: Check Spell uses: crate-ci/typos@master - staticcheck: - runs-on: self-hosted + golangci-lint: + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - + - uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version: 1.16 - - - uses: actions/cache@v3 + uses: actions/setup-go@v5 with: - path: ~/go/pkg/mod - key: reviewdog-${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - reviewdog-${{ runner.os }}-go- - - - uses: reviewdog/action-staticcheck@v1 + go-version: stable + # for self-hosted, the cache path is shared across projects + # and it works well without the cache of github actions + # Enable it if we're going to use Github only + cache: true + + - name: Golangci Lint + # https://golangci-lint.run/ + uses: golangci/golangci-lint-action@v6 with: - github_token: ${{ secrets.github_token }} - # Change reviewdog reporter if you need [github-pr-check,github-check,github-pr-review]. - reporter: github-pr-review - # Report all results. - filter_mode: nofilter - # Exit with 1 when it find at least one finding. - fail_on_error: true - # Set staticcheck flags - staticcheck_flags: -checks=inherit,-SA1029,-SA6002 + version: latest diff --git a/.github/workflows/release-check.yml b/.github/workflows/release-check.yml deleted file mode 100644 index fb6cfcd..0000000 --- a/.github/workflows/release-check.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: Release Check - -on: - pull_request: - branches: - - main - -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Check Source Branch - run: python2 -c "exit(0 if '${{ github.head_ref }}'.startswith('release') or '${{ github.head_ref }}'.startswith('hotfix') else 1)" - - - name: Check Version - run: | - # get version code, runner not support grep -E here - SOURCE_VERSION=`grep 'Version\s*=\s*\"v[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\"' *.go | awk -F '\"' '{print $(NF-1)}'` - git checkout main - MASTER_VERSION=`grep 'Version\s*=\s*\"v[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\"' *.go | awk -F '\"' '{print $(NF-1)}'` - git checkout ${{Head.SHA}} - # check version update - python2 -c "exit(0 if list(map(int,'${SOURCE_VERSION#v}'.split('.')[:3])) > list(map(int,'${MASTER_VERSION#v}'.split('.')[:3])) else 1)" diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1d38c83..eedff89 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,30 +1,31 @@ name: Tests -on: [push, pull_request] +on: [ push, pull_request ] jobs: - lint-and-ut: - runs-on: self-hosted + benchmark: + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - + - uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version: 1.18 - - - uses: actions/cache@v3 + uses: actions/setup-go@v5 with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- + go-version: stable - - name: Lint - run: | - go vet -stdmethods=false $(go list ./...) - go install mvdan.cc/gofumpt@v0.2.0 - test -z "$(gofumpt -l -extra .)" + - name: Benchmark + run: go test -bench=. -benchmem ./... + uinttest: + strategy: + matrix: + go: [ "1.18", "1.19", "1.20", "1.21", "1.22", "1.23" ] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go }} + cache: true # set false for self-hosted - name: Unit Test - run: go test -race -covermode=atomic -coverprofile=coverage.out ./... + run: go test -race ./... diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..af8f11b --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,10 @@ +linters: # https://golangci-lint.run/usage/linters/ + disable-all: true + enable: + # - errcheck # can not skip _test.go ? + - gosimple + - govet + - ineffassign + - staticcheck + - unused + - unconvert diff --git a/client.go b/client.go index c26ec71..a635ce4 100644 --- a/client.go +++ b/client.go @@ -1,3 +1,19 @@ +/* + * Copyright 2024 CloudWeGo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package websocket import ( @@ -86,5 +102,6 @@ func (p *ClientUpgrader) UpgradeResponse(req *protocol.Request, resp *protocol.R conn.newCompressionWriter = compressNoContextTakeover conn.newDecompressionReader = decompressNoContextTakeover } + conn.resp = resp return conn, nil } diff --git a/client_test.go b/client_test.go index 26f5f93..85b4adb 100644 --- a/client_test.go +++ b/client_test.go @@ -1,3 +1,19 @@ +/* + * Copyright 2024 CloudWeGo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package websocket import ( diff --git a/conn.go b/conn.go index 2eb8874..3805bd0 100644 --- a/conn.go +++ b/conn.go @@ -282,6 +282,10 @@ type Conn struct { readDecompress bool // whether last read frame had RSV1 set newDecompressionReader func(io.Reader) io.ReadCloser + + // keep reference to the resp to make sure the underyling conn will not be closed. + // see: https://github.com/cloudwego/hertz/pull/1214 for the details. + resp interface{} // *protocol.Response } func newConn(conn net.Conn, isServer bool, readBufferSize, writeBufferSize int, writeBufferPool BufferPool, br *bufio.Reader, writeBuf []byte) *Conn { diff --git a/conn_test.go b/conn_test.go index 4961797..90b7fdb 100644 --- a/conn_test.go +++ b/conn_test.go @@ -86,7 +86,7 @@ func TestFraming(t *testing.T) { return w.Write(writeBuf[:n]) }}, {"string", func(w io.Writer, n int) (int, error) { - return io.WriteString(w, string(writeBuf[:n])) + return io.WriteString(w, string(writeBuf[:n])) // nolint: staticcheck }}, } diff --git a/server.go b/server.go index e8a049b..6dbfbe4 100644 --- a/server.go +++ b/server.go @@ -211,7 +211,9 @@ func (u *HertzUpgrader) Upgrade(ctx *app.RequestContext, handler HertzHandler) e handler(c) writeBuf = writeBuf[0:0] - poolWriteBuffer.Put(writeBuf) + + // FIXME: argument should be pointer-like to avoid allocations (staticcheck) + poolWriteBuffer.Put(writeBuf) // nolint: staticcheck }) return nil