mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-01 15:00:00 +00:00
acc333c40b
When GTS is running in a container runtime which has configured CPU or memory limits or under an init system that uses cgroups to impose CPU and memory limits the values the Go runtime sees for GOMAXPROCS and GOMEMLIMIT are still based on the host resources, not the cgroup. At least for the throttling middlewares which use GOMAXPROCS to configure their queue size, this can result in GTS running with values too big compared to the resources that will actuall be available to it. This introduces 2 dependencies which can pick up resource contraints from the current cgroup and tune the Go runtime accordingly. This should result in the different queues being appropriately sized and in general more predictable performance. These dependencies are a no-op on non-Linux systems or if running in a cgroup that doesn't set a limit on CPU or memory. The automatic tuning of GOMEMLIMIT can be disabled by either explicitly setting GOMEMLIMIT yourself or by setting AUTOMEMLIMIT=off. The automatic tuning of GOMAXPROCS can similarly be counteracted by setting GOMAXPROCS yourself.
109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
package units
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// See: http://en.wikipedia.org/wiki/Binary_prefix
|
|
const (
|
|
// Decimal
|
|
|
|
KB = 1000
|
|
MB = 1000 * KB
|
|
GB = 1000 * MB
|
|
TB = 1000 * GB
|
|
PB = 1000 * TB
|
|
|
|
// Binary
|
|
|
|
KiB = 1024
|
|
MiB = 1024 * KiB
|
|
GiB = 1024 * MiB
|
|
TiB = 1024 * GiB
|
|
PiB = 1024 * TiB
|
|
)
|
|
|
|
type unitMap map[string]int64
|
|
|
|
var (
|
|
decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
|
|
binaryMap = unitMap{"k": KiB, "m": MiB, "g": GiB, "t": TiB, "p": PiB}
|
|
sizeRegex = regexp.MustCompile(`^(\d+(\.\d+)*) ?([kKmMgGtTpP])?[iI]?[bB]?$`)
|
|
)
|
|
|
|
var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
|
var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
|
|
|
|
func getSizeAndUnit(size float64, base float64, _map []string) (float64, string) {
|
|
i := 0
|
|
unitsLimit := len(_map) - 1
|
|
for size >= base && i < unitsLimit {
|
|
size = size / base
|
|
i++
|
|
}
|
|
return size, _map[i]
|
|
}
|
|
|
|
// CustomSize returns a human-readable approximation of a size
|
|
// using custom format.
|
|
func CustomSize(format string, size float64, base float64, _map []string) string {
|
|
size, unit := getSizeAndUnit(size, base, _map)
|
|
return fmt.Sprintf(format, size, unit)
|
|
}
|
|
|
|
// HumanSizeWithPrecision allows the size to be in any precision,
|
|
// instead of 4 digit precision used in units.HumanSize.
|
|
func HumanSizeWithPrecision(size float64, precision int) string {
|
|
size, unit := getSizeAndUnit(size, 1000.0, decimapAbbrs)
|
|
return fmt.Sprintf("%.*g%s", precision, size, unit)
|
|
}
|
|
|
|
// HumanSize returns a human-readable approximation of a size
|
|
// capped at 4 valid numbers (eg. "2.746 MB", "796 KB").
|
|
func HumanSize(size float64) string {
|
|
return HumanSizeWithPrecision(size, 4)
|
|
}
|
|
|
|
// BytesSize returns a human-readable size in bytes, kibibytes,
|
|
// mebibytes, gibibytes, or tebibytes (eg. "44kiB", "17MiB").
|
|
func BytesSize(size float64) string {
|
|
return CustomSize("%.4g%s", size, 1024.0, binaryAbbrs)
|
|
}
|
|
|
|
// FromHumanSize returns an integer from a human-readable specification of a
|
|
// size using SI standard (eg. "44kB", "17MB").
|
|
func FromHumanSize(size string) (int64, error) {
|
|
return parseSize(size, decimalMap)
|
|
}
|
|
|
|
// RAMInBytes parses a human-readable string representing an amount of RAM
|
|
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
|
|
// returns the number of bytes, or -1 if the string is unparseable.
|
|
// Units are case-insensitive, and the 'b' suffix is optional.
|
|
func RAMInBytes(size string) (int64, error) {
|
|
return parseSize(size, binaryMap)
|
|
}
|
|
|
|
// Parses the human-readable size string into the amount it represents.
|
|
func parseSize(sizeStr string, uMap unitMap) (int64, error) {
|
|
matches := sizeRegex.FindStringSubmatch(sizeStr)
|
|
if len(matches) != 4 {
|
|
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
|
|
}
|
|
|
|
size, err := strconv.ParseFloat(matches[1], 64)
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
|
|
unitPrefix := strings.ToLower(matches[3])
|
|
if mul, ok := uMap[unitPrefix]; ok {
|
|
size *= float64(mul)
|
|
}
|
|
|
|
return int64(size), nil
|
|
}
|