controller

package
v0.0.0-...-9994f1b Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: BSD-3-Clause Imports: 25 Imported by: 0

Documentation

Overview

Package controller contains the implementation to interact and manipulate the data models of the Device Lease service.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BulkLeaseDevices

BulkLeaseDevices leases multiple Devices specified by the request.

The function executes as a transaction. It attempts to create lease records on available Devices. Then it updates the Devices' state to LEASED and publishes to a PubSub stream. The transaction is then committed.

All calls to this RPC return 200 (except for panics), with global errors returned in the bulk response, or individual errors per-device returned in the list of per-device responses.

func CheckExtensionIdempotency

func CheckExtensionIdempotency(ctx context.Context, db *sql.DB, idemKey string) (*api.ExtendLeaseResponse, error)

CheckExtensionIdempotency checks if there is a extend request with the same idempotency key.

If there is a duplicate request, it will return the request. If there is no record, it will return an empty response and no error.

func CheckLeaseIdempotency

func CheckLeaseIdempotency(ctx context.Context, db *sql.DB, idemKey string) (*api.LeaseDeviceResponse, error)

CheckLeaseIdempotency checks if there is a record with the same idempotency key.

If there is an unexpired record, it will return the record. If it is expired, it will error. If there is no record, it will return an empty response and no error.

func ExpireLeases

func ExpireLeases(ctx context.Context, db *sql.DB, opts *ExpirerOpts) error

ExpireLeases marks expired leases as released and releases Devices in the DB.

func ExtendLease

func ExtendLease(ctx context.Context, db *sql.DB, r *api.ExtendLeaseRequest) (*api.ExtendLeaseResponse, error)

ExtendLease attempts to extend the lease on a device.

ExtendLease checks the requested lease to verify that it is unexpired. If unexpired, it will extend the lease by the requested duration. This maintains the leased state on a device.

func ExtractSingleValuedDimension

func ExtractSingleValuedDimension(ctx context.Context, dims map[string]*api.HardwareRequirements_LabelValues, key string) (string, error)

ExtractSingleValuedDimension extracts one specified dimension from a dimension slice.

func GetDevice

func GetDevice(ctx context.Context, db *sql.DB, idType model.DeviceIDType, deviceID string) (*api.Device, error)

GetDevice gets a Device from the database based on a deviceID.

func IsDeviceAvailable

func IsDeviceAvailable(ctx context.Context, state string) bool

IsDeviceAvailable checks if a device state is available.

func LeaseDevice

func LeaseDevice(ctx context.Context, db *sql.DB, r *api.LeaseDeviceRequest, deviceID string, idType model.DeviceIDType) (*api.LeaseDeviceResponse, error)

LeaseDevice leases a device specified by the request.

The function executes as a transaction. It attempts to create a lease record with an available device. Then it updates the Device's state to LEASED and publishes to a PubSub stream. The transaction is then committed.

func ListDevices

func ListDevices(ctx context.Context, db *sql.DB, r *api.ListDevicesRequest) (*api.ListDevicesResponse, error)

ListDevices lists Devices from the db based on filters.

func PublishDeviceEvent

func PublishDeviceEvent(ctx context.Context, psClient external.PubSubClient, device *model.Device) error

PublishDeviceEvent takes a Device and publishes an event to PubSub.

func ReleaseDevice

ReleaseDevice releases the leased device.

ReleaseDevice takes a lease ID and releases the device associated. In a transaction, the RPC will update the lease and set the device to be available.

func SendNotifications

func SendNotifications(
	ctx context.Context,
	db *sql.DB,
	psClient external.PubSubClient,
	opts *NotifierOpts,
)

SendNotifications selects Devices for which no notifications have been sent since last_updated_time. It then sets up a worker pool to start publishing a message per device to Pub/Sub. Successfully sending results in the Device row updates to note the last notification time. The time used for notification is when the function is first called. This avoids missing updates that happen as we go this batch of updates. These final updates are done in batches with a max wait time between updates.

Types

type ExpirerOpts

type ExpirerOpts struct {
	ExpirationWorkersN *int
}

ExpirerOpts struct holds configuration options for the Expirer service

type NotifierOpts

type NotifierOpts struct {
	PublishWorkersN   *int
	UpdateBatchSize   *int
	MaxUpdateWaitTime time.Duration
}

NotifierOpts struct holds configuration options for the Notifier service

Jump to

Keyboard shortcuts

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