2019-04-24 15:09:41 +03:00
|
|
|
package deadline
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
// Deadline allows for commands to expire when they run too long
|
|
|
|
type Deadline struct {
|
|
|
|
unixNano int64
|
|
|
|
hit bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new deadline object
|
2019-10-30 20:17:59 +03:00
|
|
|
func New(dl time.Time) *Deadline {
|
|
|
|
return &Deadline{unixNano: dl.UnixNano()}
|
2019-04-24 15:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the deadline and panic when reached
|
2022-09-13 03:06:27 +03:00
|
|
|
//
|
2019-04-24 15:09:41 +03:00
|
|
|
//go:noinline
|
2019-10-30 20:17:59 +03:00
|
|
|
func (dl *Deadline) Check() {
|
|
|
|
if dl == nil || dl.unixNano == 0 {
|
2019-04-24 15:09:41 +03:00
|
|
|
return
|
|
|
|
}
|
2019-10-30 20:17:59 +03:00
|
|
|
if !dl.hit && time.Now().UnixNano() > dl.unixNano {
|
|
|
|
dl.hit = true
|
2019-04-24 15:09:41 +03:00
|
|
|
panic("deadline")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hit returns true if the deadline has been hit
|
2019-10-30 20:17:59 +03:00
|
|
|
func (dl *Deadline) Hit() bool {
|
|
|
|
return dl.hit
|
2019-04-24 15:09:41 +03:00
|
|
|
}
|
2019-04-25 03:00:52 +03:00
|
|
|
|
2019-10-30 20:17:59 +03:00
|
|
|
// GetDeadlineTime returns the time object for the deadline, and an
|
|
|
|
// "empty" boolean
|
|
|
|
func (dl *Deadline) GetDeadlineTime() time.Time {
|
|
|
|
return time.Unix(0, dl.unixNano)
|
2019-04-25 03:00:52 +03:00
|
|
|
}
|