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

     1  package schedule
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/yandex/pandora/core"
     7  )
     8  
     9  type ConstConfig struct {
    10  	Ops      float64       `validate:"min=0"`
    11  	Duration time.Duration `validate:"min-time=1ms"`
    12  }
    13  
    14  func NewConstConf(conf ConstConfig) core.Schedule {
    15  	return NewConst(conf.Ops, conf.Duration)
    16  }
    17  
    18  func NewConst(ops float64, duration time.Duration) core.Schedule {
    19  	if ops < 0 {
    20  		ops = 0
    21  	}
    22  	xn := float64(duration) / 1e9 // Seconds.
    23  	n := int64(ops * xn)
    24  	return NewDoAtSchedule(duration, n, constDoAt(ops))
    25  }
    26  
    27  func constDoAt(ops float64) func(i int64) time.Duration {
    28  	billionDivOps := 1e9 / ops
    29  	return func(i int64) time.Duration {
    30  		return time.Duration(float64(i) * billionDivOps)
    31  		//return time.Duration(float64(i) * 1e9 / ops)
    32  	}
    33  }