limit

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2023 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package limit supports enforcing the maximum number of simultaneous time-limited resources by user

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Confirm

func Confirm(confirm chan<- struct{}) func()

Confirm confirms a provisional booking

Types

type Limit

type Limit struct {
	*sync.Mutex

	Now func() int64
	// contains filtered or unexported fields
}

Limit represents a limit store

func New

func New() *Limit

New creates a new Limit with optional hourly flushing to avoid memory leakage

func (*Limit) FlushAll

func (l *Limit) FlushAll()

FlushAll removes all stale entries

func (*Limit) GetSessions

func (l *Limit) GetSessions(user string) int

GetSessions returns the current sessions for a user

func (*Limit) ProvisionalRequest

func (l *Limit) ProvisionalRequest(who string, exp int64) (func(), error)

ProvisionalRequest checks if a user has spare capacity within their limit adding their request provisionally (with a delayed autodelete) if approved. The autodelete can be cancelled with the returned CancelFunc, in order to make the booking. If there is no quota left, an error is returned

func (*Limit) Request

func (l *Limit) Request(who string, exp int64) bool

Request makes a provisional request, and immediately confirms it. Used for testing.

func (*Limit) WithFlush

func (l *Limit) WithFlush(ctx context.Context, interval time.Duration) *Limit

WithFlush adds a regular flushing action that occurs every interval

func (*Limit) WithMax

func (l *Limit) WithMax(max int) *Limit

WithMax sets the maximum number of bookings per user

func (*Limit) WithNow

func (l *Limit) WithNow(now func() int64) *Limit

WithNow sets the function used to obtain the current time (required for testing)

func (*Limit) WithProvisionalPeriod

func (l *Limit) WithProvisionalPeriod(interval time.Duration) *Limit

WithProvisionalPeriod specifies the provisional period for provisional bookings

Jump to

Keyboard shortcuts

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