标签:参考 select href ogg cron struct level pack 链接
https://juejin.im/post/5ef6eed9f265da22a8513d57
直接参考上面的链接即可
这段代码的意思是如果到了该执行下一次任务时上次任务还在执行则跳过下次任务,这种写法还是第一次看到
// SkipIfStillRunning skips an invocation of the Job if a previous invocation is
// still running. It logs skips to the given logger at Info level.
func SkipIfStillRunning(logger Logger) JobWrapper {
return func(j Job) Job {
var ch = make(chan struct{}, 1)
ch <- struct{}{}
return FuncJob(func() {
select {
case v := <-ch:
j.Run()
ch <- v
default:
logger.Info("skip")
}
})
}
}
仿照实现
package main
import (
"fmt"
"time"
)
func main() {
skipIfStillRunning()
}
func skipIfStillRunning() {
m := make(chan struct{}, 1)
m <- struct{}{}
for {
select {
case v := <-m:
fmt.Println("running")
time.Sleep(time.Second * 2)
m <- v
default:
fmt.Println("skip")
}
}
}
蛮有意思的这个库
标签:参考 select href ogg cron struct level pack 链接
原文地址:https://www.cnblogs.com/CherryTab/p/13216190.html