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