分享
log4go的精确定时程序(带自动延迟补偿)
ccpaging · · 1392 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
程序设计目标是在程序启动10秒后执行某个任务,例如日志转储(rotate),以后每隔15秒执行一次。
初次的设计
package main
import (
"time"
"fmt"
)
func main() {
timer := time.NewTimer(10 * time.Second)
fmt.Println(time.Now())
for {
select {
case <-timer.C:
fmt.Println(time.Now())
time.Sleep(1 * time.Second)
timer.Reset(15 * time.Second)
}
}
}显然,在设置下一个定时(timer.Reset(15 * time.Second))之前,多少都会消耗一点时间,即使是毫秒级的延迟,也一定会产生累计误差。
为了使问题更明显,在程序中增加了一秒的 Sleep。测试结果如下:
2017年07月18日 23:16:24.791623 +0800 CST
2017年07月18日 23:16:34.7917567 +0800 CST
2017年07月18日 23:16:50.7920782 +0800 CST
2017年07月18日 23:17:06.7929373 +0800 CST
2017年07月18日 23:17:22.7944063 +0800 CST
2017年07月18日 23:17:38.7951302 +0800 CST
2017年07月18日 23:17:54.7968096 +0800 CST
2017年07月18日 23:18:10.7985468 +0800 CST
2017年07月18日 23:18:26.7993588 +0800 CST
2017年07月18日 23:18:42.7996568 +0800 CST
2017年07月18日 23:18:58.8008621 +0800 CST
改进后的程序
package main
import (
"time"
"fmt"
)
func main() {
next := time.Now().Add(10 * time.Second)
timer := time.NewTimer(next.Sub(time.Now()))
fmt.Println(time.Now())
for {
select {
case <-timer.C:
fmt.Println(time.Now())
time.Sleep(1 * time.Second)
next = next.Add(15 * time.Second)
timer.Reset(next.Sub(time.Now()))
}
}
}简单说就是增加了一个时间变量 next,在设置定时器之前,通过计算获得下一次任务的执行时间。
而定时器用 next - Now() 来设置。测试结果如下:
2017年07月18日 23:16:20.2456695 +0800 CST
2017年07月18日 23:16:30.2466397 +0800 CST
2017年07月18日 23:16:45.2457191 +0800 CST
2017年07月18日 23:17:00.2458328 +0800 CST
2017年07月18日 23:17:15.2451861 +0800 CST
2017年07月18日 23:17:30.2452624 +0800 CST
2017年07月18日 23:17:45.2468138 +0800 CST
2017年07月18日 23:18:00.245947 +0800 CST
从以上结果看,精度可控制在 ± 2ms。完全可以满足定时转储日志文件的需要。
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信1392 次点击
上一篇:Golang指针与nil浅析
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
程序设计目标是在程序启动10秒后执行某个任务,例如日志转储(rotate),以后每隔15秒执行一次。
初次的设计
package main
import (
"time"
"fmt"
)
func main() {
timer := time.NewTimer(10 * time.Second)
fmt.Println(time.Now())
for {
select {
case <-timer.C:
fmt.Println(time.Now())
time.Sleep(1 * time.Second)
timer.Reset(15 * time.Second)
}
}
}显然,在设置下一个定时(timer.Reset(15 * time.Second))之前,多少都会消耗一点时间,即使是毫秒级的延迟,也一定会产生累计误差。
为了使问题更明显,在程序中增加了一秒的 Sleep。测试结果如下:
2017年07月18日 23:16:24.791623 +0800 CST
2017年07月18日 23:16:34.7917567 +0800 CST
2017年07月18日 23:16:50.7920782 +0800 CST
2017年07月18日 23:17:06.7929373 +0800 CST
2017年07月18日 23:17:22.7944063 +0800 CST
2017年07月18日 23:17:38.7951302 +0800 CST
2017年07月18日 23:17:54.7968096 +0800 CST
2017年07月18日 23:18:10.7985468 +0800 CST
2017年07月18日 23:18:26.7993588 +0800 CST
2017年07月18日 23:18:42.7996568 +0800 CST
2017年07月18日 23:18:58.8008621 +0800 CST
改进后的程序
package main
import (
"time"
"fmt"
)
func main() {
next := time.Now().Add(10 * time.Second)
timer := time.NewTimer(next.Sub(time.Now()))
fmt.Println(time.Now())
for {
select {
case <-timer.C:
fmt.Println(time.Now())
time.Sleep(1 * time.Second)
next = next.Add(15 * time.Second)
timer.Reset(next.Sub(time.Now()))
}
}
}简单说就是增加了一个时间变量 next,在设置定时器之前,通过计算获得下一次任务的执行时间。
而定时器用 next - Now() 来设置。测试结果如下:
2017年07月18日 23:16:20.2456695 +0800 CST
2017年07月18日 23:16:30.2466397 +0800 CST
2017年07月18日 23:16:45.2457191 +0800 CST
2017年07月18日 23:17:00.2458328 +0800 CST
2017年07月18日 23:17:15.2451861 +0800 CST
2017年07月18日 23:17:30.2452624 +0800 CST
2017年07月18日 23:17:45.2468138 +0800 CST
2017年07月18日 23:18:00.245947 +0800 CST
从以上结果看,精度可控制在 ± 2ms。完全可以满足定时转储日志文件的需要。