From a0865df33c51170ccc19d8624a290dff0a312209 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sat, 18 Apr 2020 12:44:52 -0700 Subject: [PATCH] Change default concurrency to the number of CPUs --- CHANGELOG.md | 1 + server.go | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a372919..3578243 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - CLI is renamed to `asynq`. - To upgrade the CLI to the latest version run `go get -u github.com/hibiken/tools/asynq` - The `ps` command in CLI is renamed to `servers` +- `Concurrency` defaults to the number of CPUs when unset or set to a negative value. ### Added diff --git a/server.go b/server.go index 9a10008..50fc27a 100644 --- a/server.go +++ b/server.go @@ -11,6 +11,7 @@ import ( "math" "math/rand" "os" + "runtime" "sync" "time" @@ -51,7 +52,8 @@ type Server struct { type Config struct { // Maximum number of concurrent processing of tasks. // - // If set to a zero or negative value, NewServer will overwrite the value to one. + // If set to a zero or negative value, NewServer will overwrite the value + // to the number of CPUs usable by the currennt process. Concurrency int // Function to calculate retry delay for a failed task. @@ -168,7 +170,7 @@ const defaultShutdownTimeout = 8 * time.Second func NewServer(r RedisConnOpt, cfg Config) *Server { n := cfg.Concurrency if n < 1 { - n = 1 + n = runtime.NumCPU() } delayFunc := cfg.RetryDelayFunc if delayFunc == nil {