From 04d4e0fc6ad708677b6dba602866401b7e49a185 Mon Sep 17 00:00:00 2001 From: Yifu Yu Date: Thu, 15 Feb 2024 00:20:18 +0800 Subject: [PATCH] Attempt to fix the orphan client issue. --- main.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 02eb6e5..53733e5 100644 --- a/main.go +++ b/main.go @@ -168,6 +168,7 @@ func broadcastFiber(name string) { e := ctx.clientsChanList.Front() for e != nil { ch := e.Value.(*ClientChannel) + // Note: select in Golang is not ordered! select { case <-ch.Notify: // Closed client, remove this entry @@ -175,11 +176,14 @@ func broadcastFiber(name string) { next := e.Next() ctx.clientsChanList.Remove(e) e = next - case ch.Data <- chunk: - e = e.Next() + break default: - e = e.Next() } + select { + case ch.Data <- chunk: + default: + } + e = e.Next() } // Try to serve all new clients, but don't get locked up here :) if ctx.newClientsChanList.Len() > 0 && ctx.newChanLock.TryLock() {