Documentation
¶
Overview ¶
Package runtimebp provides extensions to the stdlib runtime package.
Index ¶
- Constants
- Variables
- func GOMAXPROCS(min, max int) (oldVal, newVal int)
- func GOMAXPROCSwithFormula(min, max int, formula MaxProcsFormula) (oldVal, newVal int)
- func GetFirstIPv4() (string, error)
- func HandleShutdown(ctx context.Context, handler ShutdownHandler, signals ...os.Signal)
- func InitFromConfig(cfg Config)
- func NumCPU() (n float64)
- type Config
- type MaxProcsFormula
- type ShutdownHandler
Constants ¶
const UndefinedIP = "undefined"
UndefinedIP is used when we fail to get the ip address of this machine.
Variables ¶
var ErrNoIPv4Found = errors.New("runtimebp: no IPv4 ip found")
ErrNoIPv4Found is an error could be returned by GetFirstIPv4 when we can't find any non-loopback IPv4 addresses on this machine.
Functions ¶
func GOMAXPROCS ¶
GOMAXPROCS sets runtime.GOMAXPROCS with the default formula, in bound of [min, max].
Currently the default formula is NumCPU() rounding up.
func GOMAXPROCSwithFormula ¶
func GOMAXPROCSwithFormula(min, max int, formula MaxProcsFormula) (oldVal, newVal int)
GOMAXPROCSwithFormula sets runtime.GOMAXPROCS with the given formula, in bound of [min, max].
func GetFirstIPv4 ¶
GetFirstIPv4 returns the first local IPv4 address that's not a loopback.
func HandleShutdown ¶
func HandleShutdown(ctx context.Context, handler ShutdownHandler, signals ...os.Signal)
HandleShutdown register a handler to do cleanups for a graceful shutdown.
This function blocks until the ctx passed in is cancelled, or a signal happens, whichever comes first. So it should usually be started in its own goroutine.
Baseplate services should use baseplate.Serve which will manage this for you rather than using HandleShutdown directly.
SIGTERM, as specified in https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods, and os.Interrupt as for handling ^C in command line, are always registered in this function and there's no need to pass them in (but passing them in won't cause any harm), the signals vararg is for any additional signals you wish to handle.
func InitFromConfig ¶
func InitFromConfig(cfg Config)
InitFromConfig sets GOMAXPROCS using the given config.
func NumCPU ¶
func NumCPU() (n float64)
NumCPU returns the number of CPUs assigned to this running container.
This is the container aware version of runtime.NumCPU. It reads from the cgroup sysfs values.
If the current process is not running inside a container, or for whatever reason we failed to read the cgroup sysfs values, it will fallback to runtime.NumCPU() instead.
When fallback happens, it also prints the reason to stderr.
Types ¶
type Config ¶
type Config struct {
// NumProcesses can be used to set the maximum and minimum number of Go
// Processes.
NumProcesses struct {
// Max controls the maximum number of Go processes. This is the ceiling,
// the final maximum number will depend on the number of CPUs available to
// your service.
//
// Defaults to 64 if not set.
Max int `yaml:"max"`
// Max controls the minimum number of Go processes.
//
// Defaults to 1 if not set.
Min int `yaml:"min"`
} `yaml:"numProcesses"`
}
Config is the configuration struct for the runtimebp package.
Can be parsed from YAML.
type MaxProcsFormula ¶
MaxProcsFormula is the function to calculate GOMAXPROCS based on NumCPU value passed in.
type ShutdownHandler ¶
ShutdownHandler is the callback type used in HandleSignals.