feat: remove unused client mutex

- remove unused Mutex from `models.Client` struct
- remove lock from `HandleConnection()`
- replace websocket.Conn with models.Client in `handleClientCommands()`

🤖
This commit is contained in:
Sebastian Mark 2024-10-30 08:09:41 +01:00
parent 94b6786c7c
commit bdfd5c3b84
3 changed files with 8 additions and 12 deletions

View file

@ -3,7 +3,6 @@ package websocket
import ( import (
"encoding/json" "encoding/json"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/GoTomato/internal/pomodoro" "git.smsvc.net/pomodoro/GoTomato/internal/pomodoro"
"git.smsvc.net/pomodoro/GoTomato/internal/shared" "git.smsvc.net/pomodoro/GoTomato/internal/shared"
@ -11,7 +10,8 @@ import (
) )
// Listens for commands from a client and handles them // Listens for commands from a client and handles them
func handleClientCommands(ws *websocket.Conn) { func handleClientCommands(c models.Client) {
ws := c.Conn
for { for {
var clientCommand models.ClientCommand var clientCommand models.ClientCommand

View file

@ -31,12 +31,13 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) {
log.Info("Client connected", "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)+1) log.Info("Client connected", "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)+1)
// Register the new client // Register the new client
mu.Lock() client := models.Client{
Clients[ws] = &models.Client{ Conn: ws,
Conn: ws, // Store the WebSocket connection
} }
mu.Lock()
Clients[ws] = &client
mu.Unlock() mu.Unlock()
// Listen for commands from the connected client // Listen for commands from the connected client
handleClientCommands(ws) handleClientCommands(client)
} }

View file

@ -3,7 +3,6 @@ package models
import ( import (
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"sync"
) )
// Represents a command from the client (start/stop) // Represents a command from the client (start/stop)
@ -15,16 +14,12 @@ type ClientCommand struct {
// Represents a single client // Represents a single client
type Client struct { type Client struct {
Conn *websocket.Conn // Websocket connection of the client Conn *websocket.Conn
Mutex sync.Mutex // Mutex used to lock
} }
// Sends a message to the websocket. // Sends a message to the websocket.
// Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time. // Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time.
func (c *Client) SendMessage(messageType int, data []byte) error { func (c *Client) SendMessage(messageType int, data []byte) error {
c.Mutex.Lock()
defer c.Mutex.Unlock()
err := c.Conn.WriteMessage(messageType, data) err := c.Conn.WriteMessage(messageType, data)
if err != nil { if err != nil {
log.Error("Error writing to WebSocket:", "msg", err) log.Error("Error writing to WebSocket:", "msg", err)