summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/shazow/rateio/limiter.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2017-12-03 01:24:05 +0100
committerWim <wim@42.be>2017-12-03 01:24:05 +0100
commited9118b34620f1ecd5f28506328d4ffe1b04793d (patch)
tree442998012f6779446a463e402fa7c0836edcac91 /vendor/github.com/shazow/rateio/limiter.go
parent59e55cfbd5cc3c82236c5e8b95e5baff256f7143 (diff)
downloadmatterbridge-msglm-ed9118b34620f1ecd5f28506328d4ffe1b04793d.tar.gz
matterbridge-msglm-ed9118b34620f1ecd5f28506328d4ffe1b04793d.tar.bz2
matterbridge-msglm-ed9118b34620f1ecd5f28506328d4ffe1b04793d.zip
Add sshchat dependencies in vendor
Diffstat (limited to 'vendor/github.com/shazow/rateio/limiter.go')
-rw-r--r--vendor/github.com/shazow/rateio/limiter.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/vendor/github.com/shazow/rateio/limiter.go b/vendor/github.com/shazow/rateio/limiter.go
new file mode 100644
index 00000000..db5ae76c
--- /dev/null
+++ b/vendor/github.com/shazow/rateio/limiter.go
@@ -0,0 +1,62 @@
+package rateio
+
+import (
+ "errors"
+ "time"
+)
+
+const minInt = -int(^uint(0)>>1) - 1
+
+// The error returned when the read rate exceeds our specification.
+var ErrRateExceeded = errors.New("Read rate exceeded.")
+
+// Limiter is an interface for a rate limiter.
+// There are a few example limiters included in the package, but feel free to go wild with your own.
+type Limiter interface {
+ // Apply this many bytes to the limiter, return ErrRateExceeded if the defined rate is exceeded.
+ Count(int) error
+}
+
+// simpleLimiter is a rate limiter that restricts Amount bytes in Frequency duration.
+type simpleLimiter struct {
+ Amount int
+ Frequency time.Duration
+
+ numRead int
+ timeRead time.Time
+}
+
+// NewSimpleLimiter creates a Limiter that restricts a given number of bytes per frequency.
+func NewSimpleLimiter(amount int, frequency time.Duration) Limiter {
+ return &simpleLimiter{
+ Amount: amount,
+ Frequency: frequency,
+ }
+}
+
+// NewGracefulLimiter returns a Limiter that is the same as a
+// SimpleLimiter but adds a grace period at the start of the rate
+// limiting where it allows unlimited bytes to be read during that
+// period.
+func NewGracefulLimiter(amount int, frequency time.Duration, grace time.Duration) Limiter {
+ return &simpleLimiter{
+ Amount: amount,
+ Frequency: frequency,
+ numRead: minInt,
+ timeRead: time.Now().Add(grace),
+ }
+}
+
+// Count applies n bytes to the limiter.
+func (limit *simpleLimiter) Count(n int) error {
+ now := time.Now()
+ if now.After(limit.timeRead) {
+ limit.numRead = 0
+ limit.timeRead = now.Add(limit.Frequency)
+ }
+ limit.numRead += n
+ if limit.numRead > limit.Amount {
+ return ErrRateExceeded
+ }
+ return nil
+}