future

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package future defines Future, a value which is determined some time in future.

Future is designed for writing long-running program which doing some works repeatedly like crawlers. It's good to be used as a broadcaster, or as a param to task generator.

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
crawlerTask, pageFuture := downloadWebPage()
log.Print(task.Skip(
	task.Iter(
		crawlerTask,
		parseAndSavePage(pageFuture),
		task.Skip(
			notifyWebDash(pageFuture),
			cleanUpOldData(pageFuture),
		),
	).Timed(time.Minute).RetryN(3).Loop(),
	task.HTTPServer(myWebDash),
).Run(ctx))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Future

type Future[T any] struct {
	// contains filtered or unexported fields
}

Future represents a value will be resolved or rejected some time in future. The term "resolve" indicates the value is computed successfully.

Future implements [task.Task] so it's easier to wait for multiple futures.

err = task.Skip(future1, future2, future3).Run(ctx)
if err != nil {
	// one or more futures are rejected
}

func New

func New[T any]() (fut *Future[T], res func(T), rej func(error))

New creates a future fut, which can be resolved by res or rejected by rej.

func Reject

func Reject[T any](e error) *Future[T]

Reject creates an already rejected future.

func Resolve

func Resolve[T any](v T) *Future[T]

Resolve creates an already resolved future.

func (*Future[T]) Await

func (f *Future[T]) Await(ctx context.Context) (T, error)

Await is a helper which waits the value and retrieves it along with the error.

func (*Future[T]) Done

func (f *Future[T]) Done() <-chan struct{}

Done returns a channel which is closed when value is determined.

func (*Future[T]) Err

func (f *Future[T]) Err() error

Err waits the value and retrieves the error.

func (*Future[T]) Get

func (f *Future[T]) Get() T

Get waits the value and retrieves it.

func (*Future[T]) Run

func (f *Future[T]) Run(ctx context.Context) error

Run waits the value to be resolved or rejected. It also implements [task.Task].

Jump to

Keyboard shortcuts

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