rl

package
v2.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2022 License: MIT Imports: 5 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	Uses       int
	Limit      int
	ResetAfter time.Duration
	NextReset  time.Time
}

func NewBucket

func NewBucket(limit int, resetAfter time.Duration) *Bucket

NewBucket creates a new Bucket.

func (*Bucket) CanDraw

func (b *Bucket) CanDraw(amount int) bool

CanDraw checks if a certain number of tokens can be drawn.

func (*Bucket) Draw

func (b *Bucket) Draw(amount int) bool

Draw draws tokens from a bucket, returning false and doing nothing if there are not enough.

func (*Bucket) DrawMax

func (b *Bucket) DrawMax(amount int) int

DrawMax draws as many tokens from as possible up to `amount`, returning the number of drawn tokens.

func (*Bucket) ForceDraw

func (b *Bucket) 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 (*Bucket) RemainingTime

func (b *Bucket) RemainingTime() int64

RemainingTime returns the remaining time until the next reset, in milliseconds.

func (*Bucket) RemainingUses

func (b *Bucket) RemainingUses() int

RemainingUses returns the remaining uses until the bucket is depleted, which may be negative.

func (*Bucket) Reset

func (b *Bucket) Reset()

Reset resets this Bucket's uses.

type BucketManager

type BucketManager struct {
	Buckets    map[string]*Bucket
	Limit      int
	ResetAfter time.Duration
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL