From eb6a5032f60e8292d514bbadbc6df716d3e18d7f Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Tue, 19 Nov 2019 07:46:04 -0800 Subject: [PATCH] Make Launcher thread safe --- asynq.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/asynq.go b/asynq.go index 41d0377..7aeb4c6 100644 --- a/asynq.go +++ b/asynq.go @@ -15,6 +15,7 @@ import ( "math" "math/rand" "strconv" + "sync" "time" "github.com/go-redis/redis/v7" @@ -105,8 +106,9 @@ func (c *Client) enqueue(msg *taskMessage, executeAt time.Time) error { type Launcher struct { rdb *redis.Client - // running indicates whether the workes are currently running. + // running indicates whether manager and poller are both running. running bool + mu sync.Mutex poller *poller @@ -135,6 +137,8 @@ type TaskHandler func(*Task) error // Start starts the manager and poller. func (l *Launcher) Start(handler TaskHandler) { + l.mu.Lock() + defer l.mu.Unlock() if l.running { return } @@ -147,6 +151,8 @@ func (l *Launcher) Start(handler TaskHandler) { // Stop stops both manager and poller. func (l *Launcher) Stop() { + l.mu.Lock() + defer l.mu.Unlock() if !l.running { return }