My current Go program has a bug when I cancel a context, the CPU goes wild without stopping what's supposed to be stopped. Simple context/select statements are easy but they could become a nightmare in real world applications. Last time my CPU spiked, it was caused by an infinite loop without a sleep statement.
=> More informations about this toot | More toots from jriou@hachyderm.io
I may know where the CPU cycles are spent. There's a select statement waiting for a context to be canceled and a for loop with a sleep function taking a context too. The sleep is skipped when the context is closed but the infinite loop doesn't end. I should move the for loop outside of the select and use the regular sleep function. Let's see that tomorrow 💡
=> More informations about this toot | More toots from jriou@hachyderm.io
That was it!
=> More informations about this toot | More toots from jriou@hachyderm.io
The solution looks like this:
for {
select {
case <-ctx.Done():
// cancelation work
return
default:
run()
time.Sleep(...) // remove WithContext
}
}
=> More informations about this toot | More toots from jriou@hachyderm.io This content has been proxied by September (3851b).Proxy Information
text/gemini