From 4ed64089dccf53c39a09835b0c09d9c38ca0afc6 Mon Sep 17 00:00:00 2001 From: Carlos Nihelton Date: Wed, 28 Aug 2024 15:59:00 -0300 Subject: [PATCH] Make the task wait channel buffered. Tests has shown that's possible to have writers reaching the channel before any reader, thus the notification may never arrive. The amount of writers is a best-guess at this moment and may be adjusted. --- .../internal/distros/worker/task_queue.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/windows-agent/internal/distros/worker/task_queue.go b/windows-agent/internal/distros/worker/task_queue.go index 74f2b9bf5..1cbaf39ee 100644 --- a/windows-agent/internal/distros/worker/task_queue.go +++ b/windows-agent/internal/distros/worker/task_queue.go @@ -21,10 +21,17 @@ type taskQueue struct { data []task.Task } +// newWaitChannel creates a channel to notify waiters of new tasks. +func newWaitChannel() chan struct{} { + // Tests has shown that's possible to have writers reaching the channel before any reader, thus the notification may never arrive. + // The amount of writers is a best-guess at this moment and may be adjusted. + return make(chan struct{}, 4) +} + func newTaskQueue() *taskQueue { return &taskQueue{ mu: sync.RWMutex{}, - wait: make(chan struct{}), + wait: newWaitChannel(), data: make([]task.Task, 0), } } @@ -35,7 +42,7 @@ func (q *taskQueue) Load(newData []task.Task) { defer q.mu.Unlock() close(q.wait) - q.wait = make(chan struct{}) + q.wait = newWaitChannel() q.data = newData } @@ -51,11 +58,11 @@ func (q *taskQueue) Absorb(other *taskQueue) { transferedData := other.data close(other.wait) - other.wait = make(chan struct{}) + other.wait = newWaitChannel() other.data = make([]task.Task, 0) close(q.wait) - q.wait = make(chan struct{}) + q.wait = newWaitChannel() q.data = append(q.data, transferedData...) }