Skip to content

Commit

Permalink
Make the task wait channel buffered.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CarlosNihelton committed Aug 28, 2024
1 parent 400acf7 commit 4ed6408
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions windows-agent/internal/distros/worker/task_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand All @@ -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
}
Expand All @@ -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...)
}

Expand Down

0 comments on commit 4ed6408

Please sign in to comment.