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() 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() 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 v2 cpu.max values to determine the hard CPU limit of the container.
If the current process is not running with cgroup v2, it falls back to read from the cgroup v1 cpu.cfs_quota_us and cpu.cfs_period_us values. If the current process is not running inside a container, or if there's no limit set in cgroup, it will fallback to runtime.NumCPU() instead.
Types ¶
type Config ¶
type Config struct {
// Deprecated: No-op for now, will be removed in a future release.
NumProcesses struct {
// Deprecated: Always overridden to math.MaxInt.
Max int `yaml:"max"`
// Deprecated: Always overridden to 1.
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.