coredns/coredns · error

prefetch amount should be positive: %d

Error message

prefetch amount should be positive: %d

What it means

The cache plugin's `prefetch amount [duration [percentage]]` option requires the prefetch amount to be positive (or zero, which the check `amount < 0` permits — negative amounts are rejected). cacheParse returns this error at setup when the first prefetch argument is a negative integer.

Source

Thrown at plugin/cache/setup.go:147

						}
						// Reserve < 0
						if minnttl < 0 {
							return nil, fmt.Errorf("cache min TTL can not be negative: %d", minnttl)
						}
						ca.minnttl = time.Duration(minnttl) * time.Second
					}
				}
			case "prefetch":
				args := c.RemainingArgs()
				if len(args) == 0 || len(args) > 3 {
					return nil, c.ArgErr()
				}
				amount, err := strconv.Atoi(args[0])
				if err != nil {
					return nil, err
				}
				if amount < 0 {
					return nil, fmt.Errorf("prefetch amount should be positive: %d", amount)
				}
				ca.prefetch = amount

				if len(args) > 1 {
					dur, err := time.ParseDuration(args[1])
					if err != nil {
						return nil, err
					}
					ca.duration = dur
				}
				if len(args) > 2 {
					pct := args[2]
					if x := pct[len(pct)-1]; x != '%' {
						return nil, fmt.Errorf("last character of percentage should be `%%`, but is: %q", x)
					}
					pct = pct[:len(pct)-1]

					num, err := strconv.Atoi(pct)

View on GitHub (pinned to 558c9757a9)

Solutions

  1. Set the prefetch amount to a positive integer, e.g. `prefetch 1` or `prefetch 10 1m 10%`.
  2. Remove the prefetch line if you do not want prefetching.
  3. Check template variables that supply the amount.
  4. Note amount 0 passes validation but effectively makes prefetch useless; prefer removing the option.

Example fix

// before (Corefile)
cache 30
    prefetch -1

// after
cache 30
    prefetch 1
Defensive patterns

Strategy: validation

Validate before calling

const amount = 1; if (!Number.isInteger(amount) || amount < 0) { throw new Error(`prefetch amount must be a non-negative integer, got ${amount}`); }

Prevention

When it happens

Trigger: Corefile line like `prefetch -1` or `prefetch -10 1m` inside a cache block where strconv.Atoi(args[0]) yields a negative number.

Common situations: Sign typos, templated configs where a variable meant to be a count expands negative, or misunderstanding the option semantics (0 means disabled-ish, negative is invalid).

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of coredns/coredns@558c9757a9 (2026-09-06). Data as JSON: /api/errors/bea5944497d569e8. Report an issue: GitHub.