Skip to content

Commit

Permalink
Make code tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed May 12, 2024
1 parent 238fc48 commit 9828e8b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions backend/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
)

type WSBackend struct {
URL string
connections map[string]*websocket.Conn
lock *sync.RWMutex
URL string
}

// NewWSBackend creates a new instance of WSBackend with the specified URL.
func NewWSBackend(url string) *WSBackend {
return &WSBackend{
URL: url,
connections: make(map[string]*websocket.Conn),
lock: &sync.RWMutex{},
URL: url,
}
}

Expand Down Expand Up @@ -49,11 +49,15 @@ func (b *WSBackend) getConnection(conn wasabi.Connection) (*websocket.Conn, erro
return c, nil
}

c, _, err := websocket.Dial(conn.Context(), b.URL, nil)
c, resp, err := websocket.Dial(conn.Context(), b.URL, nil)
if err != nil {
return nil, err
}

if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}

b.lock.Lock()
b.connections[conn.ID()] = c
b.lock.Unlock()
Expand All @@ -71,7 +75,7 @@ func (b *WSBackend) responseHandler(server *websocket.Conn, client wasabi.Connec
delete(b.connections, client.ID())
b.lock.Unlock()

// TODO: implement propogation of status code if connection was cloased by server
// TODO: implement propagation of status code if connection was cloased by server
server.Close(websocket.StatusNormalClosure, "")
client.Close(websocket.StatusNormalClosure, "")
}()
Expand All @@ -81,6 +85,7 @@ func (b *WSBackend) responseHandler(server *websocket.Conn, client wasabi.Connec

for ctx.Err() == nil {
buffer.Reset()

msgType, reader, err := server.Reader(ctx)
if err != nil {
return
Expand Down
1 change: 1 addition & 0 deletions backend/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func TestGetConnectionDialError(t *testing.T) {
func TestWSBackend_Handle(t *testing.T) {
server := httptest.NewServer(wsHandlerEcho)
url := "ws://" + server.Listener.Addr().String()

defer server.Close()

conn := mocks.NewMockConnection(t)
Expand Down

0 comments on commit 9828e8b

Please sign in to comment.