Attempt to fix the orphan client issue.

This commit is contained in:
Yifu Yu 2024-02-15 00:20:18 +08:00
parent 7a9ec1a1d3
commit 04d4e0fc6a
1 changed files with 7 additions and 3 deletions

10
main.go
View File

@ -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() {