From 1ec5935152f613b88eaeaa8647415c0a0a492e26 Mon Sep 17 00:00:00 2001 From: Arjen de Vos Date: Mon, 23 Nov 2026 15:48:37 +0000 Subject: [PATCH] feat(queueing): set how often idle workers look for work from the config asynq waits about a second (0.5 to 1.5s with jitter) between looks when its queues are empty, so a job dispatched to an idle queue can sit up to 1.5s before it starts. The default queue and each group now take a CheckInterval in config.Queue, so a group someone waits on can look often without every worker doing so. 0 keeps asynq's. --- config/queue.go | 9 +++++++++ queueing/resolvers/pools.go | 22 +++++++++++++++++----- queueing/resolvers/redis.go | 5 +++-- tests/queueing/resolvers/pools_test.go | 15 +++++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/config/queue.go b/config/queue.go index 79f95ee..8875e43 100644 --- a/config/queue.go +++ b/config/queue.go @@ -1,5 +1,7 @@ package config +import "time" + // QueueDriver is how jobs run: sync where they are dispatched, redis on // the worker. type QueueDriver string @@ -27,6 +29,9 @@ type Queue struct { // Concurrency is how many jobs of the default queue run at once: every // job registered outside a group. Concurrency int + // CheckInterval is how long an idle default worker waits before it + // looks for work again; 0 keeps asynq's second, with jitter. + CheckInterval time.Duration // Groups is each queue group's workers of its own, by the name // engine.Group registers its jobs under. A group registered without one // stops the worker from starting. @@ -41,6 +46,10 @@ type QueueGroup struct { // Concurrency is how many of its jobs run at once, on workers of its // own: the other queues never take them. Concurrency int + // CheckInterval is how long its idle workers wait before they look for + // work again; 0 keeps asynq's second, with jitter. A job someone waits + // on wants it short: on an idle queue it can sit up to 1.5s otherwise. + CheckInterval time.Duration } type RedisQueueConnection struct { diff --git a/queueing/resolvers/pools.go b/queueing/resolvers/pools.go index 424edba..9d5588f 100644 --- a/queueing/resolvers/pools.go +++ b/queueing/resolvers/pools.go @@ -3,6 +3,7 @@ package resolvers import ( "fmt" "sort" + "time" "github.com/gonstruct/core/config" "github.com/gonstruct/core/queueing/job" @@ -19,9 +20,10 @@ const DefaultPool = "default" // Pool is what one asynq server of the worker runs: Concurrency workers // taking from Queues, by weight. type Pool struct { - Name string - Concurrency int - Queues map[string]int + Name string + Concurrency int + CheckInterval time.Duration + Queues map[string]int } // Pools is a pool for each group, on its queue with the workers the config @@ -45,7 +47,12 @@ func Pools(jobs map[string]job.Job, groups map[string]string, queue config.Queue if workers < 1 { return nil, fmt.Errorf("[queueing] queue group %s has no workers: give it a Concurrency in the config's Groups", group) } - pools = append(pools, Pool{Name: group, Concurrency: workers, Queues: map[string]int{group: 1}}) + pools = append(pools, Pool{ + Name: group, + Concurrency: workers, + CheckInterval: queue.Groups[group].CheckInterval, + Queues: map[string]int{group: 1}, + }) } for group := range queue.Groups { if !grouped[group] { @@ -74,7 +81,12 @@ func Pools(jobs map[string]job.Job, groups map[string]string, queue config.Queue if queue.Concurrency < 1 { return nil, fmt.Errorf("[queueing] the default queue has no workers: give the config a Concurrency") } - pools = append(pools, Pool{Name: DefaultPool, Concurrency: queue.Concurrency, Queues: sharing}) + pools = append(pools, Pool{ + Name: DefaultPool, + Concurrency: queue.Concurrency, + CheckInterval: queue.CheckInterval, + Queues: sharing, + }) } return pools, nil diff --git a/queueing/resolvers/redis.go b/queueing/resolvers/redis.go index a0e3d56..ca692e6 100644 --- a/queueing/resolvers/redis.go +++ b/queueing/resolvers/redis.go @@ -178,8 +178,9 @@ func serve(servers []*asynq.Server, handler asynq.Handler) error { // retried and failed, the same in every pool. func (e *Redis) config(jobs map[string]job.Job, pool Pool) asynq.Config { return asynq.Config{ - Concurrency: pool.Concurrency, - Queues: pool.Queues, + Concurrency: pool.Concurrency, + TaskCheckInterval: pool.CheckInterval, + Queues: pool.Queues, RetryDelayFunc: func(retries int, err error, task *asynq.Task) time.Duration { if release, ok := job.AsRelease(err); ok { return release.Delay diff --git a/tests/queueing/resolvers/pools_test.go b/tests/queueing/resolvers/pools_test.go index c9f36ce..7eb192a 100644 --- a/tests/queueing/resolvers/pools_test.go +++ b/tests/queueing/resolvers/pools_test.go @@ -4,6 +4,7 @@ import ( "context" "reflect" "testing" + "time" "github.com/gonstruct/core/config" "github.com/gonstruct/core/queueing/job" @@ -86,6 +87,20 @@ func TestPools(t *testing.T) { config: config.Queue{Groups: map[string]config.QueueGroup{"ci": {Concurrency: 150}}}, want: []resolvers.Pool{{Name: "ci", Concurrency: 150, Queues: map[string]int{"ci": 1}}}, }, + { + name: "each pool looks for work as often as its config says", + jobs: jobs(&queued{name: "launch"}, &plain{}), + groups: map[string]string{"launch": "ci"}, + config: config.Queue{ + Concurrency: 100, + CheckInterval: time.Second, + Groups: map[string]config.QueueGroup{"ci": {Concurrency: 150, CheckInterval: 50 * time.Millisecond}}, + }, + want: []resolvers.Pool{ + {Name: "ci", Concurrency: 150, CheckInterval: 50 * time.Millisecond, Queues: map[string]int{"ci": 1}}, + {Name: "default", Concurrency: 100, CheckInterval: time.Second, Queues: map[string]int{"default": 1}}, + }, + }, } for _, each := range cases {