Turn the proxy code into a library

Allow other go programs to easily import the snowflake proxy library and
start/stop a snowflake proxy.
This commit is contained in:
idk 2021-10-25 22:51:40 -04:00 committed by Cecylia Bocovich
parent 54ab79384f
commit 50e4f4fd61
7 changed files with 184 additions and 99 deletions

44
proxy/lib/tokens.go Normal file
View file

@ -0,0 +1,44 @@
package snowflake
import (
"sync/atomic"
)
type tokens_t struct {
ch chan struct{}
capacity uint
clients 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: 0,
}
}
func (t *tokens_t) get() {
atomic.AddInt64(&t.clients, 1)
if t.capacity != 0 {
t.ch <- struct{}{}
}
}
func (t *tokens_t) ret() {
atomic.AddInt64(&t.clients, -1)
if t.capacity != 0 {
<-t.ch
}
}
func (t tokens_t) count() int64 {
return atomic.LoadInt64(&t.clients)
}