Plan: Sleep 2.5 hours in a for
loop
Implementation:
package test
import (
"context"
"time"
)
const connChkInterval = 2.5 * time.Hour.Hours()
func testFunc(ctx context.Context) {
_Loop:
for {
select {
case <-ctx.Done():
break _Loop
default:
// code of default branch
}
time.Sleep(connChkInterval)
}
}
Issue:
- CPU usage goes high with the code
- Did analysis with golang profile, and saw
runtime/internal/syscall.Syscall
is the root cause - The CPU usage never went to so high ever even though I run SQLite and the Golang App on the same server
Troubleshooting:
Did search on google, and most of posts are not helpful to me. So I decided to go to the hard way: revert my changes line by line
Light:
The cpu usage went back to normal after I revert my change on connChkInterval
package test
import (
"context"
"time"
)
const connChkInterval = 3 * time.Hour.Hours()
func testFunc(ctx context.Context) {
_Loop:
for {
select {
case <-ctx.Done():
break _Loop
default:
// code of default branch
}
time.Sleep(connChkInterval)
}
}
Scan the QR code using WeChat