safegoroutines

module
v0.0.0-...-78d3f23 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: MIT

README

safegoroutine

safegoroutine is a linter that ensures every Goroutine is has a defer to catch it. This is required because recover handler are not inherited by child Goroutines in Go.

In it's current state I personally wouldn't use it, since it gives too many false positives.

Instead I would recommend creating a central function to launch Goroutine e.g.

// Go safely launches a Goroutine, so that a panic doesn't bring down the host.
func Go(f func()) {
  go func() {
	defer func() {
	  if err := recover(); err != nil {
		// TODO: handle error
	  }
	}()

	f()
  }()
}

// CtxGo safely launches a Goroutine, so that a panic doesn't bring down the host.
// The context is used to improve our observability.
func CtxGo(ctx context.Context, f func()) {
	go func() {
	defer func() {
	  if err := recover(); err != nil {
		// TODO: handle error
	  }
	}()
	f()
  }()
}

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

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