diff options
author | Christopher Speller <crspeller@gmail.com> | 2016-05-12 15:08:58 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2016-05-12 16:37:29 -0400 |
commit | 84d2482ddbff9564c9ad75b2d30af66e3ddfd44d (patch) | |
tree | 8bfa567d2b6381f4a996ada2deff8a16aa85a3ac /vendor/gopkg.in/throttled/throttled.v1/delayer.go | |
parent | d1efb66ad7b017f0fbfe6f0c20843b30f396e504 (diff) | |
download | chat-84d2482ddbff9564c9ad75b2d30af66e3ddfd44d.tar.gz chat-84d2482ddbff9564c9ad75b2d30af66e3ddfd44d.tar.bz2 chat-84d2482ddbff9564c9ad75b2d30af66e3ddfd44d.zip |
Updating go depencancies. Switching to go1.6 vendoring (#2949)
Diffstat (limited to 'vendor/gopkg.in/throttled/throttled.v1/delayer.go')
-rw-r--r-- | vendor/gopkg.in/throttled/throttled.v1/delayer.go | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/vendor/gopkg.in/throttled/throttled.v1/delayer.go b/vendor/gopkg.in/throttled/throttled.v1/delayer.go new file mode 100644 index 000000000..e62ec9e86 --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/delayer.go @@ -0,0 +1,109 @@ +package throttled + +import "time" + +// The Quota interface defines the method to implement to describe +// a time-window quota, as required by the RateLimit throttler. +type Quota interface { + // Quota returns a number of requests allowed, and a duration. + Quota() (int, time.Duration) +} + +// The Delayer interface defines the method to implement to describe +// a delay as required by the Interval throttler. +type Delayer interface { + // Delay returns a duration. + Delay() time.Duration +} + +// PerSec represents a number of requests per second. +type PerSec int + +// Delay returns the duration to wait before the next request can go through, +// so that PerSec(n) == n requests per second at regular intervals. +func (ps PerSec) Delay() time.Duration { + if ps <= 0 { + return 0 + } + return time.Duration(1.0 / float64(ps) * float64(time.Second)) +} + +// Quota returns the number of requests allowed in a 1 second time window, +// so that PerSec(n) == n requests allowed per second. +func (ps PerSec) Quota() (int, time.Duration) { + return int(ps), time.Second +} + +// PerMin represents a number of requests per minute. +type PerMin int + +// Delay returns the duration to wait before the next request can go through, +// so that PerMin(n) == n requests per minute at regular intervals. +func (pm PerMin) Delay() time.Duration { + if pm <= 0 { + return 0 + } + return time.Duration(1.0 / float64(pm) * float64(time.Minute)) +} + +// Quota returns the number of requests allowed in a 1 minute time window, +// so that PerMin(n) == n requests allowed per minute. +func (pm PerMin) Quota() (int, time.Duration) { + return int(pm), time.Minute +} + +// PerHour represents a number of requests per hour. +type PerHour int + +// Delay returns the duration to wait before the next request can go through, +// so that PerHour(n) == n requests per hour at regular intervals. +func (ph PerHour) Delay() time.Duration { + if ph <= 0 { + return 0 + } + return time.Duration(1.0 / float64(ph) * float64(time.Hour)) +} + +// Quota returns the number of requests allowed in a 1 hour time window, +// so that PerHour(n) == n requests allowed per hour. +func (ph PerHour) Quota() (int, time.Duration) { + return int(ph), time.Hour +} + +// PerDay represents a number of requests per day. +type PerDay int + +// Delay returns the duration to wait before the next request can go through, +// so that PerDay(n) == n requests per day at regular intervals. +func (pd PerDay) Delay() time.Duration { + if pd <= 0 { + return 0 + } + return time.Duration(1.0 / float64(pd) * float64(24*time.Hour)) +} + +// Quota returns the number of requests allowed in a 1 day time window, +// so that PerDay(n) == n requests allowed per day. +func (pd PerDay) Quota() (int, time.Duration) { + return int(pd), 24 * time.Hour +} + +// D represents a custom delay. +type D time.Duration + +// Delay returns the duration to wait before the next request can go through, +// which is the custom duration represented by the D value. +func (d D) Delay() time.Duration { + return time.Duration(d) +} + +// Q represents a custom quota. +type Q struct { + Requests int + Window time.Duration +} + +// Quota returns the number of requests allowed and the custom time window. +func (q Q) Quota() (int, time.Duration) { + return q.Requests, q.Window +} |