goroutine

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2021 License: MIT Imports: 1 Imported by: 0

README

goroutine

Go Report Card GitHub go.mod Go version GitHub

A goroutine wrapper for creating and running panic safe goroutines.

The purpose of this package is to provide a simple wrapper function for goroutines, which automatically handles panics in goroutines. Starting a new goroutine without taking care of recovering from a possible panic in that goroutine itself could crash the whole application.

The Go function runs an arbitrary function f in a separate goroutine, which handles the recovering from panic within that goroutine.

Usage (with dot import)

Instead of running

go func() {
    panic("Panic raised in goroutine")
}()

simply call

Go(func() {
    panic("Panic raised in goroutine")
})

in order to create a panic safe goroutine.

Functions with multiple input params must be wrapped within an anonymous function.

Instead of running

go func(a, b int) {
    panic(a+b)
}(21,21)

simply call

Go(func() {
    func(a, b int) {
        panic(a+b)
    }(21,21)
})

Examples

package main

import (
	"fmt"
	. "github.com/sknr/goroutine"
)

var errChan chan string

func init() {
	errChan = make(chan string)

	// Override the default recover function.
	SetDefaultRecoverFunc(func(v interface{}) {
		errChan <- fmt.Sprintf("%v", v)
	})
}

func main() {
	exitChan := make(chan struct{})
	cnt := 1
	for {
	    select {
		case <-Go(func() {
			func(a, b int) {
				fmt.Println("Divide by zero", a/b)
			}(1, 0)
		}):
			fmt.Printf("Never reached due to panic in goroutine")
		case err := <-errChan:
			fmt.Println("Goroutine exits with error: ", err)

		case <-exitChan:
			fmt.Println("Exit program")
			return
		}

		if cnt >= 3 {
			close(exitChan)
			fmt.Println("Channel closed")
		}
		cnt++
	}
}

Create a new goroutine with a custom recover function

In order to override the default recover function for goroutines, simply provide an alternative implementation for with goroutine.SetDefaultRecoverFunc.

If you need different recover functions for different goroutines, you can simply call

Goroutine(func() {
    func(name string) {
        panic(fmt.Sprintln("Hallo", name))
    }("Welt")
}).WithRecoverFunc(func(v interface{}, done chan<- error) {
    log.Printf("Custom recover function in goroutine, with error: %v", v)
}).Go()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPanicRecovered          = &goroutineError{msg: "panic recovered"}
	ErrRecoverFunctionPanicked = &goroutineError{msg: "recover function panicked"}
)

Functions

func Go

func Go(f func()) <-chan error

Go runs a function f in a separate goroutine, which does handle the recovering from panic within that goroutine. Starting a new goroutine without taking care of recovering from a possible panic in that goroutine itself could crash the whole application. Functions with more than one input param must be wrapped within a func(){} like in the example below. Instead of running:

go func(s string) {
  panic(s)
}("Hello World")

simply call:

Go(func() {
  func(s string) {
    panic(s)
  }("Hello World")
})

func Goroutine

func Goroutine(f func()) *goroutine

Goroutine creates a new panic safe goroutine, with the defaultRecoverFunc as recover function.

func SetDefaultRecoverFunc

func SetDefaultRecoverFunc(recF RecoverFunc)

SetDefaultRecoverFunc can be used to override the defaultRecoverFunc which is used by Go and Goroutine functions. Note: Calling panic() within the recover function recF, will cause the application to crash.

If you pass nil as a RecoverFunc, the panic will be silently recovered.

Types

type RecoverFunc

type RecoverFunc func(v interface{}, done chan<- error)

The RecoverFunc type defines the signature of a recover function within a goroutine.

func GetDefaultRecoverFunc

func GetDefaultRecoverFunc() RecoverFunc

GetDefaultRecoverFunc returns the current default recover function for goroutines used by the Go and Goroutine functions.

Jump to

Keyboard shortcuts

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