Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable system resources configuration #95

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -19,6 +19,7 @@ require (
github.com/onsi/gomega v1.10.3
github.com/satori/go.uuid v1.2.0
github.com/vmihailenco/msgpack/v5 v5.0.0
github.com/stretchr/testify v1.6.1
golang.org/x/net v0.0.0-20201027133719-8eef5233e2a1 // indirect
google.golang.org/protobuf v1.25.0 // indirect
)
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")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to fix a typo here

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 && free/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())
}