{"record":{"id":"8423307327f673fc","repo":"panjf2000/ants","slug":"invalid-expiry-for-pool","errorCode":null,"errorMessage":"invalid expiry for pool","messagePattern":"invalid expiry for pool","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ants.go","lineNumber":67,"sourceCode":"\n\t// DefaultCleanIntervalTime is the interval time to clean up goroutines.\n\tDefaultCleanIntervalTime = time.Second\n)\n\nconst (\n\t// OPENED represents that the pool is opened.\n\tOPENED = iota\n\n\t// CLOSED represents that the pool is closed.\n\tCLOSED\n)\n\nvar (\n\t// ErrLackPoolFunc will be returned when invokers don't provide function for pool.\n\tErrLackPoolFunc = errors.New(\"must provide function for pool\")\n\n\t// ErrInvalidPoolExpiry will be returned when setting a negative number as the periodic duration to purge goroutines.\n\tErrInvalidPoolExpiry = errors.New(\"invalid expiry for pool\")\n\n\t// ErrPoolClosed will be returned when submitting task to a closed pool.\n\tErrPoolClosed = errors.New(\"this pool has been closed\")\n\n\t// ErrPoolOverload will be returned when the pool is full and no workers available.\n\tErrPoolOverload = errors.New(\"too many goroutines blocked on submit or Nonblocking is set\")\n\n\t// ErrInvalidPreAllocSize will be returned when trying to set up a negative capacity under PreAlloc mode.\n\tErrInvalidPreAllocSize = errors.New(\"can not set up a negative capacity under PreAlloc mode\")\n\n\t// ErrTimeout will be returned after the operations timed out.\n\tErrTimeout = errors.New(\"operation timed out\")\n\n\t// ErrInvalidPoolIndex will be returned when trying to retrieve a pool with an invalid index.\n\tErrInvalidPoolIndex = errors.New(\"invalid pool index\")\n\n\t// ErrInvalidLoadBalancingStrategy will be returned when trying to create a MultiPool with an invalid load-balancing strategy.\n\tErrInvalidLoadBalancingStrategy = errors.New(\"invalid load-balancing strategy\")","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/panjf2000/ants/blob/107e37678122b00e1b365636ce4f80623ba41579/ants.go#L49-L85","documentation":"ErrInvalidPoolExpiry is returned when a negative duration is configured as the pool's expiry (purge) interval. ants periodically purges idle workers; a negative ExpiryDuration is meaningless, so newPool (ants.go:216) rejects it at construction. Zero is allowed and falls back to DefaultCleanIntervalTime.","triggerScenarios":"ants.NewPool(-1, ants.WithExpiryDuration(-1)); any NewPool/NewPoolWithFunc/NewPoolWithFuncGeneric/NewMultiPool* call with WithExpiryDuration of a negative duration (checked inside newPool only when purge is enabled, i.e. DisablePurge is false).","commonSituations":"Computing the expiry from config/environment where a negative value slips through; accidentally swapping arguments so a size like -1 lands in the duration; copy-pasting test configs into production.","solutions":["Pass a positive duration to WithExpiryDuration (or omit it to use DefaultCleanIntervalTime).","Validate/normalize config-sourced durations: if <= 0, use time.Duration(0) or a sane default before constructing the pool.","Alternatively set WithDisablePurge(true) if you never want purging, which bypasses the expiry check."],"exampleFix":"// before\npool, err := ants.NewPool(1000, ants.WithExpiryDuration(cfg.PurgeInterval))\n// after\nexpiry := cfg.PurgeInterval\nif expiry < 0 {\n    expiry = 0 // ants falls back to DefaultCleanIntervalTime\n}\npool, err := ants.NewPool(1000, ants.WithExpiryDuration(expiry))","handlingStrategy":"validation","validationCode":"if expiry < 0 {\n    return fmt.Errorf(\"expiry duration must be >= 0, got %s\", expiry)\n}\npool, err := ants.NewPool(size, ants.WithExpiryDuration(expiry))","typeGuard":null,"tryCatchPattern":"if errors.Is(err, ants.ErrInvalidPoolExpiry) {\n    // fix configuration and rebuild the pool\n}","preventionTips":["Centralize pool option construction in one helper that clamps negative durations.","Validate duration config fields at startup before any pool is created.","Remember 0 means 'use ants.DefaultCleanIntervalTime'; use it instead of negatives."],"tags":["go","validation","duration","configuration"],"backgroundTag":"invalid-config-value","analyzedSha":"107e37678122b00e1b365636ce4f80623ba41579","analyzedAt":"2026-09-06T13:28:28.492Z","contentChangedAt":"2026-09-06T13:28:28.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}