2
0
mirror of https://github.com/hibiken/asynq.git synced 2026-05-26 12:45:48 +08:00

[ci skip] Normalize queue priority numbers

This commit is contained in:
Ken Hibino
2020-01-07 21:17:01 -08:00
parent 8d9a2d1313
commit 24bb45b36b
2 changed files with 115 additions and 2 deletions

View File

@@ -97,12 +97,12 @@ func NewBackground(r *redis.Client, cfg *Config) *Background {
delayFunc = defaultDelayFunc
}
queues := cfg.Queues
if queues == nil {
if queues == nil || len(queues) == 0 {
queues = defaultQueueConfig
}
rdb := rdb.NewRDB(r)
scheduler := newScheduler(rdb, 5*time.Second)
processor := newProcessor(rdb, n, queues, delayFunc)
processor := newProcessor(rdb, n, normalizeQueueCfg(queues), delayFunc)
return &Background{
rdb: rdb,
scheduler: scheduler,
@@ -185,3 +185,35 @@ func (bg *Background) stop() {
bg.processor.handler = nil
bg.running = false
}
// normalizeQueueCfg divides priority numbers by their
// greatest common divisor.
func normalizeQueueCfg(queueCfg map[string]uint) map[string]uint {
var xs []uint
for _, x := range queueCfg {
xs = append(xs, x)
}
d := gcd(xs...)
res := make(map[string]uint)
for q, x := range queueCfg {
res[q] = x / d
}
return res
}
func gcd(xs ...uint) uint {
fn := func(x, y uint) uint {
for y > 0 {
x, y = y, x%y
}
return x
}
res := xs[0]
for i := 0; i < len(xs); i++ {
res = fn(xs[i], res)
if res == 1 {
return 1
}
}
return res
}