github.com/m-lab/locate@v0.17.6/limits/cron.go (about)

     1  package limits
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/aptible/supercronic/cronexpr"
     7  )
     8  
     9  // NewCron returns a new instance of Cron.
    10  func NewCron(schedule string, duration time.Duration) *Cron {
    11  	return &Cron{
    12  		Expression: cronexpr.MustParse(schedule),
    13  		duration:   duration,
    14  	}
    15  }
    16  
    17  // Cron infers time limits based on a cron schedule.
    18  type Cron struct {
    19  	*cronexpr.Expression
    20  	duration time.Duration
    21  }
    22  
    23  // Agents holds the cron limits for a set of user agents.
    24  type Agents map[string]*Cron
    25  
    26  // IsLimited returns whether the input time is within a time-limited
    27  // window [start, end).
    28  func (c *Cron) IsLimited(t time.Time) bool {
    29  	start := c.Next(t.Add(-c.duration))
    30  	end := start.Add(c.duration)
    31  	return (t.Equal(start) || t.After(start)) && t.Before(end)
    32  }