我有这个循环,它的作用是试图重复轮询另一台服务器。我使用 Ticker 来实现这一点,但是程序反复显示 100% 的 CPU 使用率。
这个代码运行在一个 goroutine 中。并且 HTTP 服务器在另一个 goroutine 中运行。
func() Monitor() {
abort := make(chan bool)
log.Info("Monitor started.")
// start the monitor goroutine
go func() {
defer log.Info("Stopped monitor")
ticker := time.NewTicker(time.Duration(35.0) * time.Second)
defer ticker.Stop()
log.Info("Monitor started! \n")
for {
select {
case t := <-ticker.C:
log.Infof("Subscribe to service at time %v\n", t)
if err := selfConn.SubscribeToService(); err != nil {
log.Errorf("Failed to subscribe to primary connector: %v", err)
}
case <-abort:
log.Info("Finished routine!") …Run Code Online (Sandbox Code Playgroud)