runtimebp

package
v0.9.0-alpha01 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: BSD-3-Clause Imports: 11 Imported by: 2

Documentation

Overview

Package runtimebp provides extensions to the stdlib runtime package.

Index

Constants

View Source
const UndefinedIP = "undefined"

UndefinedIP is used when we fail to get the ip address of this machine.

Variables

View Source
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

func GOMAXPROCS(min, max int) (oldVal, newVal int)

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

func GetFirstIPv4() (string, error)

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

type MaxProcsFormula func(n float64) int

MaxProcsFormula is the function to calculate GOMAXPROCS based on NumCPU value passed in.

type ShutdownHandler

type ShutdownHandler func(signal os.Signal)

ShutdownHandler is the callback type used in HandleSignals.

Jump to

Keyboard shortcuts

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