safe

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: Unlicense Imports: 7 Imported by: 0

README

Package Safe

GoDoc

Package safe provides helpers for gracefully handling panics in background goroutines.

Example Usage

import (
  "context"
  "fmt"

  "github.com/mbranch/safe-go"
)

func main() {
  ctx := context.Background()

  items := []int{1, 2, 3, 4, 5}

  g, groupCtx := safe.GroupWithContext(ctx)
  for _, item := range items {
    // Why do we need this wrapper?
    // See: https://medium.com/@julienetienne/golang-for-loop-concurrency-quirk-95e6b184cfe
    func(itemVal int) {
      g.Go(func() error {
        return processItem(groupCtx, itemVal)
      })
    }(item)
  }

  err := g.Wait()
  if err != nil {
    fmt.Println("error", err)
  }
}

Documentation

Overview

Package safe provides helpers for gracefully handling panics in background goroutines.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(fn func() error) (err error)

Do executes fn. If a panic occurs, it will be recovered and returned as a safe.PanicError.

Example
getName := func() string {
	panic("unhandled error")
}
err := safe.Do(func() error {
	_ = getName()
	return nil
})

if err, ok := err.(safe.PanicError); ok {
	fmt.Println(err)
}
Output:

panic: unhandled error

func DoWithResult

func DoWithResult(fn func() (interface{}, error)) (res interface{}, err error)

DoWithResult executes fn. If a panic occurs, it will be recovered and returned as a safe.PanicError.

Example
getName := func() string {
	panic("unhandled error")
}
_, err := safe.DoWithResult(func() (interface{}, error) {
	_ = getName()
	return nil, nil
})

if err, ok := err.(safe.PanicError); ok {
	fmt.Println(err)
}
Output:

panic: unhandled error
Example (Ok)
getName := func() string {
	return "OK"
}
res, err := safe.DoWithResult(func() (interface{}, error) {
	return getName(), nil
})

if err, ok := err.(safe.PanicError); ok {
	fmt.Println(err)
} else {
	fmt.Println(res.(string))
}
Output:

OK

func Go

func Go(fn func())

Go executes fn in a background goroutine. If a panic occurs, it will be recovered and passed to the global panic handler.

Example
safe.SetPanicHandler(func(err error) {
	fmt.Println(err)
})
var wg sync.WaitGroup
wg.Add(1)
safe.Go(func() {
	defer wg.Done()
	panic("unhandled error")
})
wg.Wait()
Output:

panic: unhandled error

func SetPanicHandler

func SetPanicHandler(fn func(err error))

SetPanicHandler configures a global handler for any panics that occur in background goroutines spawned by safe.Go. If unset, they'll instead be written directly to the log.

Types

type Group

type Group struct {
	// contains filtered or unexported fields
}

A Group is a drop-in replacement for errgroup.Group, a collection of goroutines working on subtasks that are part of the same overall task. If any panics occur, they will be recovered and returned as a safe.PanicError.

A zero Group is valid and does not cancel on error.

Example
var g safe.Group
g.Go(func() error {
	panic("unhandled error")
})
err := g.Wait()
fmt.Printf("(%T) %v\n", err, err)
Output:

(safe.PanicError) panic: unhandled error

func GroupWithContext

func GroupWithContext(ctx context.Context) (*Group, context.Context)

GroupWithContext 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.

func (*Group) Go

func (g *Group) Go(fn func() error)

Go calls the given function in a new goroutine. It blocks until the new goroutine can be added without the number of active goroutines in the group exceeding the configured limit.

The first call to panic or return a non-nil error cancels the group's context, if the group was created by calling WithContext. The error will be returned by Wait.

func (*Group) SetLimit

func (g *Group) SetLimit(n int)

SetLimit limits the number of active goroutines in this group to at most n. A negative value indicates no limit.

Any subsequent call to the Go method will block until it can add an active goroutine without exceeding the configured limit.

The limit must not be modified while any goroutines in the group are active.

func (*Group) TryGo

func (g *Group) TryGo(f func() error) bool

TryGo calls the given function in a new goroutine only if the number of active goroutines in the group is currently below the configured limit.

The return value reports whether the goroutine was started.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

type PanicError

type PanicError struct {
	// contains filtered or unexported fields
}

PanicError is an error that wraps a panic value. It also embeds a pkg/errors error to ensure the stack trace is properly captured and rendered to any error reporters.

func (PanicError) Panic

func (p PanicError) Panic() interface{}

Panic returns the underlying value passed to panic().

Jump to

Keyboard shortcuts

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