Skip to content

Commit

Permalink
Panic fix on gorilla ws when connection lost.
Browse files Browse the repository at this point in the history
fix linter errors with shadow checking
upgrade linter
  • Loading branch information
ElecTwix committed Apr 28, 2024
1 parent 7c2584a commit caf1bea
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 31 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ permissions:
jobs:
golangci:
permissions:
contents: read # for actions/checkout to fetch code
pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
contents: read # for actions/checkout to fetch code
pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- uses: actions/setup-go@v5
with:
go-version: '>=1.18'
- uses: actions/checkout@v3
go-version: ">=1.18"
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v5
with:
args: --timeout=5m
11 changes: 6 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ linters-settings:
- condition
- return
ignored-numbers:
- '0'
- '1'
- '2'
- '3'
- "0"
- "1"
- "2"
- "3"
ignored-functions:
- strings.SplitN

govet:
check-shadowing: true
enable:
- shadow
lll:
line-length: 140
misspell:
Expand Down
6 changes: 6 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func New(url string, connection conn.Connection) (*DB, error) {
return &DB{connection}, nil
}

// Initialize initializes the connection to the database.
// Can return an error if the connection fails on runtime.
func (db *DB) Initialize() error {
return db.conn.Initialize()
}

// --------------------------------------------------
// Public methods
// --------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ func (s *SurrealDBTestSuite) openConnection() *surrealdb.DB {
impl := s.connImplementations[s.name]
require.NotNil(s.T(), impl)
db, err := surrealdb.New(url, impl)

go func(s *SurrealDBTestSuite) {
connErr := db.Initialize()
if connErr != nil {
fmt.Println(connErr)
require.NoError(s.T(), connErr)
}
}(s)

s.Require().NoError(err)
return db
}
Expand Down
7 changes: 5 additions & 2 deletions internal/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (
"github.com/surrealdb/surrealdb.go/pkg/model"
)

type ws struct {
}
type ws struct{}

func (w *ws) Connect(url string) (conn.Connection, error) {
return w, nil
}

func (w *ws) Initialize() error {
return nil
}

func (w *ws) Send(method string, params []interface{}) (interface{}, error) {
return nil, nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "github.com/surrealdb/surrealdb.go/pkg/model"

type Connection interface {
Connect(url string) (Connection, error)
Initialize() error
Send(method string, params []interface{}) (interface{}, error)
Close() error
LiveNotifications(id string) (chan model.Notification, error)
Expand Down
43 changes: 25 additions & 18 deletions pkg/conn/gorilla/gorilla.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
"reflect"
"strconv"
Expand Down Expand Up @@ -73,10 +74,13 @@ func (ws *WebSocket) Connect(url string) (conn.Connection, error) {
}
}

ws.initialize()
return ws, nil
}

func (ws *WebSocket) Initialize() error {
return ws.initialize()
}

func (ws *WebSocket) SetTimeOut(timeout time.Duration) *WebSocket {
ws.Option = append(ws.Option, func(ws *WebSocket) error {
ws.Timeout = timeout
Expand Down Expand Up @@ -234,26 +238,29 @@ func (ws *WebSocket) write(v interface{}) error {
return ws.Conn.WriteMessage(gorilla.TextMessage, data)
}

func (ws *WebSocket) initialize() {
go func() {
for {
select {
case <-ws.close:
return
default:
var res rpc.RPCResponse
err := ws.read(&res)
if err != nil {
if errors.Is(err, net.ErrClosed) {
break
}
ws.logger.Error(err.Error())
continue
func (ws *WebSocket) initialize() error {
for {
select {
case <-ws.close:
return nil
default:
var res rpc.RPCResponse
err := ws.read(&res)
if err != nil {
// this needed because gorilla not shudown gracefully
if errors.Is(err, net.ErrClosed) {
return nil
}
// returns error if connection drop in fly
if gorilla.IsUnexpectedCloseError(err) {
return io.ErrClosedPipe
}
go ws.handleResponse(res)
ws.logger.Error(err.Error())
continue
}
go ws.handleResponse(res)
}
}()
}
}

func (ws *WebSocket) handleResponse(res rpc.RPCResponse) {
Expand Down

0 comments on commit caf1bea

Please sign in to comment.