bookingstore

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2022 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyStore

func CopyStore(from, to *Limit)

CopyStore moves the contents of one Limit(store) to another

Types

type Limit

type Limit struct {
	*sync.Mutex `json:"-"`

	// activities represents users' activities
	// it maps each user id to a map of their sessionIDs and activities
	Activities map[string]map[string]*models.Activity `json:"activities"`

	// sessions represents the expiry time of current sessions
	// it maps user id to map of session ids and expiry times
	Sessions map[string]map[string]int64 `json:"sessions"`

	// activityBySession represents all activities
	// use for reverse look up activity by sessionID
	ActivityBySession map[string]*models.Activity `json:"activityBySession"`

	// userBySession represents all users
	// sue for reverse look up user by sessionID
	UserBySession map[string]string `json:"userBySession"`

	// lastFlush represents the last time a FlushAll() was done
	// There is no point in flushing more often than the clock granularity
	// of one second
	LastFlush *int64 `json:"lastFlush"`

	// lastBookingEnds represents the expiry time of the longest running session
	// when new bookings are confirmed, this is updated
	LastBookingEnds *int64 `json:"lastBookingEnds"`

	// max represents the per-user maximum number of concurrent sessions
	// there is no limit on historical usage within this package
	Max *int `json:"max"`

	// lockBookings represents whether there is a lock on bookings
	// locking bookings prevents new bookings, letting
	// existing bookings continue
	Locked *bool `json:"lockBookings"`

	// Message represents a status comment that e.g.
	// useful for letting users know why the
	// lock is applied, or when bookings will be
	// locked/unlocked next
	Message *string `json:"message"`

	// flusInterval represents the time delay between automated FlushAll calls
	// these are intended to prevent inadvertent memory leakage
	FlushInterval *time.Duration `json:"flushInterval"`

	// provisionalPeriod represents the time that a booking can remain unresolved
	// into a cancellation or a confirmation with an activity
	// failing to either cancel or confirm, results in the user being treated
	// as if they confirmed the booking, in terms of quota, but they cannot
	// access the activity details again.
	ProvisionalPeriod *time.Duration `json:"provisionalPeriod"`

	// Now represents a function that returns the current time (mockable for non-timer parts of
	// of the package)
	Now func() int64 `json:"-"`
	// contains filtered or unexported fields
}

Limit represents allocation of limited resources

func ImportAll

func ImportAll(l *Limit, b []byte) (*Limit, error)

ImportAll takes the booking store marshalled into b as our new bookingstore manage pointer scope by making this return the pointer for the calling function to replace the poolstore pointer with

func New

func New(ctx context.Context) *Limit

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

func (*Limit) ExportAll

func (l *Limit) ExportAll() ([]byte, error)

ExportAll returns the store marshalled into a json []byte

func (*Limit) FlushAll

func (l *Limit) FlushAll()

FlushAll removes all stale entries from all maps

func (*Limit) GetActivityFromSessionID

func (l *Limit) GetActivityFromSessionID(sid string) (*models.Activity, error)

GetActivityFromSessionID allows an activity to be retrieved even if the userID has been lost -> admin scope only!

func (*Limit) GetAllActivities

func (l *Limit) GetAllActivities() map[string]*models.Activity

GetAllActivities returns a map of all activities by session This must be for admin use only - else anyone can use anyone else's sessions - activities contain credentials

func (*Limit) GetAllActivitiesCount

func (l *Limit) GetAllActivitiesCount() int

GetAllActivitiesCount returns a count of how many activities there are

func (*Limit) GetAllSessionCount

func (l *Limit) GetAllSessionCount() int

GetAllSessionCount is primarily an admin function for a bit of dashboard eye-candy, but there is no reason that users can't see this too

func (*Limit) GetLastBookingEnds

func (l *Limit) GetLastBookingEnds() int64

GetLastBookingEnds is an admin function to help figure out how long until the system is free of booked sessions e.g. after disabling new bookings, existing bookings will still have some time to run

func (*Limit) GetLockBookings

func (l *Limit) GetLockBookings() bool

GetLockBookings returns bookings are locked (true is locked)

func (*Limit) GetMax

func (l *Limit) GetMax() int

GetMax returns Limit.Max

func (*Limit) GetMessage

func (l *Limit) GetMessage() string

GetMessage returns the message of the day

func (*Limit) GetUserActivities

func (l *Limit) GetUserActivities(user string) (map[string]*models.Activity, error)

GetUserActivities provides pointers to all of a users activities, so that they can be provided with their current bookings (intended for both user and admin scope)

func (*Limit) GetUserSessionCount

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

GetUserSessionCount returns number of current sessions a user has

func (*Limit) LockBookings

func (l *Limit) LockBookings()

LockBookings prevents new bookings

func (*Limit) PostImportEssential

func (l *Limit) PostImportEssential(ctx context.Context)

PostImportEssential sets up mutexes and Now() functions Assume the original bookingstore context was cancelled so as to stop the registerhandler and flush....

func (*Limit) ProvisionalRequest

func (l *Limit) ProvisionalRequest(userID string, exp int64) (func(), func(activity *models.Activity), string, error)

ProvisionalRequest checks if a user has spare capacity within their limit If so, a provisional booking is made, and cancel and confirm functions are returned along with the sessionID The session should either be cancelled, or confirmed with argument to models.Activity so that booking details can be retrieved again later in the booking if need be, from this store. If there is no quota left, or new bookings are suspended, an error is returned

func (*Limit) Request

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

Request is primarily for testing purposes but may be useful in some other context so remains exported

func (*Limit) Reset

func (l *Limit) Reset()

Reset the limit store to its original default state (loses all data)

func (*Limit) SetMessage

func (l *Limit) SetMessage(msg string)

SetMessage sets the message of the day

func (*Limit) SetNow

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

SetNow sets the function used to get the current time

func (*Limit) UnlockBookings

func (l *Limit) UnlockBookings()

UnlockBookings allows new bookings

func (*Limit) WithFlush

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

WithFlush adds a periodic flush to avoid memory leakage Use this in production

func (*Limit) WithMax

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

WithMax sets the maximum number of concurrent sessions a user can have

func (*Limit) WithNow

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

WithNow sets the time function, useful for testing basic functionality but does not affect anything running off timers

func (*Limit) WithProvisionalPeriod

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

WithProvisionalPeriod sets the period within which a booking can be cancelled, or confirmed This is used to prevent leakage of the autoDelete goroutine

Jump to

Keyboard shortcuts

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