Documentation ¶
Index ¶
- type Bucket
- type BucketManager
- func (bm *BucketManager) Add(id string, bucket *Bucket)
- func (bm *BucketManager) CanDraw(id string, amount int) bool
- func (bm *BucketManager) Draw(id string, amount int) bool
- func (bm *BucketManager) DrawMax(id string, amount int) int
- func (bm *BucketManager) ForceDraw(id string, amount int) int
- func (bm *BucketManager) Get(id string) *Bucket
- func (bm *BucketManager) Purge()
- func (bm *BucketManager) RemainingTime(id string) int64
- func (bm *BucketManager) RemainingUses(id string) int
- func (bm *BucketManager) Reset(id string)
- func (bm *BucketManager) SaveFile(path string) error
- type SyncBucket
- type SyncBucketManager
- func (bm *SyncBucketManager) Add(id string, bucket *SyncBucket)
- func (bm *SyncBucketManager) CanDraw(id string, amount int) bool
- func (bm *SyncBucketManager) Draw(id string, amount int) bool
- func (bm *SyncBucketManager) DrawMax(id string, amount int) int
- func (bm *SyncBucketManager) ForceDraw(id string, amount int) int
- func (bm *SyncBucketManager) Get(id string) *SyncBucket
- func (bm *SyncBucketManager) Purge()
- func (bm *SyncBucketManager) RemainingTime(id string) int64
- func (bm *SyncBucketManager) RemainingUses(id string) int
- func (bm *SyncBucketManager) Reset(id string)
- func (bm *SyncBucketManager) SaveFile(path string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bucket ¶
func (*Bucket) Draw ¶
Draw draws tokens from a bucket, returning false and doing nothing if there are not enough.
func (*Bucket) DrawMax ¶
DrawMax draws as many tokens from as possible up to `amount`, returning the number of drawn tokens.
func (*Bucket) ForceDraw ¶
ForceDraw forcefully draw a certain number of tokens and returns the number of remaining uses, which may be negative.
The number of uses be reset to the limit at the next reset, even if this returns a negative number due to excess drawing.
func (*Bucket) RemainingTime ¶
RemainingTime returns the remaining time until the next reset, in milliseconds.
func (*Bucket) RemainingUses ¶
RemainingUses returns the remaining uses until the bucket is depleted, which may be negative.
type BucketManager ¶
func LoadFile ¶
func LoadFile(path string) (*BucketManager, error)
LoadFile loads a BucketManager from a JSON file.
func New ¶
func New(limit int, resetAfter time.Duration) *BucketManager
New creates a new BucketManager.
Bucket instances are automatically inserted as needed.
func (*BucketManager) Add ¶
func (bm *BucketManager) Add(id string, bucket *Bucket)
Add manually adds a Bucket to this BucketManager.
func (*BucketManager) CanDraw ¶
func (bm *BucketManager) CanDraw(id string, amount int) bool
CanDraw checks if a certain number of tokens can be drawn from a Bucket.
func (*BucketManager) Draw ¶
func (bm *BucketManager) Draw(id string, amount int) bool
Draw draws tokens from a bucket, returning false and doing nothing if there are not enough.
func (*BucketManager) DrawMax ¶
func (bm *BucketManager) DrawMax(id string, amount int) int
DrawMax draws as many tokens from a Bucket as possible up to `amount`, returning the number of drawn tokens.
func (*BucketManager) ForceDraw ¶
func (bm *BucketManager) ForceDraw(id string, amount int) int
ForceDraw forcefully draw a certain number of tokens from a Bucket and returns the number of remaining uses, which may be negative.
The number of uses be reset to the limit at the next reset, even if this returns a negative number due to excess drawing.
func (*BucketManager) Get ¶
func (bm *BucketManager) Get(id string) *Bucket
Get gets a Bucket from this BucketManager by its ID.
Creates a new Bucket if one does not exist.
func (*BucketManager) Purge ¶ added in v2.2.2
func (bm *BucketManager) Purge()
Purge purges buckets that are no longer needed, as they have reset.
func (*BucketManager) RemainingTime ¶
func (bm *BucketManager) RemainingTime(id string) int64
RemainingTime returns the remaining time until the Bucket resets in milliseconds.
func (*BucketManager) RemainingUses ¶
func (bm *BucketManager) RemainingUses(id string) int
RemainingUses returns the remaining uses until the Bucket is depleted, which may be negative.
func (*BucketManager) Reset ¶
func (bm *BucketManager) Reset(id string)
Reset resets a Bucket's uses.
func (*BucketManager) SaveFile ¶
func (bm *BucketManager) SaveFile(path string) error
SaveFile saves this BucketManager to a file as JSON.
type SyncBucket ¶ added in v2.2.0
type SyncBucket struct { Uses int Limit int ResetAfter time.Duration NextReset time.Time // contains filtered or unexported fields }
func NewSyncBucket ¶ added in v2.2.0
func NewSyncBucket(limit int, resetAfter time.Duration) *SyncBucket
NewSyncBucket creates a new SyncBucket.
func (*SyncBucket) CanDraw ¶ added in v2.2.0
func (b *SyncBucket) CanDraw(amount int) bool
CanDraw checks if a certain number of tokens can be drawn.
func (*SyncBucket) Draw ¶ added in v2.2.0
func (b *SyncBucket) Draw(amount int) bool
Draw draws tokens from a SyncBucket, returning false and doing nothing if there are not enough.
func (*SyncBucket) DrawMax ¶ added in v2.2.0
func (b *SyncBucket) DrawMax(amount int) int
DrawMax draws as many tokens from as possible up to `amount`, returning the number of drawn tokens.
func (*SyncBucket) ForceDraw ¶ added in v2.2.0
func (b *SyncBucket) ForceDraw(amount int) int
ForceDraw forcefully draw a certain number of tokens and returns the number of remaining uses, which may be negative.
The number of uses be reset to the limit at the next reset, even if this returns a negative number due to excess drawing.
func (*SyncBucket) RemainingTime ¶ added in v2.2.0
func (b *SyncBucket) RemainingTime() int64
RemainingTime returns the remaining time until the next reset, in milliseconds.
func (*SyncBucket) RemainingUses ¶ added in v2.2.0
func (b *SyncBucket) RemainingUses() int
RemainingUses returns the remaining uses until the SyncBucket is depleted, which may be negative.
func (*SyncBucket) Reset ¶ added in v2.2.0
func (b *SyncBucket) Reset()
Reset resets this SyncBucket's uses.
type SyncBucketManager ¶ added in v2.2.0
type SyncBucketManager struct { Buckets map[string]*SyncBucket Limit int ResetAfter time.Duration // contains filtered or unexported fields }
func LoadSyncFile ¶ added in v2.2.0
func LoadSyncFile(path string) (*SyncBucketManager, error)
LoadSyncFile loads a SyncBucketManager from a JSON file.
func NewSync ¶ added in v2.2.0
func NewSync(limit int, resetAfter time.Duration) *SyncBucketManager
NewSync creates a new SyncBucketManager.
SyncBucket instances are automatically inserted as needed.
func (*SyncBucketManager) Add ¶ added in v2.2.0
func (bm *SyncBucketManager) Add(id string, bucket *SyncBucket)
Add manually adds a SyncBucket to this SyncBucketManager.
func (*SyncBucketManager) CanDraw ¶ added in v2.2.0
func (bm *SyncBucketManager) CanDraw(id string, amount int) bool
CanDraw checks if a certain number of tokens can be drawn from a SyncBucket.
func (*SyncBucketManager) Draw ¶ added in v2.2.0
func (bm *SyncBucketManager) Draw(id string, amount int) bool
Draw draws tokens from a SyncBucket, returning false and doing nothing if there are not enough.
func (*SyncBucketManager) DrawMax ¶ added in v2.2.0
func (bm *SyncBucketManager) DrawMax(id string, amount int) int
DrawMax draws as many tokens from a SyncBucket as possible up to `amount`, returning the number of drawn tokens.
func (*SyncBucketManager) ForceDraw ¶ added in v2.2.0
func (bm *SyncBucketManager) ForceDraw(id string, amount int) int
ForceDraw forcefully draw a certain number of tokens from a SyncBucket and returns the number of remaining uses, which may be negative.
The number of uses be reset to the limit at the next reset, even if this returns a negative number due to excess drawing.
func (*SyncBucketManager) Get ¶ added in v2.2.0
func (bm *SyncBucketManager) Get(id string) *SyncBucket
Get gets a SyncBucket from this SyncBucketManager by its ID.
Creates a new SyncBucket if one does not exist.
func (*SyncBucketManager) Purge ¶ added in v2.2.2
func (bm *SyncBucketManager) Purge()
Purge purges buckets that are no longer needed, as they have reset.
func (*SyncBucketManager) RemainingTime ¶ added in v2.2.0
func (bm *SyncBucketManager) RemainingTime(id string) int64
RemainingTime returns the remaining time until the SyncBucket resets in milliseconds.
func (*SyncBucketManager) RemainingUses ¶ added in v2.2.0
func (bm *SyncBucketManager) RemainingUses(id string) int
RemainingUses returns the remaining uses until the SyncBucket is depleted, which may be negative.
func (*SyncBucketManager) Reset ¶ added in v2.2.0
func (bm *SyncBucketManager) Reset(id string)
Reset resets a SyncBucket's uses.
func (*SyncBucketManager) SaveFile ¶ added in v2.2.0
func (bm *SyncBucketManager) SaveFile(path string) error
SaveFile saves this SyncBucketManager to a file as JSON.