safe

package
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package safe provides panic-safe goroutine helpers. A panic in a worker goroutine (e.g. parsing a malformed provider stream, an index-out-of-range on an empty API response) is not recoverable by the goroutine that spawned it and crashes the entire process. These helpers recover and log the panic instead, and let the caller signal downstream (close a channel, emit an error) so consumers don't hang — turning "one bad response kills the process" into "one failed turn".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go(name string, fn func(), onPanic func(recovered any))

Go runs fn in a new goroutine with panic recovery (see Run).

func Recover

func Recover(name string)

Recover recovers and logs a panic (with stack). Defer it at the top of a goroutine whose body has its own cleanup defers (e.g. a channel close):

go func() {
	defer close(outChan)     // still runs after the panic is recovered
	defer safe.Recover("x")  // recovers so the process survives
	...
}()

func Run

func Run(name string, fn func(), onPanic func(recovered any))

Run executes fn with panic recovery. On panic it logs the value and stack, then calls onPanic (if non-nil) with the recovered value so the caller can signal downstream (e.g. send an error chunk before a deferred close). Use this inside an existing goroutine body.

Types

This section is empty.

Jump to

Keyboard shortcuts

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