guard against nil connections on write to protect against concurrent listener removals.
This commit is contained in:
@@ -2,6 +2,7 @@ package khatru
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
@@ -31,6 +32,9 @@ type WebSocket struct {
|
||||
}
|
||||
|
||||
func (ws *WebSocket) WriteJSON(any any) error {
|
||||
if ws == nil {
|
||||
return fmt.Errorf("connection doesn't exist")
|
||||
}
|
||||
ws.mutex.Lock()
|
||||
err := ws.conn.WriteJSON(any)
|
||||
ws.mutex.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user