syncx

package
v1.1.19 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go(ctx context.Context, f func(ctx context.Context), opts ...GoOption)

Go launches a goroutine with panic recovery and optional context modifications. The provided function f is executed in a new goroutine with the given context. Any panics in the goroutine will be caught and handled by the registered panic handler.

Example usage:

Go(ctx, func(ctx context.Context) {
    // Your goroutine logic here
}, WithTimeout(5*time.Second))

func RegisterPanicHandler

func RegisterPanicHandler(f func(context.Context, error))

RegisterPanicHandler sets a custom handler function for panics that occur in goroutines started by Go(). The handler receives the context and the error that caused the panic.

If no handler is registered, panics will be propagated normally.

func WG

func WG(ctx context.Context) *wg

WG creates a new wait group with context support. It combines the functionality of sync.WaitGroup with context cancellation. The returned wait group will be cancelled when the provided context is cancelled.

Example usage:

wg := WG(ctx)
for i := 0; i < 5; i++ {
    wg.Go(func(ctx context.Context) {
        // Your concurrent work here
    })
}
wg.Wait() // Wait for all goroutines to complete

Types

type GoOption

type GoOption func(ctx context.Context) (context.Context, func())

GoOption represents a configuration option for the Go function. It receives a context and returns a modified context along with a cleanup function.

func WithNoCancel added in v1.1.5

func WithNoCancel() GoOption

WithNoCancel returns a GoOption that creates a context that cannot be cancelled. This is useful when you want the goroutine to complete regardless of the parent context's cancellation.

func WithTimeout

func WithTimeout(d time.Duration) GoOption

WithTimeout returns a GoOption that adds a timeout to the context. The goroutine will be cancelled after the specified duration.

Jump to

Keyboard shortcuts

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