Skip to content

Commit

Permalink
feat(local): implement static token bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed Sep 2, 2024
1 parent a3034e6 commit cb9af35
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions drivers/local/token_bucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package local

import "context"

type TokenBucket interface {
Take() <-chan struct{}
Put()
Do(context.Context, func() error) error
}

// StaticTokenBucket is a bucket with a fixed number of tokens,
// where the retrieval and return of tokens are manually controlled.
// In the initial state, the bucket is full.
type StaticTokenBucket struct {
bucket chan struct{}
}

func NewStaticTokenBucket(size int) StaticTokenBucket {
bucket := make(chan struct{}, size)
for range size {
bucket <- struct{}{}
}
return StaticTokenBucket{bucket: bucket}
}

func (b StaticTokenBucket) Take() <-chan struct{} {
return b.bucket
}

func (b StaticTokenBucket) Put() {
b.bucket <- struct{}{}
}

func (b StaticTokenBucket) Do(ctx context.Context, f func() error) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-b.bucket:
defer b.Put()
}
return f()
}

// NopTokenBucket all function calls to this bucket will success immediately
type NopTokenBucket struct {
nop chan struct{}
}

func NewNopTokenBucket() NopTokenBucket {
nop := make(chan struct{})
close(nop)
return NopTokenBucket{nop}
}

func (b NopTokenBucket) Take() <-chan struct{} {
return b.nop
}

func (b NopTokenBucket) Put() {}

func (b NopTokenBucket) Do(_ context.Context, f func() error) error { return f() }

0 comments on commit cb9af35

Please sign in to comment.