From 218adc40bfbe4687cfeeda2308a807848a48ed66 Mon Sep 17 00:00:00 2001 From: Saimon Shaplygin Date: Fri, 13 May 2022 16:12:23 +0300 Subject: [PATCH] add: golangci lint --- .github/workflow/lint.yml | 28 ++++++++++++++++ .github/workflow/test.yml | 65 +++++++++++++++++++++++++++++++++++++ .golangci.yml | 68 +++++++++++++++++++++++++++++++++++++++ adapter.go | 18 ++++++++--- broadcast.go | 6 ++-- helpers.go | 2 +- interface.go | 6 ++-- logger.go | 9 ------ options.go | 16 +++++---- types.go | 20 ++++++------ 10 files changed, 200 insertions(+), 38 deletions(-) create mode 100644 .github/workflow/lint.yml create mode 100644 .github/workflow/test.yml create mode 100644 .golangci.yml delete mode 100644 logger.go diff --git a/.github/workflow/lint.yml b/.github/workflow/lint.yml new file mode 100644 index 0000000..e3839cf --- /dev/null +++ b/.github/workflow/lint.yml @@ -0,0 +1,28 @@ +name: Lint +on: + push: + branches: + - master + - release/** + pull_request: +permissions: + contents: read +defaults: + run: + shell: bash +concurrency: + group: lint-${{ github.ref }} + cancel-in-progress: true +jobs: + golangci-lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/setup-go@v3 + - uses: actions/checkout@v3 + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.45 + only-new-issues: true + timeout-minutes: 5 \ No newline at end of file diff --git a/.github/workflow/test.yml b/.github/workflow/test.yml new file mode 100644 index 0000000..9cc33bc --- /dev/null +++ b/.github/workflow/test.yml @@ -0,0 +1,65 @@ +name: Test +on: + push: + branches: + - master + - release/** + pull_request: +permissions: + contents: read +defaults: + run: + shell: bash +concurrency: + group: test-${{ github.ref }} + cancel-in-progress: true +jobs: + test: + name: Unit + runs-on: ${{ matrix.os }}-latest + strategy: + matrix: + go: [ "1.18", "1.17", "1.16", "1.15", "1.14", "1.13" ] + os: [ ubuntu, windows, macos ] + fail-fast: false + env: + GO111MODULE: "on" + GOFLAGS: "-mod=readonly" + steps: + - uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go }} + - uses: actions/checkout@v3 + - uses: actions/cache@v2 + with: + # In order: + # * Module download cache + # * Build cache (Linux) + # * Build cache (Mac) + # * Build cache (Windows) + path: | + ~/go/pkg/mod + ~/.cache/go-build + ~/Library/Caches/go-build + %LocalAppData%\go-build + key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go-${{ matrix.go }}- + ${{ runner.os }}-go- + - name: Build + run: go build ./... + - name: Vet + run: go vet ./... + - name: Check go.mod Tidiness + run: go mod tidy -go=1.18 -compat=1.16 && git diff --exit-code + if: ${{ matrix.go == '1.18' }} + - name: Test + run: go test -count=1 ./... + - name: Test (race) + run: go test -count=1 -race ./... + # The race detector adds considerable runtime overhead. To save time on + # pull requests, only run this step for a single job in the matrix. For + # all other workflow triggers (e.g., pushes to a release branch) run + # this step for the whole matrix. + if: ${{ github.event_name != 'pull_request' || (matrix.go == '1.18' && matrix.os == 'ubuntu') }} + timeout-minutes: 5 diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..710f2b5 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,68 @@ +# More info on config here: https://github.com/golangci/golangci-lint#config-file +run: + deadline: 5m + issues-exit-code: 1 + tests: true + modules-download-mode: mod + skip-dirs-use-default: true + skip-dirs: + - bin + - vendor + - tests + +output: + format: colored-line-number + print-issued-lines: true + print-linter-name: true + +linters-settings: + govet: + check-shadowing: true + golint: + min-confidence: 0 + dupl: + threshold: 100 + goconst: + min-len: 2 + min-occurrences: 2 + + +linters: + disable-all: true + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - dupl + - errcheck + - exportloopref + - gochecknoinits + - goconst + - gocritic + - gocyclo + - godot + - gofmt + - goimports + - gosec + - gosimple + - govet + - ineffassign + - misspell + - nakedret + - prealloc + - staticcheck + - structcheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace + +issues: + exclude-use-default: false + exclude: + # ST1003: method parameter userId should be userID (stylecheck) + - ST1003 + - ST1000 \ No newline at end of file diff --git a/adapter.go b/adapter.go index 141209a..d793253 100644 --- a/adapter.go +++ b/adapter.go @@ -1,4 +1,4 @@ -package go_socket_io_redis_adapter +package go_socketio_redis_adapter import ( "fmt" @@ -16,7 +16,7 @@ type Adapter struct { // NewAdapter create new redis adapter. This method used for client API in main function. func NewAdapter(opts ...OptionsFunc) (*Adapter, error) { a := &Adapter{ - opts: NewOptions(), + opts: newOptions(), } for _, o := range opts { @@ -31,7 +31,7 @@ func NewAdapter(opts ...OptionsFunc) (*Adapter, error) { a.subConn = &redis.PubSubConn{Conn: conn} a.pubConn = &redis.PubSubConn{Conn: conn} - return a, conn.Close() + return a, nil } // NewBroadcast create broadcast for inner server API. @@ -65,6 +65,14 @@ func (a *Adapter) NewBroadcast(nsp string) (*redisBroadcast, error) { return rbc, nil } -func (a *Adapter) GetName() string { - return "redis-adapter" +func (a *Adapter) Close() error { + if err := a.subConn.Close(); err != nil { + return err + } + + if err := a.pubConn.Close(); err != nil { + return nil + } + + return nil } diff --git a/broadcast.go b/broadcast.go index c9fc0ad..3c9c099 100644 --- a/broadcast.go +++ b/broadcast.go @@ -1,4 +1,4 @@ -package go_socket_io_redis_adapter +package go_socketio_redis_adapter import ( "encoding/json" @@ -10,7 +10,7 @@ import ( ) // redisBroadcast gives Join, Leave & BroadcastTO server API support to socket.io along with room management -// map of rooms where each room contains a map of connection id to connections in that room +// map of rooms where each room contains a map of connection id to connections in that room. type redisBroadcast struct { pub *redis.PubSubConn sub *redis.PubSubConn @@ -79,7 +79,7 @@ func (bc *redisBroadcast) Join(room string, connection Conn) { bc.rooms[room][connection.ID()] = connection } -// Leave leaves the given connection from given room (if exist) +// Leave leaves the given connection from given room (if exist). func (bc *redisBroadcast) Leave(room string, connection Conn) { bc.lock.Lock() defer bc.lock.Unlock() diff --git a/helpers.go b/helpers.go index 45d9a3c..4c50557 100644 --- a/helpers.go +++ b/helpers.go @@ -1,4 +1,4 @@ -package go_socket_io_redis_adapter +package go_socketio_redis_adapter import "github.com/gofrs/uuid" diff --git a/interface.go b/interface.go index 0b66dd5..c230e73 100644 --- a/interface.go +++ b/interface.go @@ -1,4 +1,4 @@ -package go_socket_io_redis_adapter +package go_socketio_redis_adapter import ( "io" @@ -7,10 +7,10 @@ import ( "net/url" ) -// EachFunc typed for each callback function +// EachFunc typed for each callback function. type EachFunc func(Conn) -// Conn is a connection in go-socket.io +// Conn is a connection in go-socket.io. type Conn interface { io.Closer Namespace diff --git a/logger.go b/logger.go deleted file mode 100644 index 456beb2..0000000 --- a/logger.go +++ /dev/null @@ -1,9 +0,0 @@ -package go_socket_io_redis_adapter - -// Logger logs messages with different levels. -type Logger interface { - Errorf(format string, v ...interface{}) - Warningf(format string, v ...interface{}) - Infof(format string, v ...interface{}) - Debugf(format string, v ...interface{}) -} diff --git a/options.go b/options.go index 44483f5..68395d4 100644 --- a/options.go +++ b/options.go @@ -1,13 +1,14 @@ -package go_socket_io_redis_adapter +package go_socketio_redis_adapter -// Options is configuration to create new adapter +// Options is configuration to create new adapter. type Options struct { Addr string Prefix string - Network string + Network string //notice: redis supported tcp|unix network } -func NewOptions() *Options { +// newOptions create default options. +func newOptions() *Options { return &Options{ Addr: "127.0.0.1:6379", Prefix: "socket.io", @@ -15,23 +16,24 @@ func NewOptions() *Options { } } +// OptionsFunc as option interface. type OptionsFunc func(o *Options) -// WithAddrOptions +// WithAddrOptions set custom connect addr. func WithAddrOptions(addr string) OptionsFunc { return func(o *Options) { o.Addr = addr } } -// WithPrefixOptions +// WithPrefixOptions set custom prefix for redis key. func WithPrefixOptions(prefix string) OptionsFunc { return func(o *Options) { o.Prefix = prefix } } -// WithNetworkOptions +// WithNetworkOptions set custom connection network type. func WithNetworkOptions(network string) OptionsFunc { return func(o *Options) { o.Network = network diff --git a/types.go b/types.go index 990e7f0..2006efa 100644 --- a/types.go +++ b/types.go @@ -1,15 +1,15 @@ -package go_socket_io_redis_adapter +package go_socketio_redis_adapter import "sync" -// request types +// request types. const ( roomLenReqType = "0" clearRoomReqType = "1" allRoomReqType = "2" ) -// request structs +// request structs. type roomLenRequest struct { RequestType string RequestID string @@ -21,6 +21,13 @@ type roomLenRequest struct { done chan bool `json:"-"` } +// response struct. +type roomLenResponse struct { + RequestType string + RequestID string + Connections int +} + type clearRoomRequest struct { RequestType string RequestID string @@ -38,13 +45,6 @@ type allRoomRequest struct { done chan bool `json:"-"` } -// response struct -type roomLenResponse struct { - RequestType string - RequestID string - Connections int -} - type allRoomResponse struct { RequestType string RequestID string