Skip to content

Commit 6a73fc7

Browse files
authored
Merge pull request #140 from jzyjinziyan/fix_clients_maximum
Improve client connection handling with atomic operations
2 parents 6779e48 + 3b60322 commit 6a73fc7

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

mqtt/server.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,15 @@ func (s *Server) inheritClientSession(pk packets.Packet, cl *Client) bool {
516516
return true // [MQTT-3.2.2-3]
517517
}
518518

519-
if atomic.LoadInt64(&s.Info.ClientsConnected) > atomic.LoadInt64(&s.Info.ClientsMaximum) {
520-
atomic.AddInt64(&s.Info.ClientsMaximum, 1)
519+
for {
520+
currentMax := atomic.LoadInt64(&s.Info.ClientsMaximum)
521+
currentConn := atomic.LoadInt64(&s.Info.ClientsConnected)
522+
if currentConn <= currentMax {
523+
break
524+
}
525+
if atomic.CompareAndSwapInt64(&s.Info.ClientsMaximum, currentMax, currentConn) {
526+
break
527+
}
521528
}
522529

523530
if pk.Connect.Clean {

0 commit comments

Comments
 (0)