-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Customizable system resources configuration
This change would allow end users to specify custom system resources configuration. It should not change the existing behaviour as it keeps the defaults in place. Signed-off-by: Maxime VISONNEAU <[email protected]>
- Loading branch information
1 parent
1ce8735
commit 63e63fe
Showing
11 changed files
with
115 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package taskq | ||
|
||
const ( | ||
defaultSystemResourcesLoad1PerCPU float64 = 1.5 | ||
defaultSystemResourcesMemoryFreeMB uint64 = 2e5 | ||
defaultSystemResourcesMemoryFreePercentage uint64 = 5 | ||
) | ||
|
||
// SystemResources represents system related values | ||
type SystemResources struct { | ||
// Maximum per CPU load at 1min intervals | ||
Load1PerCPU float64 | ||
|
||
// Minimum free memory required in megabytes | ||
MemoryFreeMB uint64 | ||
|
||
// Minimum free memory required in percentage | ||
MemoryFreePercentage uint64 | ||
} | ||
|
||
// NewDefaultSystemResources returns a new SystemResources struct with some default values | ||
func NewDefaultSystemResources() SystemResources { | ||
return SystemResources{ | ||
Load1PerCPU: defaultSystemResourcesLoad1PerCPU, | ||
MemoryFreeMB: defaultSystemResourcesMemoryFreeMB, | ||
MemoryFreePercentage: defaultSystemResourcesMemoryFreePercentage, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// +build linux | ||
|
||
package taskq | ||
|
||
import ( | ||
"runtime" | ||
|
||
"github.com/capnm/sysinfo" | ||
"github.com/vmihailenco/taskq/v3/internal" | ||
) | ||
|
||
func hasFreeSystemResources(sr SystemResources) bool { | ||
si := sysinfo.Get() | ||
free := si.FreeRam + si.BufferRam | ||
|
||
if sr.Load1PerCPU > 0 && si.Loads[0] > sr.Load1PerCPU*float64(runtime.NumCPU()) { | ||
internal.Logger.Println("taskq: consumer memory is lower than required") | ||
return false | ||
} | ||
|
||
if sr.MemoryFreeMB > 0 && free < sr.MemoryFreeMB { | ||
internal.Logger.Println("taskq: consumer memory is lower than required") | ||
return false | ||
} | ||
|
||
if sr.MemoryFreePercentage > 0 && float64(free)/float64(si.TotalRam) < sr.MemoryFreePercentage/100 { | ||
internal.Logger.Println("taskq: consumer memory is lower than required") | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package taskq | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHasFreeSystemResources(t *testing.T) { | ||
// TODO: Manage to mock capnm/sysinfo | ||
assert.True(t, hasFreeSystemResources(SystemResources{})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build !linux | ||
|
||
package taskq | ||
|
||
func hasFreeSystemResources(_ SystemResources) bool { | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package taskq | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHasFreeSystemResources(t *testing.T) { | ||
assert.True(t, hasFreeSystemResources(SystemResources{})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package taskq | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewDefaultSystemResources(t *testing.T) { | ||
expectedValue := SystemResources{ | ||
Load1PerCPU: 1.5, | ||
MemoryFreeMB: 2e5, | ||
MemoryFreePercentage: 5, | ||
} | ||
assert.Equal(t, expectedValue, NewDefaultSystemResources()) | ||
} |