mirror of
https://github.com/lwch/natpass.git
synced 2026-10-21 11:54:20 +00:00
增加原子加的逻辑
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user