summaryrefslogtreecommitdiff
path: root/vendor/github.com/hpcloud/tail/ratelimiter
diff options
context:
space:
mode:
authorValentin Rothberg <rothberg@redhat.com>2019-02-05 11:51:41 +0100
committerValentin Rothberg <rothberg@redhat.com>2019-02-06 11:14:06 +0100
commit9ac0ebb0791851aea81ecc847802db5a39bfb6e7 (patch)
tree30ad98bcc2c2dd1136f46a48cbc44d422adfa184 /vendor/github.com/hpcloud/tail/ratelimiter
parent51714d5da7aaa19014fd67b48b79dfbd5f69c1f0 (diff)
downloadpodman-9ac0ebb0791851aea81ecc847802db5a39bfb6e7.tar.gz
podman-9ac0ebb0791851aea81ecc847802db5a39bfb6e7.tar.bz2
podman-9ac0ebb0791851aea81ecc847802db5a39bfb6e7.zip
Cirrus: add vendor_check_task
* Make sure that all vendored dependencies are in sync with the code and the vendor.conf by running `make vendor` with a follow-up status check of the git tree. * Vendor ginkgo and gomega to include the test dependencies. Signed-off-by: Chris Evic <cevich@redhat.com> Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
Diffstat (limited to 'vendor/github.com/hpcloud/tail/ratelimiter')
-rw-r--r--vendor/github.com/hpcloud/tail/ratelimiter/leakybucket.go97
-rw-r--r--vendor/github.com/hpcloud/tail/ratelimiter/memory.go58
-rw-r--r--vendor/github.com/hpcloud/tail/ratelimiter/storage.go6
3 files changed, 161 insertions, 0 deletions
diff --git a/vendor/github.com/hpcloud/tail/ratelimiter/leakybucket.go b/vendor/github.com/hpcloud/tail/ratelimiter/leakybucket.go
new file mode 100644
index 000000000..358b69e7f
--- /dev/null
+++ b/vendor/github.com/hpcloud/tail/ratelimiter/leakybucket.go
@@ -0,0 +1,97 @@
+// Package ratelimiter implements the Leaky Bucket ratelimiting algorithm with memcached and in-memory backends.
+package ratelimiter
+
+import (
+ "time"
+)
+
+type LeakyBucket struct {
+ Size uint16
+ Fill float64
+ LeakInterval time.Duration // time.Duration for 1 unit of size to leak
+ Lastupdate time.Time
+ Now func() time.Time
+}
+
+func NewLeakyBucket(size uint16, leakInterval time.Duration) *LeakyBucket {
+ bucket := LeakyBucket{
+ Size: size,
+ Fill: 0,
+ LeakInterval: leakInterval,
+ Now: time.Now,
+ Lastupdate: time.Now(),
+ }
+
+ return &bucket
+}
+
+func (b *LeakyBucket) updateFill() {
+ now := b.Now()
+ if b.Fill > 0 {
+ elapsed := now.Sub(b.Lastupdate)
+
+ b.Fill -= float64(elapsed) / float64(b.LeakInterval)
+ if b.Fill < 0 {
+ b.Fill = 0
+ }
+ }
+ b.Lastupdate = now
+}
+
+func (b *LeakyBucket) Pour(amount uint16) bool {
+ b.updateFill()
+
+ var newfill float64 = b.Fill + float64(amount)
+
+ if newfill > float64(b.Size) {
+ return false
+ }
+
+ b.Fill = newfill
+
+ return true
+}
+
+// The time at which this bucket will be completely drained
+func (b *LeakyBucket) DrainedAt() time.Time {
+ return b.Lastupdate.Add(time.Duration(b.Fill * float64(b.LeakInterval)))
+}
+
+// The duration until this bucket is completely drained
+func (b *LeakyBucket) TimeToDrain() time.Duration {
+ return b.DrainedAt().Sub(b.Now())
+}
+
+func (b *LeakyBucket) TimeSinceLastUpdate() time.Duration {
+ return b.Now().Sub(b.Lastupdate)
+}
+
+type LeakyBucketSer struct {
+ Size uint16
+ Fill float64
+ LeakInterval time.Duration // time.Duration for 1 unit of size to leak
+ Lastupdate time.Time
+}
+
+func (b *LeakyBucket) Serialise() *LeakyBucketSer {
+ bucket := LeakyBucketSer{
+ Size: b.Size,
+ Fill: b.Fill,
+ LeakInterval: b.LeakInterval,
+ Lastupdate: b.Lastupdate,
+ }
+
+ return &bucket
+}
+
+func (b *LeakyBucketSer) DeSerialise() *LeakyBucket {
+ bucket := LeakyBucket{
+ Size: b.Size,
+ Fill: b.Fill,
+ LeakInterval: b.LeakInterval,
+ Lastupdate: b.Lastupdate,
+ Now: time.Now,
+ }
+
+ return &bucket
+}
diff --git a/vendor/github.com/hpcloud/tail/ratelimiter/memory.go b/vendor/github.com/hpcloud/tail/ratelimiter/memory.go
new file mode 100644
index 000000000..8f6a5784a
--- /dev/null
+++ b/vendor/github.com/hpcloud/tail/ratelimiter/memory.go
@@ -0,0 +1,58 @@
+package ratelimiter
+
+import (
+ "errors"
+ "time"
+)
+
+const GC_SIZE int = 100
+
+type Memory struct {
+ store map[string]LeakyBucket
+ lastGCCollected time.Time
+}
+
+func NewMemory() *Memory {
+ m := new(Memory)
+ m.store = make(map[string]LeakyBucket)
+ m.lastGCCollected = time.Now()
+ return m
+}
+
+func (m *Memory) GetBucketFor(key string) (*LeakyBucket, error) {
+
+ bucket, ok := m.store[key]
+ if !ok {
+ return nil, errors.New("miss")
+ }
+
+ return &bucket, nil
+}
+
+func (m *Memory) SetBucketFor(key string, bucket LeakyBucket) error {
+
+ if len(m.store) > GC_SIZE {
+ m.GarbageCollect()
+ }
+
+ m.store[key] = bucket
+
+ return nil
+}
+
+func (m *Memory) GarbageCollect() {
+ now := time.Now()
+
+ // rate limit GC to once per minute
+ if now.Add(60*time.Second).Unix() > m.lastGCCollected.Unix() {
+
+ for key, bucket := range m.store {
+ // if the bucket is drained, then GC
+ if bucket.DrainedAt().Unix() > now.Unix() {
+ delete(m.store, key)
+ }
+ }
+
+ m.lastGCCollected = now
+ }
+}
diff --git a/vendor/github.com/hpcloud/tail/ratelimiter/storage.go b/vendor/github.com/hpcloud/tail/ratelimiter/storage.go
new file mode 100644
index 000000000..89b2fe882
--- /dev/null
+++ b/vendor/github.com/hpcloud/tail/ratelimiter/storage.go
@@ -0,0 +1,6 @@
+package ratelimiter
+
+type Storage interface {
+ GetBucketFor(string) (*LeakyBucket, error)
+ SetBucketFor(string, LeakyBucket) error
+}