Documentation

Configuration

Queue capacity, worker count, timeouts, and named presets.

Config

Pass a *Config to New. A nil config uses all defaults.

type Config struct {
    Workers int                     // default = CPU * 2
    Size    int                     // default = Workers * 64
    Timeout time.Duration           // default = 30 * seconds
    Preset  map[string]PresetConfig // default = empty
}
Field Type Default Description
Workers int runtime.NumCPU() * 2 Number of worker goroutines
Size int Workers * 64 Maximum pending heap capacity
Timeout time.Duration 30 * time.Second Base timeout used for derivation and promotion
Preset map[string]PresetConfig empty map Named priority and timeout presets

Zero values for Workers, Size, and Timeout fall back to defaults. A non-nil Preset map is deep-copied into the queue config.

PresetConfig

type PresetConfig struct {
    Priority Priority
    Timeout  time.Duration
}
Field Description
Priority Task priority when this preset name is used in Enqueue
Timeout When > 0, overrides base Config.Timeout before priority scaling; when 0, uses base timeout

Example

q := goQueue.New(&goQueue.Config{
    Workers: 4,
    Size:    256,
    Timeout: 30 * time.Second,
    Preset: map[string]goQueue.PresetConfig{
        "urgent": {Priority: goQueue.PriorityImmediate},
        "batch":  {Priority: goQueue.PriorityLow, Timeout: 60 * time.Second},
    },
})

Timeout Derivation

Resolved timeout for each enqueue (before WithTimeout override):

  1. Start from Config.Timeout.
  2. If the preset name exists and PresetConfig.Timeout > 0, use that value instead.
  3. Scale by the preset's Priority.
  4. Clamp to 15s–120s.
Priority Formula
Immediate timeout / 4
High timeout / 2
Retry timeout / 2
Normal timeout
Low timeout * 2

WithTimeout(d) replaces the derived value entirely for that enqueue.

Priority Promotion Thresholds

Promotion wait times are derived once at New from Config.Timeout:

From Wait To
Low clamp(Timeout, 30s, 120s) Normal
Normal clamp(Timeout*2, 30s, 120s) High

Changing Timeout after New has no effect — the promotion map is fixed at construction.

Enqueue Options

Per-task overrides applied at enqueue time:

Option Effect
WithTaskID(id) Sets task ID; empty/omitted → auto UUID
WithTimeout(d) Overrides derived timeout
WithCallback(fn) Runs fn(id) in a new goroutine after success
WithRetry(max...) Enables retry; optional max (default 3)

Defaults Summary

Setting Default
Workers runtime.NumCPU() * 2
Size Workers * 64
Timeout 30s
Min derived timeout 15s
Max derived timeout 120s
Retry max (when enabled, no arg) 3
Empty preset name priority PriorityImmediate (zero value of unset map entry)

See Also

中文