twoface

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: Unlicense Imports: 9 Imported by: 0

README

twoface

Higher abstractions of concurrency "primitives" to make working with concurrent and/or distributed systems easier.

context

A wrapper around native go context adding more boilerplate for ergonomics.

job

A primitive interface that allows scheduling onto pools.

Comes with a built in RetrieableJob implementation type that uses retries with gradual backoff.

pool

A goroutine worker pool that is combined with scaler.

scaler

Auto scaling and load balancing for goroutine pools.

worker

A wrapper that exposes worker goroutines as io.ReadWriteCloser types.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewScaler

func NewScaler(pool *Pool)

Types

type Context

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

Context is a wrapper around native the Go context type, while adding functionality to improve developer ergonomics.

func NewContext

func NewContext() *Context

NewContext constructs a twoface.Context.

func (*Context) Close

func (ctx *Context) Close() error

Close implements the io.Closer interface.

func (*Context) Deadline

func (ctx *Context) Deadline() (deadline time.Time, ok bool)

Deadline ...

func (*Context) Done

func (ctx *Context) Done() <-chan struct{}

Done ...

func (*Context) Err

func (ctx *Context) Err() error

Err ...

func (*Context) Error

func (ctx *Context) Error() string

Error implements Go's native error interface and wraps errnie around it to provide more context and more flexible output.

func (*Context) Read

func (ctx *Context) Read(p []byte) (n int, err error)

Read implements the io.Reader interface.

func (*Context) Root

func (ctx *Context) Root() context.Context

func (*Context) Value

func (ctx *Context) Value(key any, val any) any

Value ...

func (*Context) WG

func (ctx *Context) WG(idx int, val int) *sync.WaitGroup

func (*Context) Wait

func (ctx *Context) Wait(idx int) (err error)

func (*Context) Write

func (ctx *Context) Write(p []byte) (n int, err error)

Write implements the io.Writer interface.

type Fibonacci

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

Fibonacci is a RetryStategy that retries a function n times with a Fibonacci interval in seconds between retries.

func (*Fibonacci) Do

func (strategy *Fibonacci) Do(fn Job) error

type Job

type Job interface {
	Do() error
}

Job is an interface any type can implement if they want to be able to use the generics goroutine pool.

func NewJob

func NewJob(jobType Job) Job

NewJob is a conveniance method to convert any incoming structured type to a Job interface such that they can get onto the worker pools.

func NewRetriableJob

func NewRetriableJob(ctx Context, fn Job, tries int) Job

type Pool

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

Pool is a set of Worker types, each running their own (pre-warmed) goroutine. Any object that implements the Job interface is able to schedule work on the worker pool, which keeps the amount of goroutines in check, while still being able to benefit from high concurrency in all kinds of scenarios.

func NewPool

func NewPool(ctx *Context) *Pool

NewPool instantiates a worker pool with bound size of maxWorkers, taking in a Context type to be able to cleanly cancel all of the sub processes it starts.

func (*Pool) Do

func (pool *Pool) Do(jobType Job)

Do is the entry point for new jobs that want to be scheduled onto the worker pool.

func (*Pool) Run

func (pool *Pool) Run() *Pool

Run the workers, after creating and assigning them to the pool.

type RetriableJob

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

RetriableJob provides boilerplate for quickly building jobs that retry based on a backoff delay strategy.

func (RetriableJob) Do

func (job RetriableJob) Do() error

Do the job and retry x amount of times when needed.

type Retrier

type Retrier interface {
	Do(Job) error
}

Retrier is an interface that can be implemented by any object that wants to schedule itself onto a worker pool and be retried under certain conditions.

func NewFibonacci

func NewFibonacci(max int) Retrier

func NewRetrier

func NewRetrier(retrierType Retrier) Retrier

type Scaler

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

Scaler is a process that evaluates the resource load of a Pool and adds or removes Workers according to its opinion about how to divide the machine resources available.

type Worker

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

Worker wraps a concurrent process that is able to process Job types scheduled onto a Pool.

func NewWorker

func NewWorker(pool *Pool) *Worker

func (*Worker) Close

func (worker *Worker) Close() error

func (*Worker) Read

func (worker *Worker) Read(p []byte) (n int, err error)

func (*Worker) Write

func (worker *Worker) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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