pool

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: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckActivity

func CheckActivity(a *Activity) error

CheckActivity throws an error if any essential elements are missing from an Activity

func ConfigToModel

func ConfigToModel(config Config) *models.Config

ConfigToModel returns a pointer to a Config represented in the API's model

func CopyStore

func CopyStore(from, to *Store)

CopyStore copys pools, groups, secret, booking token duration and now function from one Store to another

func MakeClaims

func MakeClaims(mp *models.Permission) permission.Token

MakeClaims creates claims from the API's model.Permission

func NewStreamsFromModel

func NewStreamsFromModel(modelStreams []*models.Stream) map[string]*Stream

NewStreamsFromModel returns a map of streams from the API's model representation

func SingleStreamToModel

func SingleStreamToModel(s *Stream) *models.Stream

SingleStreamToModel returns a pointer to a Stream represented in the API's model

func SingleUIToModel

func SingleUIToModel(u *UI) *models.UserInterface

SingleUIToModel returns a pointer to a UI represented in the API's model

func StreamsToModel

func StreamsToModel(streams map[string]*Stream) []*models.Stream

StreamsToModel returns an array of pointers to Streams represented in the API's model

func UIsToModel

func UIsToModel(uis []*UI) []*models.UserInterface

UIsToModel returns an array of pointers to UI represented in the API's model

Types

type Activity

type Activity struct {
	*sync.RWMutex `json:"-"`
	Config        Config `json:"config"`
	Description   `json:"description"`
	ExpiresAt     int64              `json:"exp"`
	Streams       map[string]*Stream `json:"streams"`
	UI            []*UI              `json:"ui"`
}

Activity represents an individual activity that can be booked

func NewActivity

func NewActivity(name string, expires int64) *Activity

NewActivity returns a pointer to an activity that is populated with a name and expiry datetime.

func NewActivityFromModel

func NewActivityFromModel(ma *models.Activity) *Activity

NewActivityFromModel creates a new Activity from the API's model representation

func (*Activity) AddID

func (a *Activity) AddID() string

AddID sets the ID of the activity to a randomly generated UUID

func (*Activity) AddStream

func (a *Activity) AddStream(key string, stream *Stream)

AddStream adds a stream to the Activity

func (*Activity) AddUI

func (a *Activity) AddUI(ui *UI)

AddUI adds a UI to the activity

func (*Activity) ConvertToModel

func (a *Activity) ConvertToModel() *models.Activity

ConvertToModel returns a pointer to an Activity represented in the API's model

func (*Activity) GetID

func (a *Activity) GetID() string

GetID returns the ID string of the Activity

func (*Activity) SetID

func (a *Activity) SetID(id string)

SetID sets the ID of the Activity

func (*Activity) WithID

func (a *Activity) WithID(id string) *Activity

WithID adds an ID to the Activity

func (*Activity) WithNewRandomID

func (a *Activity) WithNewRandomID() *Activity

WithNewRandomID adds a new random ID to the activity

type Config

type Config struct {
	URL string `json:"url"`
}

Config represents a UI configuration file URL

func NewConfigFromModel

func NewConfigFromModel(mc *models.Config) *Config

NewConfigFromModel returns a pointer to a Config converted from the API's model

type Description

type Description struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Type string `json:"type"`
	DisplayInfo
}

Description represents an Activity Description

func NewDescription

func NewDescription(name string) *Description

NewDescription returns a pointer to a Description with given name and randomly generated UUID as ID

func NewDescriptionFromModel

func NewDescriptionFromModel(md *models.Description) *Description

NewDescriptionFromModel returns a pointer to a Description converted from the API's model

func (*Description) ConvertToModel

func (d *Description) ConvertToModel() *models.Description

ConvertToModel returns a pointer to the Description represented in the API's model

func (*Description) SetFurther

func (d *Description) SetFurther(item string)

SetFurther sets the URL for further information (in string format)

func (*Description) SetID

func (d *Description) SetID(item string)

SetID sets the ID of the Description

func (*Description) SetImage

func (d *Description) SetImage(item string)

SetImage sets the URL of the large/main image of the activity

func (*Description) SetLong

func (d *Description) SetLong(item string)

SetLong sets the long description field in the Description

func (*Description) SetShort

func (d *Description) SetShort(item string)

SetShort sets the short description field in the Descriptiom

func (*Description) SetThumb

func (d *Description) SetThumb(item string)

SetThumb sets the URL of the thumbnail image of the activity

func (*Description) SetType

func (d *Description) SetType(item string)

SetType sets the Type field in the Description

func (*Description) WithID

func (d *Description) WithID(id string) *Description

WithID sets the ID of the Description

type DisplayInfo

type DisplayInfo struct {
	Short   string `json:"short,omitempty"`
	Long    string `json:"long,omitempty"`
	Further string `json:"further,omitempty"`
	Thumb   string `json:"thumb,omitempty"`
	Image   string `json:"image,omitempty"`
}

DisplayInfo represents information about an Activity that is displayed to users

type Group

type Group struct {
	*sync.RWMutex `json:"-" yaml:"-"`
	Description   `json:"description"`
	Pools         []*Pool `json:"pools"`
}

Group represents a group of Pools

func NewGroup

func NewGroup(name string) *Group

NewGroup returns a pointer to a new Group with a name, and randomly generated ID

func NewGroupFromModel

func NewGroupFromModel(mg *models.Group) (*Group, error)

NewGroupFromModel returns a pointer to Group created from the API's model

func (*Group) AddPool

func (g *Group) AddPool(pool *Pool)

AddPool adds the pool to the existing list of pools, all existing pools are retained

func (*Group) AddPools

func (g *Group) AddPools(pools []*Pool)

AddPools adds more than one pool to the Group, retaining existing pools

func (*Group) DeletePool

func (g *Group) DeletePool(pool *Pool)

DeletePool removes a single pool from the Group, if it was present

func (*Group) GetID

func (g *Group) GetID() string

GetID returns the ID of the Group

func (*Group) GetPoolIDs

func (g *Group) GetPoolIDs() []string

GetPoolIDs returns an array of all Pool IDs in the Group

func (*Group) GetPools

func (g *Group) GetPools() []*Pool

GetPools returns the current list of pools in the Group

func (*Group) SetNewRandomID

func (g *Group) SetNewRandomID() *Group

SetNewRandomID sets a random UUID as the Group ID

func (*Group) SetPools

func (g *Group) SetPools(pools []*Pool)

SetPools replaces all pools in the group with the given array of pools

func (*Group) WithID

func (g *Group) WithID(id string) *Group

WithID sets the ID of the Group

func (*Group) WithPool

func (g *Group) WithPool(pool *Pool) *Group

WithPool adds a pool to a Group

func (*Group) WithPools

func (g *Group) WithPools(pools []*Pool) *Group

WithPools replaces all pools in the Group with the given array of pools

type Pool

type Pool struct {
	*sync.RWMutex `json:"-" yaml:"-"`
	Description   `json:"description"`
	Activities    map[string]*Activity `json:"activities"`
	Available     map[string]int64     `json:"available"`
	InUse         map[string]int64     `json:"inUse"`
	MinSession    uint64               `json:"minSession"`
	MaxSession    uint64               `json:"maxSession"`
	Now           func() int64         `json:"-" yaml:"-"`
}

Pool represents the booking status of the activities in a pool Note that each pool can have a different minSession / MaxSession duration but that users are limited to fixed maximum number of sessions they can book across the system to prevent users with access with more pools booking even more experiments simultaneously.

func NewPool

func NewPool(name string) *Pool

NewPool returns a pointer to a newly initialised, empty Pool with the given name

func (*Pool) ActivityExists

func (p *Pool) ActivityExists(id string) bool

ActivityExists checks whether an activity of the given ID exists in the pool (returns true if exists)

func (*Pool) ActivityInUse

func (p *Pool) ActivityInUse(id string) bool

ActivityInUse checks whether an activity with the given ID is currently in use (returns true if in use)

func (*Pool) ActivityNextAvailableTime

func (p *Pool) ActivityNextAvailableTime(id string) (int64, error)

ActivityNextAvailableTime returns the time that the activity of the given ID will be next available

func (*Pool) ActivityRequest

func (p *Pool) ActivityRequest(duration uint64, id string) error

ActivityRequest marks activity with given ID as being 'in use' until the time requested, but throws an error if the ID does not exist or is in-use already

func (*Pool) ActivityRequestAny

func (p *Pool) ActivityRequestAny(duration uint64) (string, error)

ActivityRequestAny returns the ID of a free activity that is available for the requested duration, and marks the activity as 'in use' until the time requested or throws an error if no free activities.

func (*Pool) ActivityWaitAny

func (p *Pool) ActivityWaitAny() (uint64, error)

ActivityWaitAny returns the wait time for an activity to come free that can be booked for one second or more.

func (*Pool) ActivityWaitDuration

func (p *Pool) ActivityWaitDuration(duration uint64) (uint64, error)

ActivityWaitDuration returns the wait time for an activity to come free that can be booked for the specified ActivityWaitDuration this covers the case when an activity might come free, but have only a short time left before it expires from the manifest

func (*Pool) AddActivity

func (p *Pool) AddActivity(activity *Activity) error

AddActivity adds a single Activity to a pool

func (*Pool) CountAvailable

func (p *Pool) CountAvailable() int

CountAvailable returns the number of activities available to book in the pool now

func (*Pool) CountInUse

func (p *Pool) CountInUse() int

CountInUse returns the number of activities in use now

func (*Pool) DeleteActivity

func (p *Pool) DeleteActivity(activity *Activity)

DeleteActivity removes a single Activity from the Pool

func (*Pool) GetActivityByID

func (p *Pool) GetActivityByID(id string) (*Activity, error)

GetActivityByID returns a pointer to the Activity of the given ID

func (*Pool) GetActivityIDs

func (p *Pool) GetActivityIDs() []string

GetActivityIDs returns an array containing the IDs of all activities in the Pool

func (*Pool) GetID

func (p *Pool) GetID() string

GetID returns the ID of the Pool

func (*Pool) GetMaxSession

func (p *Pool) GetMaxSession() uint64

GetMaxSession returns the maximum bookable session duration in seconds

func (*Pool) GetMinSession

func (p *Pool) GetMinSession() uint64

GetMinSession returns the minimum bookable session duration in seconds

func (*Pool) RemoveStaleEntries

func (p *Pool) RemoveStaleEntries()

RemoveStaleEntries tidies up any entries in the InUse list that have since expired

func (*Pool) SetMaxSesssion

func (p *Pool) SetMaxSesssion(duration uint64)

SetMaxSesssion sets the maximum session duration that can be booked by a user usually intended to match the maximum sustained session expected of a user and not set so long that a user can tie up equipment unduly (there is no cancel at the present time)

func (*Pool) SetMinSesssion

func (p *Pool) SetMinSesssion(duration uint64)

SetMinSesssion sets the minimum session duration that can be booked the minimum bookable duration is usually set by the checking routines run by the system

func (*Pool) WithDescription

func (p *Pool) WithDescription(d Description) *Pool

WithDescription adds a description to the Pool

func (*Pool) WithID

func (p *Pool) WithID(id string) *Pool

WithID sets the ID for the Pool

func (*Pool) WithMaxSesssion

func (p *Pool) WithMaxSesssion(duration uint64) *Pool

WithMaxSesssion sets the maximum session duration that can be booked by a user usually intended to match the maximum sustained session expected of a user and not set so long that a user can tie up equipment unduly (there is no cancel at the present time)

func (*Pool) WithMinSesssion

func (p *Pool) WithMinSesssion(duration uint64) *Pool

WithMinSesssion sets the minimum session duration that can be booked the minimum bookable duration is usually set by the checking routines run by the system

func (*Pool) WithNow

func (p *Pool) WithNow(now func() int64) *Pool

WithNow sets the function which reports the current time (useful in testing)

type Store

type Store struct {
	*sync.RWMutex `json:"-" yaml:"-"`

	// Groups represent non-exclusive combinations of pools
	Groups map[string]*Group `json:"groups"`

	// Pools maps all pools in the store
	Pools map[string]*Pool `json:"pools"`

	// Secret for generating tokens - assume one PoolStore per relay
	Secret []byte `json:"secret"`

	// How long to grant booking tokens for
	BookingTokenDuration int64 `json:"bookingTokenDuration"`

	// Now is a function for getting the time - useful for mocking in test
	Now func() int64 `json:"-" yaml:"-"`
}

Store represents Activities stored according to Pool and Group

func ImportAll

func ImportAll(b []byte) (*Store, error)

ImportAll imports a Store from a serialised JSON representation in []byte format It creates all necessary mutexes so the store can operate properly after the import

func NewStore

func NewStore() *Store

NewStore creates a new Store with default settings

func (*Store) AddGroup

func (p *Store) AddGroup(group *Group)

AddGroup adds a group to the Store

func (*Store) AddPool

func (p *Store) AddPool(pool *Pool)

AddPool adds a pool to the Store (but does not associate it with a group)

func (*Store) DeleteGroup

func (p *Store) DeleteGroup(group *Group)

DeleteGroup removes a group from the store

func (*Store) DeletePool

func (p *Store) DeletePool(pool *Pool)

DeletePool removes a pool from the store

func (*Store) ExportAll

func (p *Store) ExportAll() ([]byte, error)

ExportAll marshalls the Store to JSON and returns the []byte representation note that elements such as mutexes cannot be serialised and are omitted

func (*Store) GetAllGroupsCount

func (p *Store) GetAllGroupsCount() int

GetAllGroupsCount returns a count of how many groups there are in the store

func (*Store) GetAllPoolCount

func (p *Store) GetAllPoolCount() int

GetAllPoolCount returns a count of how many pools are in the store

func (*Store) GetAllPoolIDs

func (p *Store) GetAllPoolIDs() []string

GetAllPoolIDs returns an array containing all the IDs of pools in the store

func (*Store) GetAllPools

func (p *Store) GetAllPools() []*Pool

GetAllPools returns all pools in the Store

func (*Store) GetAvailableActivitiesCount

func (p *Store) GetAvailableActivitiesCount() int

GetAvailableActivitiesCount returns a count of how many activities are available

func (*Store) GetGroupByID

func (p *Store) GetGroupByID(id string) (*Group, error)

GetGroupByID returns a pointer to the Group with the given ID, or an error

func (*Store) GetGroupsByName

func (p *Store) GetGroupsByName(name string) ([]*Group, error)

GetGroupsByName returns all groups with the given name

func (*Store) GetGroupsByNamePrefix

func (p *Store) GetGroupsByNamePrefix(prefix string) ([]*Group, error)

GetGroupsByNamePrefix returns groups with a given string at the start of their name, or an error

func (*Store) GetPoolByID

func (p *Store) GetPoolByID(id string) (*Pool, error)

GetPoolByID returns a pointer to a Pool with the given ID, or an error

func (*Store) GetPoolsByName

func (p *Store) GetPoolsByName(name string) ([]*Pool, error)

GetPoolsByName returns all pools with a given name

func (*Store) GetPoolsByNamePrefix

func (p *Store) GetPoolsByNamePrefix(prefix string) ([]*Pool, error)

GetPoolsByNamePrefix returns all pools with names starting with a given string

func (*Store) GetSecret

func (p *Store) GetSecret() []byte

GetSecret returns the secret used to check tokens

func (*Store) GetTime

func (p *Store) GetTime() int64

GetTime allows flexibility in choosing clock source default is internal clock of the server

func (*Store) PostImportEssential

func (p *Store) PostImportEssential()

PostImportEssential sets up mutexes and Now() functions

func (*Store) PostImportSetNow

func (p *Store) PostImportSetNow(now func() int64)

PostImportSetNow applies a custom Now() func to the poolstore and all pools - useful for mocking time in tests which involve import/export

func (*Store) Reset

func (p *Store) Reset()

Reset removes all pools and groups but does not change min/max session durations

func (*Store) SetSecret

func (p *Store) SetSecret(secret string)

SetSecret sets the secret used to check token validity and sign tokens for streaming

func (*Store) WithBookingTokenDuration

func (p *Store) WithBookingTokenDuration(duration int64) *Store

WithBookingTokenDuration sets the duration for which booking tokens are valid

func (*Store) WithNow

func (p *Store) WithNow(now func() int64) *Store

WithNow sets the function that returns the datetime in seconds at the current time (useful for testing)

func (*Store) WithSecret

func (p *Store) WithSecret(secret string) *Store

WithSecret sets the secret used by the booking store

type Stream

type Stream struct {
	*sync.RWMutex `json:"-"`

	// For is the key in the UI's URL in which the client puts
	// the relay (wss) address and code after getting them
	// from the relay
	For string `json:"for,omitempty"`

	// URL of the relay access point for this stream
	URL string `json:"url"`

	// signed bearer token for accessing the stream
	// submit token in the header
	Token string `json:"token,omitempty"`

	// Verb is the HTTP method, typically post
	Verb string `json:"verb,omitempty"`

	// Permission is a prototype for the permission token that the booking system
	// generates and puts into the Token field
	Permission permission.Token `json:"permission,omitempty"`
}

Stream represents a data or video stream from a relay typically accessed via POST with bearer token

func NewSingleStreamFromModel

func NewSingleStreamFromModel(ms *models.Stream) *Stream

NewSingleStreamFromModel returns a pointer to a Stream created from the API's model representation

func NewStream

func NewStream(url string) *Stream

NewStream returns a pointer to a new empty Stream

func (*Stream) GetPermission

func (s *Stream) GetPermission() permission.Token

GetPermission returns the permission (token) of the Stream

func (*Stream) SetPermission

func (s *Stream) SetPermission(p permission.Token)

SetPermission sets the permission (token) for the Stream

func (*Stream) WithPermission

func (s *Stream) WithPermission(p permission.Token) *Stream

WithPermission adds a permission to the Stream

type UI

type UI struct {
	// URL with moustache {{key}} templating for stream connections
	Description     `json:"description"`
	URL             string   `json:"url"`
	StreamsRequired []string `json:"streamsRequired"`
}

UI represents a UI that can be used with an Activity

func NewSingleUIFromModel

func NewSingleUIFromModel(mui *models.UserInterface) *UI

NewSingleUIFromModel returns a pointer to a UI created from the API's model representation

func NewUI

func NewUI(url string) *UI

NewUI returns a pointer to a UI with the given URL

func NewUIsFromModel

func NewUIsFromModel(modelUIs []*models.UserInterface) []*UI

NewUIsFromModel creates an array of pointers to UI from the API's model representation

func (*UI) WithDescription

func (u *UI) WithDescription(d Description) *UI

WithDescription adds a description to the UI

func (*UI) WithStreamsRequired

func (u *UI) WithStreamsRequired(names []string) *UI

WithStreamsRequired adds required streams to the UI

Jump to

Keyboard shortcuts

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