async

package
v0.0.0-...-69d9917 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: MIT, Unlicense Imports: 5 Imported by: 0

Documentation

Overview

Loader adapted from Egon's https://github.com/egonelbre/expgio.

Index

Constants

View Source
const DefaultMaxLoaded = 10

DefaultMaxLoaded is used when no max is specified.

Variables

This section is empty.

Functions

This section is empty.

Types

type DynamicWorkerPool

type DynamicWorkerPool struct {
	// Workers specifies the maximum allowed number of concurrent workers in
	// this pool. Defaults to NumCPU.
	Workers int64

	// once time initialization.
	sync.Once
	// contains filtered or unexported fields
}

DynamicWorkerPool implements a simple dynamic-sized worker pool that spins up a new worker per unit of work, until the maximum number of workers has been reached.

This pool will minimize idle memory as goroutines will die off once complete, but will incur the latency cost, such that it is, of spinning up goroutines on-the-fly.

Additionally, ordering of work is inconsistent with highly dynamic layouts.

func (*DynamicWorkerPool) Schedule

func (p *DynamicWorkerPool) Schedule(work func())

Schedule work to be executed by the available workers. This is a blocking call if all workers are busy.

Workers are limited by a buffer of semaphores. Each worker holds a semaphore for the duration of it's life and returns it before exiting.

type FixedWorkerPool

type FixedWorkerPool struct {
	// Workers specifies the number of concurrent workers in this pool.
	Workers int

	// once time initialization.
	sync.Once
	// contains filtered or unexported fields
}

FixedWorkerPool implements a simple fixed-size worker pool that lets go runtime schedule work atop some number of goroutines.

This pool will minimize goroutine latency at the cost of maintaining the configured number of goroutines throughout the lifetime of the pool.

func (*FixedWorkerPool) Schedule

func (p *FixedWorkerPool) Schedule(work func())

Schedule work to be executed by the available workers. This is a blocking call if all workers are busy.

type LoadFunc

type LoadFunc func(ctx context.Context) interface{}

LoadFunc function that performs the blocking load.

type Loader

type Loader struct {
	// Scheduler provides scheduling behaviour. Defaults to a sized worker pool.
	// The caller can provide a scheduler that implements the best strategy for
	// the their usecase.
	Scheduler Scheduler
	// MaxLoaded specifies the maximum number of resources to load before
	// de-allocating old resources.
	MaxLoaded int
	// contains filtered or unexported fields
}

Loader is an asynchronously loaded resource. Start and poll a resource with Schedule method. Track frames with Frame method to detect stale data. Respond to updates in event loop by selecting on Updated channel.

func (*Loader) Frame

func (l *Loader) Frame(gtx layout.Context, w layout.Widget) layout.Dimensions

Frame wraps a widget and tracks frame updates.

Typically you should wrap your entire UI so that each frame is counted. However, it is sufficient to wrap only the widget that expects to use the loader during it's layout.

A frame is currently updating if activeFrame < finishedFrame.

func (*Loader) Schedule

func (l *Loader) Schedule(tag Tag, load LoadFunc) Resource

Schedule a resource to be loaded asynchronously, returning a resource that will hold the loaded value at some point.

Schedule should be called per frame and the state of the resource checked accordingly.

The first call will queue up the load, subsequent calls will poll for the status.

func (*Loader) Shutdown

func (l *Loader) Shutdown()

Shutdown ends the background processing of the loader, if any is happening.

func (*Loader) Stats

func (l *Loader) Stats() LoaderStats

Stats reports runtime data about this loader.

func (*Loader) Updated

func (l *Loader) Updated() <-chan struct{}

Updated returns a channel that reports whether loader has been updated. Integrate this into gio event loop to, for example, invalidate the window.

case <-loader.Updated():
	w.Invalidate()

The channel returned from Updated will never close.

type LoaderStats

type LoaderStats struct {
	Lookup int
	Queued int
}

LoaderStats tracks some stats about the loader.

type Resource

type Resource struct {
	// State reports current state for this resource.
	State State
	// Value for the resource. Nil if not ready.
	Value interface{}
}

Resource is an async entity that can be in various states and potentially contain a value.

type Scheduler

type Scheduler interface {
	// Schedule a piece of work. This method is allowed to block.
	Schedule(func())
}

Scheduler schedules work according to some strategy. Implementations can implement the best way to distribute work for a given application.

TODO(jfm): context cancellation.

type State

type State byte

State that an async Resource can be in.

const (
	Queued State = iota
	Loading
	Loaded
)

type Tag

type Tag interface{}

Tag pointer used to identify a unique resource.

Jump to

Keyboard shortcuts

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