Files
forge/modules/setting/queue.go
zeekayandHanzo Dev 45f2c2e593 queue: one queue
There were four ways to run a queue — dummy, channel, kv, zapdb — and three of
them did the same job. Now there is one.

  channel  a second IN-PROCESS queue whose only distinction was not surviving a
           restart.
  kv       a second DURABLE queue that needed a Redis server to be one.

Both did what ZapDB does, neither did it better, and each was one more thing to
configure wrong. Deleted.

dummy/immediate stays and is not an exception: it does not queue at all, it runs
the work inline on the caller. That is a different behaviour, not a second
implementation of the same one.

Any other TYPE in an existing app.ini resolves to zapdb, so nothing fails to
start over a name.

The tests told on themselves while collapsing this, and the failures were the
point rather than an obstacle:

  Three constructed queues from a bare QueueSettings with no Datadir. The
  in-memory backend never needed one; a durable queue does. They now pass a
  TempDir instead of zapdb quietly inventing a directory, which would have
  hidden the same misconfiguration in production.

  TestWorkerPoolQueueActiveWorkers asserted worker state 50ms after a push. That
  held when a push was a channel send. It is 150ms now — the assertion is about
  the worker pool, not about how fast a write reaches disk.

Queue package green three runs running; the rest of the tree green.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 15:13:33 -07:00

122 lines
3.8 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package setting
import (
"path/filepath"
"runtime"
"github.com/hanzoai/git/modules/json"
"github.com/hanzoai/git/modules/log"
)
// QueueSettings represent the settings for a queue from the ini
type QueueSettings struct {
Name string // not an INI option, it is the name for [queue.the-name] section
Type string
Datadir string
ConnStr string // for leveldb or kv
Length int // max queue length before blocking
QueueName, SetName string // the name suffix for storage (db key, kv key), "set" is for unique queue
BatchLength int
MaxWorkers int
}
func GetQueueSettings(rootCfg ConfigProvider, name string) (QueueSettings, error) {
queueSettingsDefault := QueueSettings{
Type: "zapdb", // zapdb (the queue) or dummy/immediate (run inline)
Datadir: "queues/common", // relative to AppDataPath
Length: 100000, // queue length before a channel queue will block
QueueName: "_queue",
SetName: "_unique",
BatchLength: 20,
MaxWorkers: runtime.NumCPU() / 2,
}
if queueSettingsDefault.MaxWorkers < 1 {
queueSettingsDefault.MaxWorkers = 1
}
if queueSettingsDefault.MaxWorkers > 10 {
queueSettingsDefault.MaxWorkers = 10
}
// deep copy default settings
cfg := QueueSettings{}
if cfgBs, err := json.Marshal(queueSettingsDefault); err != nil {
return cfg, err
} else if err = json.Unmarshal(cfgBs, &cfg); err != nil {
return cfg, err
}
cfg.Name = name
if sec, err := rootCfg.GetSection("queue"); err == nil {
if err = sec.MapTo(&cfg); err != nil {
log.Error("Failed to map queue common config for %q: %v", name, err)
return cfg, nil
}
}
if sec, err := rootCfg.GetSection("queue." + name); err == nil {
if err = sec.MapTo(&cfg); err != nil {
log.Error("Failed to map queue spec config for %q: %v", name, err)
return cfg, nil
}
if sec.HasKey("CONN_STR") {
cfg.ConnStr = sec.Key("CONN_STR").String()
}
}
if cfg.Datadir == "" {
cfg.Datadir = queueSettingsDefault.Datadir
}
if !filepath.IsAbs(cfg.Datadir) {
cfg.Datadir = filepath.Join(AppDataPath, cfg.Datadir)
}
cfg.Datadir = filepath.ToSlash(cfg.Datadir)
if cfg.Type == "kv" && cfg.ConnStr == "" {
cfg.ConnStr = "kv://127.0.0.1:6379/0"
}
if cfg.Length <= 0 {
cfg.Length = queueSettingsDefault.Length
}
if cfg.MaxWorkers <= 0 {
cfg.MaxWorkers = queueSettingsDefault.MaxWorkers
}
if cfg.BatchLength <= 0 {
cfg.BatchLength = queueSettingsDefault.BatchLength
}
return cfg, nil
}
func LoadQueueSettings() {
loadQueueFrom(CfgProvider)
}
func loadQueueFrom(rootCfg ConfigProvider) {
hasOld := false
handleOldLengthConfiguration := func(rootCfg ConfigProvider, newQueueName, oldSection, oldKey string) {
if rootCfg.Section(oldSection).HasKey(oldKey) {
hasOld = true
log.Error("Removed queue option: `[%s].%s`. Use new options in `[queue.%s]`", oldSection, oldKey, newQueueName)
}
}
handleOldLengthConfiguration(rootCfg, "issue_indexer", "indexer", "ISSUE_INDEXER_QUEUE_TYPE")
handleOldLengthConfiguration(rootCfg, "issue_indexer", "indexer", "ISSUE_INDEXER_QUEUE_BATCH_NUMBER")
handleOldLengthConfiguration(rootCfg, "issue_indexer", "indexer", "ISSUE_INDEXER_QUEUE_DIR")
handleOldLengthConfiguration(rootCfg, "issue_indexer", "indexer", "ISSUE_INDEXER_QUEUE_CONN_STR")
handleOldLengthConfiguration(rootCfg, "issue_indexer", "indexer", "UPDATE_BUFFER_LEN")
handleOldLengthConfiguration(rootCfg, "mailer", "mailer", "SEND_BUFFER_LEN")
handleOldLengthConfiguration(rootCfg, "pr_patch_checker", "repository", "PULL_REQUEST_QUEUE_LENGTH")
handleOldLengthConfiguration(rootCfg, "mirror", "repository", "MIRROR_QUEUE_LENGTH")
if hasOld {
log.Fatal("Please update your app.ini to remove deprecated config options")
}
}