forked from Sagleft/simple-cron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimiter.go
More file actions
39 lines (32 loc) · 724 Bytes
/
limiter.go
File metadata and controls
39 lines (32 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package simplecron
import "time"
// RunTimeLimitHandler - func runtime limit handler
type RunTimeLimitHandler struct {
timeout time.Duration
runFunc func()
}
// NewRuntimeLimitHandler - create new func runtime limit handler
func NewRuntimeLimitHandler(timeout time.Duration, runFunc func()) *RunTimeLimitHandler {
return &RunTimeLimitHandler{
timeout: timeout,
runFunc: runFunc,
}
}
// Run - run func & limit runtime.
// returns: bool: true if time is up
func (r *RunTimeLimitHandler) Run() bool {
timer := time.NewTimer(r.timeout)
done := make(chan bool, 1)
go func() {
<-timer.C
done <- true
}()
go func() {
r.runFunc()
done <- false
if !timer.Stop() {
<-timer.C
}
}()
return <-done
}