From 0b9a724dd0837e6959b020417a2e6dd931658a90 Mon Sep 17 00:00:00 2001 From: Martin Coetzee Date: Mon, 13 Mar 2023 14:39:56 +1000 Subject: [PATCH] some improvements --- Makefile | 4 ++++ bucket.go | 4 ---- bucket_test.go | 5 +++-- daemon.go | 20 ++++++++------------ daemon_test.go | 5 +++-- go.mod | 9 +++------ go.sum | 31 ++++++++++++++++++++++++++----- 7 files changed, 47 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index b398995..cd74af7 100644 --- a/Makefile +++ b/Makefile @@ -14,5 +14,9 @@ cover: ## Run test coverage suite build: ## Builds the token bucket @go build ${GO_FLAGS} +format: ## Format and organise imports + @go install mvdan.cc/gofumpt@latest + @gofumpt -l -w . + help: @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":[^:]*?## "}; {printf "\033[38;5;69m%-30s\033[38;5;38m %s\033[0m\n", $$1, $$2}' \ No newline at end of file diff --git a/bucket.go b/bucket.go index 818ff8c..f21e0f7 100644 --- a/bucket.go +++ b/bucket.go @@ -2,7 +2,6 @@ package tokenbucket import ( "sync" - "log" ) const hitCount = 1 @@ -30,18 +29,15 @@ func (b *Bucket) hit() bool { if b.availableTokens > 0 || (b.availableTokens-hitCount) > 0 { b.lastAvailableTokens = b.availableTokens b.availableTokens -= hitCount - log.Printf("reducing token count by %d, availble tokens %d\n", hitCount, b.availableTokens) b.mutex.Unlock() return true } b.mutex.Unlock() - log.Print("insufficient tokens available\n") return false } func (b *Bucket) fill() { b.mutex.Lock() - log.Printf("available tokens %d, filling back to %d\n", b.availableTokens, b.size) b.availableTokens = b.size b.mutex.Unlock() } diff --git a/bucket_test.go b/bucket_test.go index ae84ad0..3b1d541 100644 --- a/bucket_test.go +++ b/bucket_test.go @@ -1,9 +1,10 @@ package tokenbucket import ( - "gotest.tools/assert" "reflect" "testing" + + "gotest.tools/v3/assert" ) func TestNewBucket(t *testing.T) { @@ -76,4 +77,4 @@ func TestBucket_fill(t *testing.T) { b.fill() assert.Assert(t, b.availableTokens == b.size) }) -} \ No newline at end of file +} diff --git a/daemon.go b/daemon.go index 9c4e37c..0b1ff45 100644 --- a/daemon.go +++ b/daemon.go @@ -2,7 +2,6 @@ package tokenbucket import ( "context" - "log" "math/rand" "time" ) @@ -20,28 +19,27 @@ func (a *flag) has(flag flag) bool { } type Daemon struct { - flags flag - bucket *Bucket + flags flag + bucket *Bucket cancelFunc context.CancelFunc } func NewDaemon(bucket *Bucket, flags flag) *Daemon { return &Daemon{ - bucket: bucket, - flags: flags, + bucket: bucket, + flags: flags, } } func (w *Daemon) Start() { ctx, cancel := context.WithCancel(context.Background()) go func() { - ticker := time.Tick(time.Duration(1) * time.Second) - for true { + ticker := time.NewTicker(time.Duration(1) * time.Second) + for { select { - case <-ticker: + case <-ticker.C: w.bucket.fill() case <-ctx.Done(): - log.Printf("worker for bucket %s stopped\n", w.bucket.designation) return } } @@ -58,8 +56,7 @@ func (w *Daemon) Hit() bool { // If forgiving flag was set, look if the last available token was non 0 // And act be forgiving by flipping the result to true - if !result && w.flags.has(Forgiving) && w.bucket.lastAvailableTokens > 0{ - log.Printf("forgiving flag: retrying bucket\n") + if !result && w.flags.has(Forgiving) && w.bucket.lastAvailableTokens > 0 { w.bucket.lastAvailableTokens = 0 result = true } @@ -67,7 +64,6 @@ func (w *Daemon) Hit() bool { // If retryable flag was set, wait randomly between 0-5 seconds and retry if !result && w.flags.has(Retryable) { randSleep := rand.Intn(5) - log.Printf("retriable flag: sleeping for %d seconds\n", randSleep) time.Sleep(time.Duration(randSleep) * time.Second) result = w.bucket.hit() } diff --git a/daemon_test.go b/daemon_test.go index 4a29a27..a346d47 100644 --- a/daemon_test.go +++ b/daemon_test.go @@ -1,9 +1,10 @@ package tokenbucket import ( - "gotest.tools/assert" "testing" "time" + + "gotest.tools/v3/assert" ) func Test_flag_has(t *testing.T) { @@ -85,4 +86,4 @@ func TestDaemon_Hit(t *testing.T) { assert.Assert(t, dm.Hit()) dm.Stop() }) -} \ No newline at end of file +} diff --git a/go.mod b/go.mod index e8453fd..e1fdb18 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,7 @@ module github.com/dabump/tokenbucket -go 1.17 +go 1.20 -require gotest.tools v2.2.0+incompatible +require gotest.tools/v3 v3.4.0 -require ( - github.com/google/go-cmp v0.5.6 // indirect - github.com/pkg/errors v0.9.1 // indirect -) +require github.com/google/go-cmp v0.5.6 // indirect diff --git a/go.sum b/go.sum index afca702..ec50aaf 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,29 @@ +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= -gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o= +gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=