From f32d97eac44b4c4e028060deb93d0cb9e0d40866 Mon Sep 17 00:00:00 2001 From: Roger Ferdinan Date: Fri, 26 Sep 2025 09:16:35 +0700 Subject: [PATCH] chore: cleaning unused mutex --- go.mod | 2 ++ go.sum | 2 ++ internal/hub.go | 5 ----- internal/mutex.go | 33 --------------------------------- v1/client/client.go | 7 ++++--- 5 files changed, 8 insertions(+), 41 deletions(-) delete mode 100644 internal/mutex.go diff --git a/go.mod b/go.mod index 155947b..702eda8 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module git.neurocipta.com/rogerferdinan/safe-web-socket go 1.24.5 require github.com/gorilla/websocket v1.5.3 + +require git.neurocipta.com/rogerferdinan/custom-rwmutex v1.0.0 // indirect diff --git a/go.sum b/go.sum index 25a9fc4..9bfc916 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +git.neurocipta.com/rogerferdinan/custom-rwmutex v1.0.0 h1:KnNc40SrYsg0cksIIcQy/ca6bunkGADQOs1u7O/E+iY= +git.neurocipta.com/rogerferdinan/custom-rwmutex v1.0.0/go.mod h1:9DvvHc2UZhBwEs63NgO4IhiuHnBNtTuBkTJgiMnnCss= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= diff --git a/internal/hub.go b/internal/hub.go index 59dcec3..9ab84d4 100644 --- a/internal/hub.go +++ b/internal/hub.go @@ -19,7 +19,6 @@ type Client struct { Send chan []byte SubscribedPath string done chan struct{} - mu *CustomRwMutex } func NewClient(conn *websocket.Conn, subscribedPath string) *Client { @@ -28,7 +27,6 @@ func NewClient(conn *websocket.Conn, subscribedPath string) *Client { Send: make(chan []byte, 64), SubscribedPath: subscribedPath, done: make(chan struct{}), - mu: NewCustomRwMutex(), } } @@ -37,8 +35,6 @@ type Hub struct { Broadcast chan []byte Register chan *Client Unregister chan *Client - writeMu *CustomRwMutex - readMu *CustomRwMutex } func NewHub() *Hub { @@ -47,7 +43,6 @@ func NewHub() *Hub { Register: make(chan *Client), Unregister: make(chan *Client), Clients: make(map[*Client]bool), - writeMu: NewCustomRwMutex(), } } diff --git a/internal/mutex.go b/internal/mutex.go deleted file mode 100644 index 9c8821e..0000000 --- a/internal/mutex.go +++ /dev/null @@ -1,33 +0,0 @@ -package internal - -import ( - "sync" -) - -type CustomRwMutex struct { - mu *sync.RWMutex -} - -func NewCustomRwMutex() *CustomRwMutex { - return &CustomRwMutex{ - mu: &sync.RWMutex{}, - } -} - -func (rwMu *CustomRwMutex) WriteHandler(fn func() error) error { - rwMu.mu.Lock() - defer rwMu.mu.Unlock() - if err := fn(); err != nil { - return err - } - return nil -} - -func (rwMu *CustomRwMutex) ReadHandler(fn func() error) error { - rwMu.mu.RLock() - defer rwMu.mu.RUnlock() - if err := fn(); err != nil { - return err - } - return nil -} diff --git a/v1/client/client.go b/v1/client/client.go index 7e890ac..f252857 100644 --- a/v1/client/client.go +++ b/v1/client/client.go @@ -8,6 +8,7 @@ import ( "strings" "time" + custom_rwmutex "git.neurocipta.com/rogerferdinan/custom-rwmutex" "git.neurocipta.com/rogerferdinan/safe-web-socket/internal" "github.com/gorilla/websocket" ) @@ -74,7 +75,7 @@ func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) { ctx: ctx, cancel: cancel, dataChannel: make(chan []byte, 1), - mu: internal.NewCustomRwMutex(), + mu: custom_rwmutex.NewCustomRwMutex(), reconnectCh: make(chan struct{}, 1), isConnected: false, } @@ -94,7 +95,7 @@ type SafeWebsocketClient struct { path *string rawQuery *string dataChannel chan []byte - mu *internal.CustomRwMutex + mu *custom_rwmutex.CustomRwMutex conn *websocket.Conn ctx context.Context cancel context.CancelFunc @@ -181,7 +182,7 @@ func (wsClient *SafeWebsocketClient) startReceiveHandler() { log.Println("Reconnect handler stopped") return default: - if err := wsClient.mu.WriteHandler(func() error { + if err := wsClient.mu.ReadHandler(func() error { conn := wsClient.conn if conn == nil {