func newRateLimiter(perSec int, maxBurst time.Duration) *rateLimiter { maxPerBatch := int64(perSec / int(time.Second/maxBurst)) return &rateLimiter{ limitPerSec: perSec, resolution: maxBurst, time: clock.New(), maxPerBatch: maxPerBatch, } }
// Profile will wrap a writer and reader pair and profile where // time is spent: writing or reading. The result is returned when // the `done` func is called. The `done` func can be called multiple // times. // // There is a small performance overhead of ~µs per Read/Write call. // This is negligible in most I/O workloads. If the overhead is too // much for your needs, use the `ProfileSample` call. func Profile(w io.Writer, r io.Reader) (pw io.Writer, pr io.Reader, done func() TimeProfile) { return profile(clock.New(), w, r) }
// ProfileSample will wrap a writer and reader pair and collect // samples of where time is spent: writing or reading. The result // is an approximation that is returned when the `done` func is // called. The `done` func can be called *only once*. // // This call is not as precise as the `Profile` call, but the // performance overhead is much reduced. func ProfileSample(w io.Writer, r io.Reader, res time.Duration) (pw io.Writer, pr io.Reader, done func() SamplingProfile) { return profileSample(clock.New(), w, r, res) }
func newCounter() *rateCounter { return &rateCounter{ time: clock.New(), } }