Skip to content

Commit

Permalink
fix: remove listeners when client disconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0ff authored and fiatjaf committed Dec 29, 2022
1 parent 9ba4326 commit 8b3ff7a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) {
if _, ok := s.clients[conn]; ok {
conn.Close()
delete(s.clients, conn)
removeListener(ws)
}
s.clientsMu.Unlock()
}()
Expand Down Expand Up @@ -214,7 +215,7 @@ func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) {
return
}

removeListener(ws, id)
removeListenerId(ws, id)
break
default:
if cwh, ok := s.relay.(CustomWebSocketHandler); ok {
Expand Down
14 changes: 13 additions & 1 deletion listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func setListener(id string, ws *WebSocket, filters nostr.Filters) {
}
}

func removeListener(ws *WebSocket, id string) {
// Remove a specific subscription id from listeners for a given ws client
func removeListenerId(ws *WebSocket, id string) {
listenersMutex.Lock()
defer func() {
listenersMutex.Unlock()
Expand All @@ -78,6 +79,17 @@ func removeListener(ws *WebSocket, id string) {
}
}

// Remove WebSocket conn from listeners
func removeListener(ws *WebSocket) {
listenersMutex.Lock()
defer listenersMutex.Unlock()

_, ok := listeners[ws]
if ok {
delete(listeners, ws)
}
}

func notifyListeners(event *nostr.Event) {
listenersMutex.Lock()
defer func() {
Expand Down

0 comments on commit 8b3ff7a

Please sign in to comment.