period.go 532 B

12345678910111213141516171819202122232425262728
  1. package dispatcher
  2. import (
  3. "math/rand"
  4. "time"
  5. )
  6. type periodChooser struct {
  7. period time.Duration
  8. epsilon time.Duration
  9. rand *rand.Rand
  10. }
  11. func newPeriodChooser(period, eps time.Duration) *periodChooser {
  12. return &periodChooser{
  13. period: period,
  14. epsilon: eps,
  15. rand: rand.New(rand.NewSource(time.Now().UnixNano())),
  16. }
  17. }
  18. func (pc *periodChooser) Choose() time.Duration {
  19. var adj int64
  20. if pc.epsilon > 0 {
  21. adj = rand.Int63n(int64(2*pc.epsilon)) - int64(pc.epsilon)
  22. }
  23. return pc.period + time.Duration(adj)
  24. }