utils

package
v0.0.0-...-3f0f60f Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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

func Context(t *testing.T) context.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 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/GoPlugin/pluginV2Lib/libocr/offchainreporting2plus/chains/evmutil#RawReportContext

func WithJitter

func WithJitter(d time.Duration) time.Duration

WithJitter adds +/- 10% to a duration

Types

type Duration

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

Duration is a non-negative time duration.

func MustNewDuration

func MustNewDuration(d time.Duration) *Duration

func NewDuration

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

func (Duration) Duration

func (d Duration) Duration() time.Duration

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Duration) MarshalText

func (d Duration) MarshalText() ([]byte, error)

MarshalText implements the text.Marshaler interface.

func (*Duration) Scan

func (d *Duration) Scan(v interface{}) (err error)

func (Duration) String

func (d Duration) String() string

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(input []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(input []byte) error

UnmarshalText implements the text.Unmarshaler interface.

func (Duration) Value

func (d Duration) Value() (driver.Value, error)

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 StartStopOnce

type StartStopOnce struct {
	sync.RWMutex // lock is held during startup/shutdown, RLock is held while executing functions dependent on a particular state
	// contains filtered or unexported fields
}

StartStopOnce can be embedded in a struct to help implement types.Service.

func (*StartStopOnce) Healthy

func (s *StartStopOnce) Healthy() error

Healthy returns ErrNotStarted if the state is not started. Override this per-service with more specific implementations.

func (*StartStopOnce) IfNotStopped

func (s *StartStopOnce) IfNotStopped(f func()) (ok bool)

IfNotStopped runs the func and returns true if in any state other than Stopped

func (*StartStopOnce) IfStarted

func (s *StartStopOnce) IfStarted(f func()) (ok bool)

IfStarted runs the func and returns true only if started, otherwise returns false

func (*StartStopOnce) Ready

func (s *StartStopOnce) Ready() error

Ready returns ErrNotStarted if the state is not started.

func (*StartStopOnce) StartOnce

func (s *StartStopOnce) StartOnce(name string, fn func() error) error

StartOnce sets the state to Started

func (*StartStopOnce) State

func (s *StartStopOnce) State() startStopOnceState

State retrieves the current state

func (*StartStopOnce) StopOnce

func (s *StartStopOnce) StopOnce(name string, fn func() error) error

StopOnce sets the state to Stopped

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

type URL url.URL

URL extends url.URL to implement encoding.TextMarshaler.

func MustParseURL

func MustParseURL(s string) *URL

func ParseURL

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

func (*URL) MarshalText

func (u *URL) MarshalText() ([]byte, error)

func (*URL) UnmarshalText

func (u *URL) UnmarshalText(input []byte) error

Jump to

Keyboard shortcuts

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