Skip to content

Commit

Permalink
Customizable system resources configuration
Browse files Browse the repository at this point in the history
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
mvisonneau committed Oct 14, 2020
1 parent 1ce8735 commit 63e63fe
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 39 deletions.
2 changes: 1 addition & 1 deletion consumer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (r *configRoulette) genConfigs(bestCfg *consumerConfig, queueEmpty bool) {
r.oldBestCfg = bestCfg.Clone()
r.addConfig(r.oldBestCfg)

if !hasFreeSystemResources() {
if !hasFreeSystemResources(r.opt.MinSystemResources) {
internal.Logger.Println("taskq: system does not have enough free resources")
return
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.2
github.com/satori/go.uuid v1.2.0
github.com/stretchr/testify v1.6.1
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.1
github.com/vmihailenco/tagparser v0.1.2 // indirect
go.opentelemetry.io/otel v0.13.0
Expand Down
7 changes: 7 additions & 0 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ type QueueOptions struct {
// Optional message handler. The default is the global Tasks registry.
Handler Handler

// Minimal system resources required to consider the consumer available to process the queue
MinSystemResources SystemResources

inited bool
}

Expand Down Expand Up @@ -118,6 +121,10 @@ func (opt *QueueOptions) Init() {
if opt.Handler == nil {
opt.Handler = &Tasks
}

if opt.MinSystemResources == (SystemResources{}) {
opt.MinSystemResources = NewDefaultSystemResources()
}
}

//------------------------------------------------------------------------------
Expand Down
31 changes: 0 additions & 31 deletions sysinfo_linux.go

This file was deleted.

7 changes: 0 additions & 7 deletions sysinfo_other.go

This file was deleted.

28 changes: 28 additions & 0 deletions system.go
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,
}
}
32 changes: 32 additions & 0 deletions system_linux.go
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
}
12 changes: 12 additions & 0 deletions system_linux_test.go
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{}))
}
7 changes: 7 additions & 0 deletions system_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !linux

package taskq

func hasFreeSystemResources(_ SystemResources) bool {
return true
}
11 changes: 11 additions & 0 deletions system_other_test.go
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{}))
}
16 changes: 16 additions & 0 deletions system_test.go
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())
}

0 comments on commit 63e63fe

Please sign in to comment.