Documentation
¶
Overview ¶
Package panicx provides panic recovery utilities for goroutine safety. It ensures that panics in spawned goroutines are caught and logged rather than crashing the entire process.
Index ¶
- func Recover(logger *slog.Logger, context string)
- func RecoverWithCallback(logger *slog.Logger, context string, ...)
- func SafeGo(logger *slog.Logger, fn func())
- func SafeGoWithContext(ctx context.Context, logger *slog.Logger, fn func(context.Context))
- func SafeGoWithPolicy(logger *slog.Logger, policy RecoveryPolicy, context string, fn func())
- type RecoveryPolicy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Recover ¶
Recover is a defer-friendly panic recovery function. It logs the panic with full stack trace. Use it like:
defer panicx.Recover(logger, "myGoroutine")
func RecoverWithCallback ¶
func RecoverWithCallback(logger *slog.Logger, context string, callback func(panicValue any, stack []byte))
RecoverWithCallback recovers from panic and calls the provided callback. The callback receives the panic value and stack trace.
func SafeGo ¶
SafeGo launches a goroutine with panic recovery. The panic is logged with full stack trace, and the process continues.
func SafeGoWithContext ¶
SafeGoWithContext launches a goroutine with context and panic recovery. The context is passed to the function for cancellation support.
func SafeGoWithPolicy ¶
func SafeGoWithPolicy(logger *slog.Logger, policy RecoveryPolicy, context string, fn func())
SafeGoWithPolicy launches a goroutine with a specific recovery policy. The policy determines what happens after a panic is recovered.
Types ¶
type RecoveryPolicy ¶
type RecoveryPolicy int
RecoveryPolicy defines how to handle recovered panics.
const ( // PolicyLogAndContinue logs the panic and continues normal operation. // This is the default policy for non-critical goroutines. PolicyLogAndContinue RecoveryPolicy = iota // PolicyLogAndRestart logs the panic and signals for component restart. // Use for goroutines that should be restarted after failure. PolicyLogAndRestart // PolicyLogAndShutdown logs the panic and triggers graceful shutdown. // Use for critical goroutines where continued operation is unsafe. PolicyLogAndShutdown )