package monitor import ( "encoding/json" "errors" "time" "github.com/golang-collections/go-datastructures/queue" ) type Duration struct { time.Duration } func (d *Duration) MarshalJSON() ([]byte, error) { return json.Marshal(d.Duration) } func (d *Duration) UnmarshalJSON(b []byte) error { var v interface{} if err := json.Unmarshal(b, &v); err != nil { return err } switch value := v.(type) { case float64: d.Duration = time.Duration(value) return nil case string: var err error d.Duration, err = time.ParseDuration(value) if err != nil { return err } return nil default: return errors.New("invalid duration") } } type Item struct { URL string Interval Duration next time.Time } func NewItem(URL string, Interval time.Duration) *Item { return &Item{ URL: URL, Interval: Duration{Interval}, next: time.Now(), } } func (item *Item) Compare(other queue.Item) int { j, ok := other.(*Item) if !ok { return 0 } if item.next.Before(j.next) { return -1 } else if item.next.After(j.next) { return 1 } if item.Interval.Duration < j.Interval.Duration { return -1 } else if item.Interval.Duration > j.Interval.Duration { return 1 } return 0 } func (item *Item) increment() *Item { item.next = time.Now().Add(item.Interval.Duration) return item }