From 88ae539f545fac848309da45f05bc7ed3c28c08d Mon Sep 17 00:00:00 2001 From: Roger Ferdinan Date: Tue, 7 Oct 2025 09:16:51 +0700 Subject: [PATCH] fix: fixing memory leak from unbuffered channel --- go.mod | 2 +- internal/hub.go | 12 ++++++------ v1/client/client.go | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 702eda8..d4971f3 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,4 @@ go 1.24.5 require github.com/gorilla/websocket v1.5.3 -require git.neurocipta.com/rogerferdinan/custom-rwmutex v1.0.0 // indirect +require git.neurocipta.com/rogerferdinan/custom-rwmutex v1.0.0 diff --git a/internal/hub.go b/internal/hub.go index 9158281..0d9f95f 100644 --- a/internal/hub.go +++ b/internal/hub.go @@ -23,9 +23,9 @@ type Client struct { func NewClient(conn *websocket.Conn, subscribedPath string) *Client { return &Client{ Conn: conn, - Send: make(chan []byte, 64), + Send: make(chan []byte, 2), SubscribedPath: subscribedPath, - done: make(chan struct{}), + done: make(chan struct{}, 1), } } @@ -38,10 +38,10 @@ type Hub struct { func NewHub() *Hub { return &Hub{ - Broadcast: make(chan []byte), - Register: make(chan *Client), - Unregister: make(chan *Client), - Clients: make(map[*Client]bool), + Broadcast: make(chan []byte, 1), + Register: make(chan *Client, 1), + Unregister: make(chan *Client, 1), + Clients: make(map[*Client]bool, 1), } } diff --git a/v1/client/client.go b/v1/client/client.go index e542e3d..5dbf903 100644 --- a/v1/client/client.go +++ b/v1/client/client.go @@ -155,7 +155,7 @@ func (b *SafeWebsocketClientBuilder) Build(ctx context.Context) (*SafeWebsocketC reconnectCh: make(chan struct{}, 1), isConnected: false, doneMap: NewSafeMap[string, chan struct{}](), - writeChan: make(chan Message), + writeChan: make(chan Message, 1), } if b.authenticateFn != nil { @@ -425,7 +425,7 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() { } func (wsClient *SafeWebsocketClient) ReconnectChannel() <-chan struct{} { - reconnectCh := make(chan struct{}) + reconnectCh := make(chan struct{}, 1) wsClient.mu.WriteHandler(func() error { wsClient.reconnectChans = append(wsClient.reconnectChans, reconnectCh) return nil