reap

package module
v0.0.0-...-bf69c61 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: MPL-2.0 Imports: 4 Imported by: 75

README

go-reap

Provides a super simple set of functions for reaping child processes. This is useful for running applications as PID 1 in a Docker container.

Note that a mutex is supplied to allow your application to prevent reaping of child processes during certain periods. You need to use care in order to prevent the reaper from stealing your return values from uses of packages like Go's exec. We use an RWMutex so that we don't serialize all of your application's execution of sub processes with each other, but we do serialize them with reaping. Your application should get a read lock when it wants to do a wait and be safe from the reaper.

This should be supported on most UNIX flavors, but is not supported on Windows or Solaris. Unsupported platforms have a stub implementation that's safe to call, as well as an API to check if reaping is supported so that you can produce an error in your application code.

Documentation

The full documentation is available on Godoc.

Example

Below is a simple example of usage

// Reap children with no control or feedback.
go reap.ReapChildren(nil, nil, nil)

// Get feedback on reaped children and errors.
if reap.IsSupported() {
	pids := make(reap.PidCh, 1)
	errors := make(reap.ErrorCh, 1)
	done := make(chan struct{})
	var reapLock sync.RWMutex
	go reap.ReapChildren(pids, errors, done, &reapLock)
	// ...
	close(done)
} else {
	fmt.Println("Sorry, go-reap isn't supported on your platform.")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSupported

func IsSupported() bool

IsSupported returns true if child process reaping is supported on this platform.

func ReapChildren

func ReapChildren(pids PidCh, errors ErrorCh, done chan struct{}, reapLock *sync.RWMutex)

ReapChildren is a long-running routine that blocks waiting for child processes to exit and reaps them, reporting reaped process IDs to the optional pids channel and any errors to the optional errors channel.

The optional reapLock will be used to prevent reaping during periods when you know your application is waiting for subprocesses to return. You need to use care in order to prevent the reaper from stealing your return values from uses of packages like Go's exec. We use an RWMutex so that we don't serialize all of the application's execution of sub processes with each other, but we do serialize them with reaping. The application should get a read lock when it wants to do a wait.

Types

type ErrorCh

type ErrorCh chan error

ErrorCh is an error channel that lets you know when an error was encountered while reaping child processes.

type PidCh

type PidCh chan int

PidCh returns the process IDs of reaped child processes.

Jump to

Keyboard shortcuts

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