Unwanted 100% CPU processus
Closed this issue · 0 comments
fljdin commented
The main worker's loop is wrong with a default entry. The queue.Pop()
should be called by another goroutine, like observer, to consume any new task from a chanel instead of defaulting an action.
func (w *Worker) Start() {
w.memory.StartWorker()
defer w.memory.EndWorker()
for {
select {
case <-w.context.Done():
return
default:
if task, ok := w.memory.queue.Pop(); ok {
w.handle(task)
}
}
}
}
Possibles solutions:
- https://devsolus.com/2023/03/02/for-select-reaches-100-cpu-usage-due-to-empty-default-definition-in-golang/
- https://stackoverflow.com/questions/75613329/for-select-reaches-100-cpu-usage-due-to-empty-default-definition
- https://stackoverflow.com/questions/55367231/golang-for-select-loop-consumes-100-of-cpu