mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
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.
44 lines
597 B
Go
44 lines
597 B
Go
package snowflake_proxy
|
|
|
|
import (
|
|
"sync/atomic"
|
|
)
|
|
|
|
type tokens_t struct {
|
|
ch chan struct{}
|
|
capacity uint
|
|
clients atomic.Int64
|
|
}
|
|
|
|
func newTokens(capacity uint) *tokens_t {
|
|
var ch chan struct{}
|
|
if capacity != 0 {
|
|
ch = make(chan struct{}, capacity)
|
|
}
|
|
|
|
return &tokens_t{
|
|
ch: ch,
|
|
capacity: capacity,
|
|
clients: atomic.Int64{},
|
|
}
|
|
}
|
|
|
|
func (t *tokens_t) get() {
|
|
t.clients.Add(1)
|
|
|
|
if t.capacity != 0 {
|
|
t.ch <- struct{}{}
|
|
}
|
|
}
|
|
|
|
func (t *tokens_t) ret() {
|
|
t.clients.Add(-1)
|
|
|
|
if t.capacity != 0 {
|
|
<-t.ch
|
|
}
|
|
}
|
|
|
|
func (t *tokens_t) count() int64 {
|
|
return t.clients.Load()
|
|
}
|