futures

package
v0.0.0-...-28d43d2 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package futures allow adding incomplete computations in contexts and templates. This allows to start the rendering of the template before all the other operations (for example, database queries) had been finished.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Read

func Read(f *Future) interface{}

Read reads the Future value. Is intended to be used on templates.

Types

type Expirable

type Expirable struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Expirable manages the retrieval and recalculation of a value that expires after some time

func NewExpirable

func NewExpirable(timeout time.Duration, generator func() interface{}) *Expirable

NewExpirable creates a new Expirable with the given TTL and generated with the given generator

func (*Expirable) Read

func (e *Expirable) Read() interface{}

Read returns the current value of the Expirable, and if it has expired then it recalculates the value using the default generator

func (*Expirable) ReadFunc

func (e *Expirable) ReadFunc(f func() interface{}) interface{}

ReadFunc returns the current value of the Expirable, and if it has expired then it recalculates the value using the given generator

type ExpirablePool

type ExpirablePool struct {
	Expirables []*Expirable
}

ExpirablePool manages the retrieval and recalculation of a pool of values that expires after some time.

func NewExpirablePool

func NewExpirablePool(timeout time.Duration, size int, generator func() interface{}) *ExpirablePool

NewExpirablePool creates a pool of several Expirable, that could be requested concurrently avoiding a bit the overhead of locking a single Expirable for all the requests. Note that potentially the returned values could go out of synchronization.

func (ExpirablePool) Pick

func (p ExpirablePool) Pick() *Expirable

Pick selects an Expirable at random from the pool

func (*ExpirablePool) Read

func (p *ExpirablePool) Read() interface{}

Read reads the value from an Expirable picked at random, and if it has expired then it recalculates the value using the default generator

func (*ExpirablePool) ReadFunc

func (p *ExpirablePool) ReadFunc(f func() interface{}) interface{}

ReadFunc reads the value from an Expirable picked at random, and if it has expired then it recalculates the value using the given generator

type Future

type Future struct {
	// contains filtered or unexported fields
}

Future represents an maybe incomplete operation, that is being processed

func NewFuture

func NewFuture(input chan interface{}) *Future

NewFuture creates a Future from an input channel

func NewFutureFunc

func NewFutureFunc(f func() interface{}) *Future

NewFutureFunc creates a Future from a function that returns an interface{}

func (*Future) Read

func (f *Future) Read() interface{}

Read blocks until the computation finish, and then returns the value.

Jump to

Keyboard shortcuts

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