Skip to content

Commit

Permalink
Merge pull request #1 from dabump/improvements
Browse files Browse the repository at this point in the history
some improvements
  • Loading branch information
dabump committed Mar 13, 2023
2 parents 40e9f85 + 0b9a724 commit 653d02c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
4 changes: 0 additions & 4 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tokenbucket

import (
"sync"
"log"
)

const hitCount = 1
Expand Down Expand Up @@ -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()
}
5 changes: 3 additions & 2 deletions bucket_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package tokenbucket

import (
"gotest.tools/assert"
"reflect"
"testing"

"gotest.tools/v3/assert"
)

func TestNewBucket(t *testing.T) {
Expand Down Expand Up @@ -76,4 +77,4 @@ func TestBucket_fill(t *testing.T) {
b.fill()
assert.Assert(t, b.availableTokens == b.size)
})
}
}
20 changes: 8 additions & 12 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tokenbucket

import (
"context"
"log"
"math/rand"
"time"
)
Expand All @@ -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
}
}
Expand All @@ -58,16 +56,14 @@ 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
}

// 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()
}
Expand Down
5 changes: 3 additions & 2 deletions daemon_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package tokenbucket

import (
"gotest.tools/assert"
"testing"
"time"

"gotest.tools/v3/assert"
)

func Test_flag_has(t *testing.T) {
Expand Down Expand Up @@ -85,4 +86,4 @@ func TestDaemon_Hit(t *testing.T) {
assert.Assert(t, dm.Hit())
dm.Stop()
})
}
}
9 changes: 3 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
31 changes: 26 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 653d02c

Please sign in to comment.