goroutine

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package goroutine provides panic-safe goroutines and error groups.

Goroutines started here recover from panics, log them through the provided logger and can be retried. Pass a cloned logger (CLogger is not safe for concurrent use) when starting work in the background.

Index

Examples

Constants

View Source
const (
	RetryDelay   = time.Second
	Unrestricted = -1
)
View Source
const (
	ErrCodeGoroutineNoLogger = "GORTN-001"
)

Variables

View Source
var (
	ErrGoroutineNoLogger = func(ctx context.Context) error {
		return jet.NewAppErrBuilder(ErrCodeGoroutineNoLogger, "either logger or logger func must be specified").C(ctx).Err()
	}
)

Functions

This section is empty.

Types

type ErrGroup

type ErrGroup interface {
	// Go calls the given function in a new goroutine.
	//
	// The first call to return a non-nil error cancels the group; its error will be
	// returned by Wait.
	Go(f func() error)
	// Wait blocks until all function calls from the Go method have returned, then
	// returns the first non-nil error (if any) from them.
	Wait() error
	// Cancel cancels all goroutines running
	Cancel()
	// CancelFunc returns cancel function defined by cancelled context
	CancelFunc() func()
	// WithLogger allows to specify prepared logger
	WithLogger(logger jet.CLogger) ErrGroup
	// WithLoggerFn allows to specify logger func
	WithLoggerFn(loggerFn jet.CLoggerFunc) ErrGroup
	// Mth allows to specify method to log in case of panic
	// it works only for logger func
	Mth(method string) ErrGroup
	// Cmp allows to specify component to log in case of panic
	// it works only for logger func
	Cmp(component string) ErrGroup
}

ErrGroup is a replica of a standard errgroup with panic handling and custom logging it allows to run multiple goroutines and wait unless all finished either one of them failed Once one fail, all other are cancelled at once

Example
package main

import (
	"context"
	"fmt"
	"sync/atomic"

	"github.com/zloevil/jet"
	"github.com/zloevil/jet/goroutine"
)

func main() {
	logger := jet.L(jet.InitLogger(&jet.LogConfig{Level: jet.ErrorLevel}))
	eg := goroutine.NewGroup(context.Background()).WithLogger(logger)

	var n int64
	for i := 0; i < 5; i++ {
		eg.Go(func() error {
			atomic.AddInt64(&n, 1)
			return nil
		})
	}

	err := eg.Wait()
	fmt.Println(atomic.LoadInt64(&n), err)
}
Output:
5 <nil>

func NewGroup

func NewGroup(ctx context.Context) ErrGroup

NewGroup returns a new Group and an associated Context derived from ctx.

The derived Context is canceled the first time a function passed to Go returns a non-nil error or the first time Wait returns, whichever occurs first.

type Goroutine

type Goroutine interface {
	// Go executes a f func as a goroutine
	Go(ctx context.Context, f func())
	// WithLogger allows to specify prepared logger
	WithLogger(logger jet.CLogger) Goroutine
	// WithLoggerFn allows to specify logger func
	WithLoggerFn(loggerFn jet.CLoggerFunc) Goroutine
	// WithRetry allows to specify retry count
	// if retry less than 0, number of retries isn't restricted
	WithRetry(retry int) Goroutine
	// WithRetryDelay specifies delay before retry
	WithRetryDelay(delay time.Duration) Goroutine
	// Mth allows to specify method to log in case of panic
	// it works only for logger func
	Mth(method string) Goroutine
	// Cmp allows to specify component to log in case of panic
	// it works only for logger func
	Cmp(component string) Goroutine
}

Goroutine provides a wrapper around native GO goroutine with panic recovery and retry support

func New

func New() Goroutine

Jump to

Keyboard shortcuts

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