mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 11:11:30 -04:00
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.
This commit is contained in:
parent
730e400123
commit
46fdcce5c6
1 changed files with 6 additions and 6 deletions
|
@ -7,7 +7,7 @@ import (
|
||||||
type tokens_t struct {
|
type tokens_t struct {
|
||||||
ch chan struct{}
|
ch chan struct{}
|
||||||
capacity uint
|
capacity uint
|
||||||
clients int64
|
clients atomic.Int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTokens(capacity uint) *tokens_t {
|
func newTokens(capacity uint) *tokens_t {
|
||||||
|
@ -19,12 +19,12 @@ func newTokens(capacity uint) *tokens_t {
|
||||||
return &tokens_t{
|
return &tokens_t{
|
||||||
ch: ch,
|
ch: ch,
|
||||||
capacity: capacity,
|
capacity: capacity,
|
||||||
clients: 0,
|
clients: atomic.Int64{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tokens_t) get() {
|
func (t *tokens_t) get() {
|
||||||
atomic.AddInt64(&t.clients, 1)
|
t.clients.Add(1)
|
||||||
|
|
||||||
if t.capacity != 0 {
|
if t.capacity != 0 {
|
||||||
t.ch <- struct{}{}
|
t.ch <- struct{}{}
|
||||||
|
@ -32,13 +32,13 @@ func (t *tokens_t) get() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tokens_t) ret() {
|
func (t *tokens_t) ret() {
|
||||||
atomic.AddInt64(&t.clients, -1)
|
t.clients.Add(-1)
|
||||||
|
|
||||||
if t.capacity != 0 {
|
if t.capacity != 0 {
|
||||||
<-t.ch
|
<-t.ch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t tokens_t) count() int64 {
|
func (t *tokens_t) count() int64 {
|
||||||
return atomic.LoadInt64(&t.clients)
|
return t.clients.Load()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue