utils

package
v0.0.0-...-47b5856 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 17 Imported by: 16

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllEqual

func AllEqual[T comparable](elems ...T) bool

AllEqual returns true iff all the provided elements are equal to each other.

func BatchSplit

func BatchSplit[T any](list []T, max int) (out [][]T, err error)

BatchSplit splits an slices into an slices of slicess with a maximum length

func Context deprecated

func Context(t *testing.T) context.Context

Deprecated: use tests.Context

func ContextFromChan

func ContextFromChan(chStop <-chan struct{}) (context.Context, context.CancelFunc)

ContextFromChan creates a context that finishes when the provided channel receives or is closed. When channel closes, the ctx.Err() will always be context.Canceled NOTE: Spins up a goroutine that exits on cancellation. REMEMBER TO CALL CANCEL OTHERWISE IT CAN LEAD TO MEMORY LEAKS

func ContextWithDeadlineFn

func ContextWithDeadlineFn(ctx context.Context, deadlineFn func(orig time.Time) time.Time) (context.Context, context.CancelFunc)

ContextWithDeadlineFn returns a copy of the parent context with the deadline modified by deadlineFn. deadlineFn will only be called if the parent has a deadline. The new deadline must be sooner than the old to have an effect.

func GetRandomPort

func GetRandomPort() string

func HashReport

func HashReport(ctx types.ReportContext, r types.Report) ([]byte, error)

HashReport returns a report digest using SHA256 hash.

func IsPortOpen

func IsPortOpen(t *testing.T, port string) bool

func IsZero

func IsZero[C comparable](val C) bool

func JustError

func JustError(_ interface{}, err error) error

JustError takes a tuple and returns the last entry, the error.

func MustRandomPort

func MustRandomPort(t *testing.T) string

func RawReportContext

func RawReportContext(repctx types.ReportContext) [3][32]byte

RawReportContext is a copy of evmutil.RawReportContext to avoid importing go-ethereum. github.com/smartcontractkit/libocr/offchainreporting2plus/chains/evmutil#RawReportContext

func WaitGroupChan

func WaitGroupChan(wg *sync.WaitGroup) <-chan struct{}

WaitGroupChan creates a channel that closes when the provided sync.WaitGroup is done.

func WithJitter

func WithJitter(d time.Duration) time.Duration

WithJitter adds +/- 10% to a duration

func WrapIfError

func WrapIfError(err *error, msg string)

WrapIfError decorates an error with the given message. It is intended to be used with `defer` statements, like so:

func SomeFunction() (err error) {
    defer WrapIfError(&err, "error in SomeFunction:")

    ...
}

Types

type DependentAwaiter

type DependentAwaiter interface {
	AwaitDependents() <-chan struct{}
	AddDependents(n int)
	DependentReady()
}

DependentAwaiter contains Dependent funcs

func NewDependentAwaiter

func NewDependentAwaiter() DependentAwaiter

NewDependentAwaiter creates a new DependentAwaiter

type Duration deprecated

type Duration = config.Duration

Deprecated: use config.Duration

func MustNewDuration deprecated

func MustNewDuration(d time.Duration) *Duration

Deprecated: use config.MustNewDuration

func NewDuration deprecated

func NewDuration(d time.Duration) (Duration, error)

Deprecated: use config.NewDuration

type LazyLoad

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

func NewLazyLoad

func NewLazyLoad[T any](f func() (T, error)) *LazyLoad[T]

func (*LazyLoad[T]) Get

func (l *LazyLoad[T]) Get() (out T, err error)

func (*LazyLoad[T]) Reset

func (l *LazyLoad[T]) Reset()

type SleeperTask

type SleeperTask struct {
	services.StateMachine
	// contains filtered or unexported fields
}

SleeperTask represents a task that waits in the background to process some work.

func NewSleeperTask

func NewSleeperTask(worker Worker) *SleeperTask

NewSleeperTask takes a worker and returns a SleeperTask.

SleeperTask is guaranteed to call Work on the worker at least once for every WakeUp call. If the Worker is busy when WakeUp is called, the Worker will be called again immediately after it is finished. For this reason you should take care to make sure that Worker is idempotent. WakeUp does not block.

func (*SleeperTask) Stop

func (s *SleeperTask) Stop() error

Stop stops the SleeperTask

func (*SleeperTask) WakeUp

func (s *SleeperTask) WakeUp()

WakeUp wakes up the sleeper task, asking it to execute its Worker.

func (*SleeperTask) WakeUpIfStarted

func (s *SleeperTask) WakeUpIfStarted()

func (*SleeperTask) WorkDone

func (s *SleeperTask) WorkDone() <-chan struct{}

WorkDone isn't part of the SleeperTask interface, but can be useful in tests to assert that the work has been done.

type StartStopOnce

type StartStopOnce = services.StateMachine

StartStopOnce can be embedded in a struct to help implement types.Service. Deprecated: use services.StateMachine

type Subprocesses

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

Subprocesses is an abstraction over the following pattern of sync.WaitGroup:

var wg sync.Subprocesses
wg.Add(1)
go func() {
defer wg.Done()
...
}()

Which becomes:

var subs utils.Subprocesses
subs.Go(func() {
...
})

Note that it's important to not call Subprocesses.Wait() when there are no `Go()`ed functions in progress. This will panic. There are two cases when this can happen:

  1. all the `Go()`ed functions started before the call to `Wait()` have already returned, maybe because a system-wide error or an already cancelled context.
  2. Wait() gets called before any function is executed with `Go()`.

Reusing a Subprocesses instance is discouraged. See mode details here https://pkg.go.dev/sync#WaitGroup.Add

func (*Subprocesses) Go

func (s *Subprocesses) Go(f func())

Go calls the given function in a new goroutine.

func (*Subprocesses) Wait

func (s *Subprocesses) Wait()

Wait blocks until all function calls from the Go method have returned.

type URL deprecated

type URL = config.URL

Deprecated: use config.URL

func MustParseURL deprecated

func MustParseURL(s string) *URL

Deprecated: use config.MustParseURL

func ParseURL deprecated

func ParseURL(s string) (*URL, error)

Deprecated: use config.ParseURL

type Worker

type Worker interface {
	Work()
	Name() string
}

Worker is a simple interface that represents some work to do repeatedly

func SleeperFuncTask

func SleeperFuncTask(work func(), name string) Worker

SleeperFuncTask returns a Worker to execute the given work function.

Directories

Path Synopsis
Package bigmath compensates for awkward big.Int API.
Package bigmath compensates for awkward big.Int API.

Jump to

Keyboard shortcuts

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