From 9e99ccbd4ab516d30b36520f8ea264ad50d61a58 Mon Sep 17 00:00:00 2001 From: lwch Date: Fri, 27 Aug 2021 14:26:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=9F=E5=AD=90=E5=8A=A0?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/server/handler/clients.go | 7 ++++--- code/server/handler/handler.go | 2 -- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/code/server/handler/clients.go b/code/server/handler/clients.go index 21453c9..9d4601c 100644 --- a/code/server/handler/clients.go +++ b/code/server/handler/clients.go @@ -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 diff --git a/code/server/handler/handler.go b/code/server/handler/handler.go index 15c0c6b..9c25307 100644 --- a/code/server/handler/handler.go +++ b/code/server/handler/handler.go @@ -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, } }