simutil

package
v0.0.0-...-bc49051 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EventEvery

func EventEvery(lambda time.Duration) func(context.Context) time.Duration

EventEvery returns an interval provider for an event source that returns a result from `RandomInterval`.

func EventIntervalAverage

func EventIntervalAverage(eventCount uint64, simulationDuration time.Duration) time.Duration

EventIntervalAverage yields the average event interval given an even count and total event duration.

func EventPipe

func EventPipe[T any](ctx context.Context, from <-chan Event[T], to chan Event[T])

EventPipe pipes events from one channel to another with a background goroutine that can be canceled with the given context.

func PoissonDuration

func PoissonDuration(lambda time.Duration) time.Duration

PoissonDuration returns a poisson distributed random duration value with an average centered around the given lambda.

func PoissonF64

func PoissonF64(lambda float64) float64

PoissonF64 generates a random poisson distributed float64 with average given by `lambda`.

func ReadEvents

func ReadEvents[T any](ctx context.Context, from <-chan Event[T], to EventHandler[T])

ReadEvents reads from a given event channel and calls a given handler function for each event.

func SpewEvents

func SpewEvents[T any](ctx context.Context, from chan Event[T])

SpewEvents prints events produced by a channel to stdout.

func WaitAll

func WaitAll(sources ...Waiter)

WaitAll blocks on all of the event sources completing.

Types

type Event

type Event[T any] struct {
	Timestamp time.Time
	Event     T
}

Event is an event type wrapped with a timestamp.

type EventHandler

type EventHandler[T any] func(context.Context, Event[T])

EventHandler is a method that receives events from `EventHandle`.

type EventSource

type EventSource[T any] struct {
	Count     uint64
	StopAfter time.Duration

	Interval     func(context.Context) time.Duration
	EventFactory func(context.Context) T
	// contains filtered or unexported fields
}

EventSource helps produce events given an interval.

You must provide both an `Interval` delegate AND an `Event` delegate.

func (*EventSource[T]) Start

func (es *EventSource[T]) Start(ctx context.Context, realtime bool) <-chan Event[T]

Start begins the event source in either "simulation" or "realtime" mode.

"Simulation" mode is enabled by passing `false` a the `realtime` flag and produces messages with no real-world delay.

"Realtime" mode waits the given intervals between events.

In this mode it will publish the events to a channel based on poisson distributed arrival times.

The call does not block, and instead starts a background goroutine which you can cancel with the given context.

func (EventSource[T]) Wait

func (es EventSource[T]) Wait()

Wait waits for the event source to finish producing.

type Waiter

type Waiter interface {
	Wait()
}

Waiter is an interface that EventSource[T] implements.

Jump to

Keyboard shortcuts

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