From 46fdcce5c6e4e1406893cbe9368eb75fa5f225c7 Mon Sep 17 00:00:00 2001 From: WofWca Date: Mon, 3 Mar 2025 15:46:19 +0400 Subject: [PATCH] fix: data race warnings of tokens_t This migrates from using `atomic.LoadInt64` on `int64` to making the `clients` field itself `atomic.Int64`. Also `count` now takes `*tokens_t` by reference, which fixes a linter warning. It's not clear to me why it warned about this, but I simplified it anyway. --- proxy/lib/tokens.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/proxy/lib/tokens.go b/proxy/lib/tokens.go index d312ecf..7fc0c99 100644 --- a/proxy/lib/tokens.go +++ b/proxy/lib/tokens.go @@ -7,7 +7,7 @@ import ( type tokens_t struct { ch chan struct{} capacity uint - clients int64 + clients atomic.Int64 } func newTokens(capacity uint) *tokens_t { @@ -19,12 +19,12 @@ func newTokens(capacity uint) *tokens_t { return &tokens_t{ ch: ch, capacity: capacity, - clients: 0, + clients: atomic.Int64{}, } } func (t *tokens_t) get() { - atomic.AddInt64(&t.clients, 1) + t.clients.Add(1) if t.capacity != 0 { t.ch <- struct{}{} @@ -32,13 +32,13 @@ func (t *tokens_t) get() { } func (t *tokens_t) ret() { - atomic.AddInt64(&t.clients, -1) + t.clients.Add(-1) if t.capacity != 0 { <-t.ch } } -func (t tokens_t) count() int64 { - return atomic.LoadInt64(&t.clients) +func (t *tokens_t) count() int64 { + return t.clients.Load() }