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
|
|
|
|
func New(deadline time.Time) *Deadline {
|
|
|
|
return &Deadline{unixNano: deadline.UnixNano()}
|
|
|
|
}
|
|
|
|
|
2019-04-24 23:20:57 +03:00
|
|
|
func Empty() *Deadline {
|
|
|
|
return &Deadline{}
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:02:39 +03:00
|
|
|
// Update the deadline from a given time object
|
|
|
|
func (deadline *Deadline) Update(newDeadline time.Time) {
|
|
|
|
deadline.unixNano = newDeadline.UnixNano()
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:09:41 +03:00
|
|
|
// Check the deadline and panic when reached
|
|
|
|
//go:noinline
|
|
|
|
func (deadline *Deadline) Check() {
|
|
|
|
if deadline == nil || deadline.unixNano == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !deadline.hit && time.Now().UnixNano() > deadline.unixNano {
|
|
|
|
deadline.hit = true
|
|
|
|
panic("deadline")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hit returns true if the deadline has been hit
|
|
|
|
func (deadline *Deadline) Hit() bool {
|
|
|
|
return deadline.hit
|
|
|
|
}
|