Go 中Ticker的实际运用
type TaskInterface interface {
TaskTimeTicker()
}
type taskImpl struct {
extraTaskDao dao.IExtraTask
taskDao dao.ITask
}
func (impl *taskImpl) TaskTimeTicker() {
go local.LoadTableData()
ticker := time.NewTicker(time.Second * 5)
defer ticker.Stop()
tickTime := 0
for range ticker.C {
tickTime++
if tickTime == 120 {
go local.LoadTableData()
tickTime = 0
}
go impl.ExecuteTask()
}
}
func NewTaskTickerHandler() TaskInterface {
return &taskImpl{
extraTaskDao: impl.NewExtraTaskDao(),
taskDao: impl.NewTaskDao(),
}
}
tickerLogic := NewTaskTickerHandler()
go tickerLogic.TaskTimeTicker()