fix: properly use base2 (1024, *bibyte) when calculating memory limits (#190)

This commit is contained in:
Arnaud Lier 2024-06-29 20:34:20 +02:00 committed by GitHub
parent 29e4425e21
commit 934bf2493d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -34,7 +34,7 @@ type Mount struct {
// Limits is the build settings for a given server that impact docker container // Limits is the build settings for a given server that impact docker container
// creation and resource limits for a server instance. // creation and resource limits for a server instance.
type Limits struct { type Limits struct {
// The total amount of memory in megabytes that this server is allowed to // The total amount of memory in mebibytes that this server is allowed to
// use on the host system. // use on the host system.
MemoryLimit int64 `json:"memory_limit"` MemoryLimit int64 `json:"memory_limit"`
@ -79,7 +79,7 @@ func (l Limits) MemoryOverheadMultiplier() float64 {
} }
func (l Limits) BoundedMemoryLimit() int64 { func (l Limits) BoundedMemoryLimit() int64 {
return int64(math.Round(float64(l.MemoryLimit) * l.MemoryOverheadMultiplier() * 1_000_000)) return int64(math.Round(float64(l.MemoryLimit) * l.MemoryOverheadMultiplier() * 1024 * 1024))
} }
// ConvertedSwap returns the amount of swap available as a total in bytes. This // ConvertedSwap returns the amount of swap available as a total in bytes. This
@ -90,7 +90,7 @@ func (l Limits) ConvertedSwap() int64 {
return -1 return -1
} }
return (l.Swap * 1_000_000) + l.BoundedMemoryLimit() return (l.Swap * 1024 * 1024) + l.BoundedMemoryLimit()
} }
// ProcessLimit returns the process limit for a container. This is currently // ProcessLimit returns the process limit for a container. This is currently
@ -105,7 +105,7 @@ func (l Limits) AsContainerResources() container.Resources {
pids := l.ProcessLimit() pids := l.ProcessLimit()
resources := container.Resources{ resources := container.Resources{
Memory: l.BoundedMemoryLimit(), Memory: l.BoundedMemoryLimit(),
MemoryReservation: l.MemoryLimit * 1_000_000, MemoryReservation: l.MemoryLimit * 1024 * 1024,
MemorySwap: l.ConvertedSwap(), MemorySwap: l.ConvertedSwap(),
BlkioWeight: l.IoWeight, BlkioWeight: l.IoWeight,
OomKillDisable: &l.OOMDisabled, OomKillDisable: &l.OOMDisabled,