Skip to content

Commit 87a0167

Browse files
committed
refactor concurrency: replace atomic package usage with sync/atomic for improved clarity and thread safety
1 parent 4ef3ea8 commit 87a0167

3 files changed

Lines changed: 7 additions & 7 deletions

File tree

std/gateways/fasthttp/bundle.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type bundle struct {
4949
wsUpgrade websocket.FastHTTPUpgrader
5050
wsRoutes map[string]*routeData
5151
wsEndpoint string
52-
wsNextID uint64
52+
wsNextID atomic.Uint64
5353
predicateKey string
5454
rpcInFactory kit.IncomingRPCFactory
5555
rpcOutFactory kit.OutgoingRPCFactory
@@ -244,7 +244,7 @@ func (b *bundle) wsHandler(ctx *fasthttp.RequestCtx) {
244244
func(conn *websocket.Conn) {
245245
wsc := &wsConn{
246246
kv: map[string]string{},
247-
id: atomic.AddUint64(&b.wsNextID, 1),
247+
id: b.wsNextID.Add(1),
248248
clientIP: realip.FromRequest(ctx),
249249
c: conn,
250250
rpcOutFactory: b.rpcOutFactory,

std/gateways/fastws/gateway.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type gateway struct {
1717
utils.SpinLock
1818

1919
b *bundle
20-
nextID uint64
20+
nextID atomic.Uint64
2121
conns map[uint64]*wsConn
2222
}
2323

@@ -51,7 +51,7 @@ func (gw *gateway) OnShutdown(_ gnet.Engine) {}
5151

5252
func (gw *gateway) OnOpen(c gnet.Conn) (out []byte, action gnet.Action) {
5353
wsc := newWebsocketConn(
54-
atomic.AddUint64(&gw.nextID, 1),
54+
gw.nextID.Add(1),
5555
c,
5656
gw.b.rpcOutFactory,
5757
gw.b.writeMode,

stub/stub_ws.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type WebsocketCtx struct {
3131

3232
pendingMtx sync.Mutex
3333
pending map[string]chan kit.IncomingRPCContainer
34-
lastActivity uint32
34+
lastActivity atomic.Uint32
3535
disconnectChan chan struct{}
3636

3737
// fasthttp entities
@@ -116,11 +116,11 @@ func (wCtx *WebsocketCtx) Reconnect(ctx context.Context) error {
116116
}
117117

118118
func (wCtx *WebsocketCtx) setActivity() {
119-
atomic.StoreUint32(&wCtx.lastActivity, uint32(utils.TimeUnix()))
119+
wCtx.lastActivity.Store(uint32(utils.TimeUnix()))
120120
}
121121

122122
func (wCtx *WebsocketCtx) getActivity() int64 {
123-
return int64(atomic.LoadUint32(&wCtx.lastActivity))
123+
return int64(wCtx.lastActivity.Load())
124124
}
125125

126126
func (wCtx *WebsocketCtx) watchdog(c *websocket.Conn) {

0 commit comments

Comments
 (0)