Documentation
¶
Index ¶
- Variables
- func Filter[T any](input []T, f func(i int, val T) bool) []T
- func ForChan[T any](ch chan T, f func(val T))
- func ForEach[T any](data []T, f func(i int, val T))
- func ForEachErr[T any](data []T, f func(i int, val T) error) error
- func ForErrChan[T any](ch chan T, f func(val T) error) error
- func Map[T any, V any](data []T, f func(i int, val T) V) []V
- func MapErr[T any, V any](data []T, f func(i int, val T) (V, error)) ([]V, error)
- func Reduce[T any, V any](input []T, f func(val T, acc V) V) V
- type EachRunner
- type ErrRunner
- type ErrTaskRunner
- type Result
- type Runner
- type RunnerFunc
- type TaskFiller
- type TaskRunner
- type Waiter
Constants ¶
This section is empty.
Variables ¶
var ( ErrRecoveredFromPanic = errors.New("kytsunya: recovered from panic") ErrTimeout = errors.New("kytsunya: goroutine timed out") )
var (
ErrWaitWithoutWaitGroup = errors.New("Wait() was called without WithWaitGroup initialization, please, call WithWaitGroup() before")
)
Functions ¶
func Filter ¶
Filter function accept a slice of T(any), and a filter handler that returns a bool value (include?). In case of true, value will be included in the output slice, otherwise will be filtered out.
func ForChan ¶
func ForChan[T any](ch chan T, f func(val T))
ForChan will range through the channel until its closed
func ForEach ¶
ForEach do a range through the slice of data, and will execute input handler function on every member of the input slice. For example:
ForEach([]int{1, 2, 3, 4, 5, 6}, func(i, val int) { fmt.Printf("index: %d value: %d", i, val) })
Will print index and value of every member of input slice.
func ForEachErr ¶
fmt.Printf("index: %d value: %d", i, val) return nil })
Will print index and value of every member of input slice. In case of error, iteration will be interrupted.
func ForErrChan ¶
ForErrChan will range through the channel entries and handler will receive every value. In case of error returned from the handler, loop will be stopped.
func Map ¶
output: [1 2 3 4 5 6] as an array of string In this example Map modified an array of integers to an array of string.
Types ¶
type EachRunner ¶
EachRunner could be used in case of needing to handle every list member in different goroutine.
func NewEachRunner ¶
func NewEachRunner[T any, V any](data []T) *EachRunner[T, V]
NewEachRunner returns an instance of each runner - entity that handle a slice of data with handler, where each member handled in a different goroutine. T represents input data type. V represents output data type.
func (*EachRunner[T, V]) Handle ¶
func (er *EachRunner[T, V]) Handle(handler func(val T) Result[V]) chan Result[V]
Handle(f) accepts a functional handler for every member on the input list. Handler will be spawned in a separate goroutine and will receive on of input list entry.
func (*EachRunner[T, V]) WithRecover ¶
func (er *EachRunner[T, V]) WithRecover() *EachRunner[T, V]
WithRecover adds recovery handler to each spawned goroutine.
type ErrRunner ¶
type ErrRunner[T any] struct { // contains filtered or unexported fields }
fmt.Println(<-res) In this case goroutine also using WithTimeout functionality.
func Erroutine ¶
Erroutine represents a goroutine that returns a result of execution as a value (in case of Wait() was called) or channel with one value in case of WaitAsync() was called. T(any) param represents an output result type.
func (*ErrRunner[T]) Spawn ¶
Spawn accept handler/worker function as an argument and start the execution immediately.
func (*ErrRunner[T]) Wait ¶
func (r *ErrRunner[T]) Wait() Result[T]
Wait waits until spawned goroutine return a result of the execution or timed out.
func (*ErrRunner[T]) WaitAsync ¶
func (r *ErrRunner[T]) WaitAsync() chan Result[T]
WaitAsync returns channel that sends a value as a result of the execution, or timeout error.
func (*ErrRunner[T]) WithRecover ¶
WithRecover add a recovery handler to the Erroutine.
func (*ErrRunner[T]) WithTimeout ¶
WithTimeout adds a timeout of execution to the erroutine. In case of timeout, error message will be moved to Result{Err: "error here"}. Error type is: ErrTimeout = errors.New("kytsya: goroutine timed out")
type ErrTaskRunner ¶
type ErrTaskRunner[T any] struct { // contains filtered or unexported fields }
ErrTaskRunner is a structure for run a group of async tasks that should return a result or error, as run & collect result from some number of network calls. For example:
resCh := NewErrorBox[string](). WithRecover(). AddTask(func() Result[string] { return Result[string]{Data: "1"} }). AddTask(func() Result[string] { return Result[string]{Data: "2"} }). AddTask(func() Result[string] { return Result[string]{Err: errors.New("Houston, we have a problem")} }). AddTask(func() Result[string] { panic("aaaaa") }). Run() // ResCh will be closed after all tasks are done! for v := range resCh { fmt.Println(v) }
In this case, WithRecover() returns error to a handler-channel as a Result{Err: val} from function that called a panic.
func NewErrorBox ¶
func NewErrorBox[T any]() *ErrTaskRunner[T]
NewErrorBox is a constructor for task runner. We call it *Box cause 🐈🐈🐈 are in love with boxes!
func (*ErrTaskRunner[T]) AddTask ¶
func (tr *ErrTaskRunner[T]) AddTask(f func() Result[T]) *ErrTaskRunner[T]
AddTask accept a generic function that will contain result or error structure.
func (*ErrTaskRunner[T]) Run ¶
func (tr *ErrTaskRunner[T]) Run() chan Result[T]
Run spawns all tasks and return a chan Result[T] to collect all.
func (*ErrTaskRunner[T]) WithRecover ¶
func (tr *ErrTaskRunner[T]) WithRecover() *ErrTaskRunner[T]
WithRecover adds a recovery handler for every task. Panic message and a stacktrace will be returned as Result{Err: "error message/panic trace"}
type Result ¶
Result[T] define kind of "OneOf": Generic result of the execution or error message. Error message will be also returned in case of panic or timeout.
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
defer func(){ if err := recover(); err != nil { fmt.Println(err) } }()
With kytsya it's possible to generalize such operation in one defined manner: kytsya.Goroutine().WithRecover().Spawn(func() {fmt.Println("🐈🐈🐈")}).Wait() Wait group also built-in.
func Goroutine ¶
func Goroutine() *Runner
Goroutine creates a new goroutine runner. Example: kytsunya2.Goroutine().WithRecover().WithWaitGroup().Spawn(func() {fmt.Println("🐈🐈🐈")}).Wait()
func (*Runner) Spawn ¶
Spawn start a new goroutine, accept function that will be executed in the newly created routine.
func (*Runner) WithRecover ¶
WithRecover will add defer recover() function to the executor that will recover panics and will print the stack trace into stdout.
func (*Runner) WithWaitGroup ¶
WithWaitGroup add a wait group into the executor (Wait() could be called, and will block until created goroutine will return).
type RunnerFunc ¶
type RunnerFunc interface {
Run() Waiter
}
type TaskFiller ¶
type TaskFiller interface { AddTask(f func()) TaskFiller Run() Waiter AfterAll(f func()) RunnerFunc }
type TaskRunner ¶
type TaskRunner struct {
// contains filtered or unexported fields
}
TaskRunner represents an executor for a group of goroutines. Could be useful in case of needing to run a controlled group of goroutines with one handler point, panic-handlers of wait group. Constructor function is NewBox 'cause boxes is best friends of cats! For example:
NewBox().WithWaitGroup(). AddTask(func() {}). AddTask(func() {}). AddTask(func() {}). AfterAll(func() {}). Run().Wait()
WithWaitGroup().WithRecover() and AfterAll(f) is not necessary calls, added here just to show the possibilities.
func NewBox ¶
func NewBox() *TaskRunner
NewBox creates new runner that controls a set of running goroutines that returns no values. It has functionality to: 1. Recover all panics with WithRecover() 2. Add Wait group to run with WithWaitGroup() 3. Add new task to execution with AddTask(f()) 4. Run the set with Run() 5. Wait till all goroutines are done with Wait() 6. Add a function that will be executed after all goroutines will done with AfterAll(f())
func (*TaskRunner) AddTask ¶
func (tr *TaskRunner) AddTask(f func()) TaskFiller
AddTask accept a new task for async execution.
func (*TaskRunner) AfterAll ¶
func (tr *TaskRunner) AfterAll(f func()) RunnerFunc
AfterAll accept a handler that will be executed after all tasks. In general case it could be used for a range of tasks: - Close any kinds of connections - Logs the results of measure the time of executions - close channels - etc. up to user.
WARNING: The nature of AfterAll is async. It means, that it will wait unit WaitGroup unblock and will be executed asynchronously. If it is necessary to wait until AfterFunc will exit, use any possible sync mechanism.
func (*TaskRunner) Wait ¶
func (tr *TaskRunner) Wait()
Wait blocks until all tasks will done, call a panic in case of "WithWaitGroup()" was no called.
func (*TaskRunner) WithRecover ¶
func (tr *TaskRunner) WithRecover() *TaskRunner
WithRecover add a recovery handler to a task funner. Handler will be assigned to every goroutine and in case of panic will recover and print a stacktrace into stdout.
func (*TaskRunner) WithWaitGroup ¶
func (tr *TaskRunner) WithWaitGroup() *TaskRunner
WithWaitGroup add a WaitGroup to an executions and makes possible to call Wait() to wait until all tasks will done.