Skip to content

Commit

Permalink
Fix concurrent map writes (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Chunhai Zhang <[email protected]>
  • Loading branch information
zchunhai and Chunhai Zhang authored Oct 11, 2023
1 parent ae4273c commit 69a274a
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type Consumer struct {

handlerFuncMap map[string]HandlerFunc

activeMutex sync.RWMutex
activeTasks map[uuid.UUID]struct{}

visibilityTimeout time.Duration
Expand Down Expand Up @@ -110,6 +111,7 @@ func (c *Client) NewConsumer() *Consumer {

handlerFuncMap: make(map[string]HandlerFunc),

activeMutex: sync.RWMutex{},
activeTasks: make(map[uuid.UUID]struct{}),

visibilityTimeout: defaultVisibilityTimeout,
Expand Down Expand Up @@ -335,7 +337,9 @@ func (c *Consumer) getActiveTaskCount() int {
}

func (c *Consumer) removeFromActiveTasks(task *Task) {
c.activeMutex.Lock()
delete(c.activeTasks, task.ID)
c.activeMutex.Unlock()
}

func (c *Consumer) getActiveTaskIDs() []uuid.UUID {
Expand Down Expand Up @@ -468,7 +472,9 @@ func (c *Consumer) activateTask(ctx context.Context, task *Task) error {
return err
}

c.activeMutex.Lock()
c.activeTasks[task.ID] = struct{}{}
c.activeMutex.Unlock()

c.channel <- job

Expand Down

0 comments on commit 69a274a

Please sign in to comment.