github.com/yandex/pandora@v0.5.32/core/schedule/unlilmited.go (about)

     1  package schedule
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/yandex/pandora/core"
     7  	"go.uber.org/atomic"
     8  )
     9  
    10  // NewUnlimited returns schedule that generates unlimited ops for passed duration.
    11  func NewUnlimited(duration time.Duration) core.Schedule {
    12  	return &unlimitedSchedule{duration: duration, finish: atomic.NewTime(time.Now())}
    13  }
    14  
    15  type UnlimitedConfig struct {
    16  	Duration time.Duration `validate:"min-time=1ms"`
    17  }
    18  
    19  func NewUnlimitedConf(conf UnlimitedConfig) core.Schedule {
    20  	return NewUnlimited(conf.Duration)
    21  }
    22  
    23  type unlimitedSchedule struct {
    24  	duration time.Duration
    25  
    26  	StartSync
    27  	finish *atomic.Time
    28  }
    29  
    30  func (s *unlimitedSchedule) Start(startAt time.Time) {
    31  	s.MarkStarted()
    32  	s.startOnce.Do(func() {
    33  		s.finish.Store(startAt.Add(s.duration))
    34  	})
    35  }
    36  
    37  func (s *unlimitedSchedule) Next() (tx time.Time, ok bool) {
    38  	s.startOnce.Do(func() {
    39  		s.MarkStarted()
    40  		s.finish.Store(time.Now().Add(s.duration))
    41  	})
    42  	now := time.Now()
    43  	if now.Before(s.finish.Load()) {
    44  		return now, true
    45  	}
    46  	return s.finish.Load(), false
    47  }
    48  
    49  func (s *unlimitedSchedule) Left() int {
    50  	if !s.IsStarted() || time.Now().Before(s.finish.Load()) {
    51  		return -1
    52  	}
    53  	return 0
    54  }