Skip to content

Commit

Permalink
Merge pull request #2 from sshaplygin/add-lint
Browse files Browse the repository at this point in the history
add: golangci lint
  • Loading branch information
sshaplygin authored May 13, 2022
2 parents a54726f + 218adc4 commit 2635dda
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 38 deletions.
28 changes: 28 additions & 0 deletions .github/workflow/lint.yml
Original file line number Diff line number Diff line change
@@ -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
65 changes: 65 additions & 0 deletions .github/workflow/test.yml
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 13 additions & 5 deletions adapter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package go_socket_io_redis_adapter
package go_socketio_redis_adapter

import (
"fmt"
Expand All @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions broadcast.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package go_socket_io_redis_adapter
package go_socketio_redis_adapter

import (
"encoding/json"
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package go_socket_io_redis_adapter
package go_socketio_redis_adapter

import "github.com/gofrs/uuid"

Expand Down
6 changes: 3 additions & 3 deletions interface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package go_socket_io_redis_adapter
package go_socketio_redis_adapter

import (
"io"
Expand All @@ -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
Expand Down
9 changes: 0 additions & 9 deletions logger.go

This file was deleted.

16 changes: 9 additions & 7 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
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",
Network: "tcp",
}
}

// 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
Expand Down
20 changes: 10 additions & 10 deletions types.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2635dda

Please sign in to comment.