增加原子加的逻辑

This commit is contained in:
lwch
2021-08-27 14:26:23 +08:00
parent 1eaea40a50
commit 9e99ccbd4a
2 changed files with 4 additions and 5 deletions
+4 -3
View File
@@ -3,6 +3,7 @@ package handler
import (
"natpass/code/network"
"sync"
"sync/atomic"
"time"
"github.com/lwch/logging"
@@ -13,7 +14,7 @@ type clients struct {
parent *Handler
id string
data map[uint32]*client // idx => client
idx int
idx uint32
}
func newClients(parent *Handler, id string) *clients {
@@ -48,8 +49,8 @@ func (cs *clients) next() *client {
}
cs.RUnlock()
if len(list) > 0 {
cli := list[cs.idx%len(list)]
cs.idx++
idx := atomic.AddUint32(&cs.idx, 1)
cli := list[int(idx)%len(list)]
return cli
}
return nil
-2
View File
@@ -18,7 +18,6 @@ type Handler struct {
clients map[string]*clients // client id => client
lockLinks sync.RWMutex
links map[string][2]*client // link id => endpoints
idx int
}
// New create handler
@@ -27,7 +26,6 @@ func New(cfg *global.Configure) *Handler {
cfg: cfg,
clients: make(map[string]*clients),
links: make(map[string][2]*client),
idx: 0,
}
}