job: better limiter

pull/45/head
miraclesu 2017-07-18 11:47:18 +08:00
parent 6dc6680f99
commit d98b8c5b37
1 changed files with 3 additions and 5 deletions

8
job.go
View File

@ -167,15 +167,13 @@ func (j *Job) limit() bool {
return false
}
// 更精确的控制是加锁
// 两次运行时间极为接近的任务才可能出现控制不精确的情况
count := atomic.LoadInt64(j.Count)
if j.Parallels <= count {
count := atomic.AddInt64(j.Count, 1)
if j.Parallels < count {
atomic.AddInt64(j.Count, -1)
j.Fail(time.Now(), fmt.Sprintf("job[%s] running on[%s] running:[%d]", j.Key(), j.runOn, count))
return true
}
atomic.AddInt64(j.Count, 1)
return false
}