summaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/throttled/throttled.v2/store
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2018-04-16 05:37:14 -0700
committerJoram Wilander <jwawilander@gmail.com>2018-04-16 08:37:14 -0400
commit6e2cb00008cbf09e556b00f87603797fcaa47e09 (patch)
tree3c0eb55ff4226a3f024aad373140d1fb860a6404 /vendor/gopkg.in/throttled/throttled.v2/store
parentbf24f51c4e1cc6286885460672f7f449e8c6f5ef (diff)
downloadchat-6e2cb00008cbf09e556b00f87603797fcaa47e09.tar.gz
chat-6e2cb00008cbf09e556b00f87603797fcaa47e09.tar.bz2
chat-6e2cb00008cbf09e556b00f87603797fcaa47e09.zip
Depenancy upgrades and movign to dep. (#8630)
Diffstat (limited to 'vendor/gopkg.in/throttled/throttled.v2/store')
-rw-r--r--vendor/gopkg.in/throttled/throttled.v2/store/deprecated.go32
-rw-r--r--vendor/gopkg.in/throttled/throttled.v2/store/memstore/memstore.go2
-rw-r--r--vendor/gopkg.in/throttled/throttled.v2/store/memstore/memstore_test.go40
-rw-r--r--vendor/gopkg.in/throttled/throttled.v2/store/redigostore/redigostore.go168
-rw-r--r--vendor/gopkg.in/throttled/throttled.v2/store/redigostore/redigostore_test.go85
-rw-r--r--vendor/gopkg.in/throttled/throttled.v2/store/storetest/doc.go2
-rw-r--r--vendor/gopkg.in/throttled/throttled.v2/store/storetest/storetest.go176
7 files changed, 1 insertions, 504 deletions
diff --git a/vendor/gopkg.in/throttled/throttled.v2/store/deprecated.go b/vendor/gopkg.in/throttled/throttled.v2/store/deprecated.go
deleted file mode 100644
index 0492ba89e..000000000
--- a/vendor/gopkg.in/throttled/throttled.v2/store/deprecated.go
+++ /dev/null
@@ -1,32 +0,0 @@
-// Package store contains deprecated aliases for subpackages
-package store // import "github.com/throttled/throttled/store"
-
-import (
- "github.com/garyburd/redigo/redis"
-
- "github.com/throttled/throttled/store/memstore"
- "github.com/throttled/throttled/store/redigostore"
-)
-
-// DEPRECATED. NewMemStore is a compatible alias for mem.New
-func NewMemStore(maxKeys int) *memstore.MemStore {
- st, err := memstore.New(maxKeys)
- if err != nil {
- // As of this writing, `lru.New` can only return an error if you pass
- // maxKeys <= 0 so this should never occur.
- panic(err)
- }
- return st
-}
-
-// DEPRECATED. NewMemStore is a compatible alias for redis.New
-func NewRedisStore(pool *redis.Pool, keyPrefix string, db int) *redigostore.RedigoStore {
- st, err := redigostore.New(pool, keyPrefix, db)
- if err != nil {
- // As of this writing, creating a Redis store never returns an error
- // so this should be safe while providing some ability to return errors
- // in the future.
- panic(err)
- }
- return st
-}
diff --git a/vendor/gopkg.in/throttled/throttled.v2/store/memstore/memstore.go b/vendor/gopkg.in/throttled/throttled.v2/store/memstore/memstore.go
index 352232958..5d8fee8b5 100644
--- a/vendor/gopkg.in/throttled/throttled.v2/store/memstore/memstore.go
+++ b/vendor/gopkg.in/throttled/throttled.v2/store/memstore/memstore.go
@@ -1,5 +1,5 @@
// Package memstore offers an in-memory store implementation for throttled.
-package memstore // import "github.com/throttled/throttled/store/memstore"
+package memstore // import "gopkg.in/throttled/throttled.v2/store/memstore"
import (
"sync"
diff --git a/vendor/gopkg.in/throttled/throttled.v2/store/memstore/memstore_test.go b/vendor/gopkg.in/throttled/throttled.v2/store/memstore/memstore_test.go
deleted file mode 100644
index 27b1c9b95..000000000
--- a/vendor/gopkg.in/throttled/throttled.v2/store/memstore/memstore_test.go
+++ /dev/null
@@ -1,40 +0,0 @@
-package memstore_test
-
-import (
- "testing"
-
- "github.com/throttled/throttled/store/memstore"
- "github.com/throttled/throttled/store/storetest"
-)
-
-func TestMemStoreLRU(t *testing.T) {
- st, err := memstore.New(10)
- if err != nil {
- t.Fatal(err)
- }
- storetest.TestGCRAStore(t, st)
-}
-
-func TestMemStoreUnlimited(t *testing.T) {
- st, err := memstore.New(10)
- if err != nil {
- t.Fatal(err)
- }
- storetest.TestGCRAStore(t, st)
-}
-
-func BenchmarkMemStoreLRU(b *testing.B) {
- st, err := memstore.New(10)
- if err != nil {
- b.Fatal(err)
- }
- storetest.BenchmarkGCRAStore(b, st)
-}
-
-func BenchmarkMemStoreUnlimited(b *testing.B) {
- st, err := memstore.New(0)
- if err != nil {
- b.Fatal(err)
- }
- storetest.BenchmarkGCRAStore(b, st)
-}
diff --git a/vendor/gopkg.in/throttled/throttled.v2/store/redigostore/redigostore.go b/vendor/gopkg.in/throttled/throttled.v2/store/redigostore/redigostore.go
deleted file mode 100644
index 03acbcab0..000000000
--- a/vendor/gopkg.in/throttled/throttled.v2/store/redigostore/redigostore.go
+++ /dev/null
@@ -1,168 +0,0 @@
-// Package redigostore offers Redis-based store implementation for throttled using redigo.
-package redigostore // import "github.com/throttled/throttled/store/redigostore"
-
-import (
- "strings"
- "time"
-
- "github.com/garyburd/redigo/redis"
-)
-
-const (
- redisCASMissingKey = "key does not exist"
- redisCASScript = `
-local v = redis.call('get', KEYS[1])
-if v == false then
- return redis.error_reply("key does not exist")
-end
-if v ~= ARGV[1] then
- return 0
-end
-redis.call('setex', KEYS[1], ARGV[3], ARGV[2])
-return 1
-`
-)
-
-// RedigoStore implements a Redis-based store using redigo.
-type RedigoStore struct {
- pool *redis.Pool
- prefix string
- db int
-}
-
-// New creates a new Redis-based store, using the provided pool to get
-// its connections. The keys will have the specified keyPrefix, which
-// may be an empty string, and the database index specified by db will
-// be selected to store the keys. Any updating operations will reset
-// the key TTL to the provided value rounded down to the nearest
-// second. Depends on Redis 2.6+ for EVAL support.
-func New(pool *redis.Pool, keyPrefix string, db int) (*RedigoStore, error) {
- return &RedigoStore{
- pool: pool,
- prefix: keyPrefix,
- db: db,
- }, nil
-}
-
-// GetWithTime returns the value of the key if it is in the store
-// or -1 if it does not exist. It also returns the current time at
-// the redis server to microsecond precision.
-func (r *RedigoStore) GetWithTime(key string) (int64, time.Time, error) {
- var now time.Time
-
- key = r.prefix + key
-
- conn, err := r.getConn()
- if err != nil {
- return 0, now, err
- }
- defer conn.Close()
-
- conn.Send("TIME")
- conn.Send("GET", key)
- conn.Flush()
- timeReply, err := redis.Values(conn.Receive())
- if err != nil {
- return 0, now, err
- }
-
- var s, us int64
- if _, err := redis.Scan(timeReply, &s, &us); err != nil {
- return 0, now, err
- }
- now = time.Unix(s, us*int64(time.Microsecond))
-
- v, err := redis.Int64(conn.Receive())
- if err == redis.ErrNil {
- return -1, now, nil
- } else if err != nil {
- return 0, now, err
- }
-
- return v, now, nil
-}
-
-// SetIfNotExistsWithTTL sets the value of key only if it is not
-// already set in the store it returns whether a new value was set.
-// If a new value was set, the ttl in the key is also set, though this
-// operation is not performed atomically.
-func (r *RedigoStore) SetIfNotExistsWithTTL(key string, value int64, ttl time.Duration) (bool, error) {
- key = r.prefix + key
-
- conn, err := r.getConn()
- if err != nil {
- return false, err
- }
- defer conn.Close()
-
- v, err := redis.Int64(conn.Do("SETNX", key, value))
- if err != nil {
- return false, err
- }
-
- updated := v == 1
-
- ttlSeconds := int(ttl.Seconds())
-
- // An `EXPIRE 0` will delete the key immediately, so make sure that we set
- // expiry for a minimum of one second out so that our results stay in the
- // store.
- if ttlSeconds < 1 {
- ttlSeconds = 1
- }
-
- if _, err := conn.Do("EXPIRE", key, ttlSeconds); err != nil {
- return updated, err
- }
-
- return updated, nil
-}
-
-// CompareAndSwapWithTTL atomically compares the value at key to the
-// old value. If it matches, it sets it to the new value and returns
-// true. Otherwise, it returns false. If the key does not exist in the
-// store, it returns false with no error. If the swap succeeds, the
-// ttl for the key is updated atomically.
-func (r *RedigoStore) CompareAndSwapWithTTL(key string, old, new int64, ttl time.Duration) (bool, error) {
- key = r.prefix + key
- conn, err := r.getConn()
- if err != nil {
- return false, err
- }
- defer conn.Close()
-
- ttlSeconds := int(ttl.Seconds())
-
- // An `EXPIRE 0` will delete the key immediately, so make sure that we set
- // expiry for a minimum of one second out so that our results stay in the
- // store.
- if ttlSeconds < 1 {
- ttlSeconds = 1
- }
-
- swapped, err := redis.Bool(conn.Do("EVAL", redisCASScript, 1, key, old, new, ttlSeconds))
- if err != nil {
- if strings.Contains(err.Error(), redisCASMissingKey) {
- return false, nil
- }
-
- return false, err
- }
-
- return swapped, nil
-}
-
-// Select the specified database index.
-func (r *RedigoStore) getConn() (redis.Conn, error) {
- conn := r.pool.Get()
-
- // Select the specified database
- if r.db > 0 {
- if _, err := redis.String(conn.Do("SELECT", r.db)); err != nil {
- conn.Close()
- return nil, err
- }
- }
-
- return conn, nil
-}
diff --git a/vendor/gopkg.in/throttled/throttled.v2/store/redigostore/redigostore_test.go b/vendor/gopkg.in/throttled/throttled.v2/store/redigostore/redigostore_test.go
deleted file mode 100644
index ee9e2904d..000000000
--- a/vendor/gopkg.in/throttled/throttled.v2/store/redigostore/redigostore_test.go
+++ /dev/null
@@ -1,85 +0,0 @@
-package redigostore_test
-
-import (
- "testing"
- "time"
-
- "github.com/garyburd/redigo/redis"
-
- "github.com/throttled/throttled/store/redigostore"
- "github.com/throttled/throttled/store/storetest"
-)
-
-const (
- redisTestDB = 1
- redisTestPrefix = "throttled:"
-)
-
-func getPool() *redis.Pool {
- pool := &redis.Pool{
- MaxIdle: 3,
- IdleTimeout: 30 * time.Second,
- Dial: func() (redis.Conn, error) {
- return redis.Dial("tcp", ":6379")
- },
- TestOnBorrow: func(c redis.Conn, t time.Time) error {
- _, err := c.Do("PING")
- return err
- },
- }
- return pool
-}
-
-func TestRedisStore(t *testing.T) {
- c, st := setupRedis(t, 0)
- defer c.Close()
- defer clearRedis(c)
-
- clearRedis(c)
- storetest.TestGCRAStore(t, st)
- storetest.TestGCRAStoreTTL(t, st)
-}
-
-func BenchmarkRedisStore(b *testing.B) {
- c, st := setupRedis(b, 0)
- defer c.Close()
- defer clearRedis(c)
-
- storetest.BenchmarkGCRAStore(b, st)
-}
-
-func clearRedis(c redis.Conn) error {
- keys, err := redis.Values(c.Do("KEYS", redisTestPrefix+"*"))
- if err != nil {
- return err
- }
-
- if _, err := redis.Int(c.Do("DEL", keys...)); err != nil {
- return err
- }
-
- return nil
-}
-
-func setupRedis(tb testing.TB, ttl time.Duration) (redis.Conn, *redigostore.RedigoStore) {
- pool := getPool()
- c := pool.Get()
-
- if _, err := redis.String(c.Do("PING")); err != nil {
- c.Close()
- tb.Skip("redis server not available on localhost port 6379")
- }
-
- if _, err := redis.String(c.Do("SELECT", redisTestDB)); err != nil {
- c.Close()
- tb.Fatal(err)
- }
-
- st, err := redigostore.New(pool, redisTestPrefix, redisTestDB)
- if err != nil {
- c.Close()
- tb.Fatal(err)
- }
-
- return c, st
-}
diff --git a/vendor/gopkg.in/throttled/throttled.v2/store/storetest/doc.go b/vendor/gopkg.in/throttled/throttled.v2/store/storetest/doc.go
deleted file mode 100644
index 405c59a12..000000000
--- a/vendor/gopkg.in/throttled/throttled.v2/store/storetest/doc.go
+++ /dev/null
@@ -1,2 +0,0 @@
-// Package storetest provides a helper for testing throttled stores.
-package storetest // import "github.com/throttled/throttled/store/storetest"
diff --git a/vendor/gopkg.in/throttled/throttled.v2/store/storetest/storetest.go b/vendor/gopkg.in/throttled/throttled.v2/store/storetest/storetest.go
deleted file mode 100644
index 2233ebdfb..000000000
--- a/vendor/gopkg.in/throttled/throttled.v2/store/storetest/storetest.go
+++ /dev/null
@@ -1,176 +0,0 @@
-// Package storetest provides a helper for testing throttled stores.
-package storetest // import "github.com/throttled/throttled/store/storetest"
-
-import (
- "math/rand"
- "strconv"
- "sync/atomic"
- "testing"
- "time"
-
- "github.com/throttled/throttled"
-)
-
-// TestGCRAStore tests the behavior of a GCRAStore implementation for
-// compliance with the throttled API. It does not require support
-// for TTLs.
-func TestGCRAStore(t *testing.T, st throttled.GCRAStore) {
- // GetWithTime a missing key
- if have, _, err := st.GetWithTime("foo"); err != nil {
- t.Fatal(err)
- } else if have != -1 {
- t.Errorf("expected GetWithTime to return -1 for a missing key but got %d", have)
- }
-
- // SetIfNotExists on a new key
- want := int64(1)
-
- if set, err := st.SetIfNotExistsWithTTL("foo", want, 0); err != nil {
- t.Fatal(err)
- } else if !set {
- t.Errorf("expected SetIfNotExists on an empty key to succeed")
- }
-
- before := time.Now()
-
- if have, now, err := st.GetWithTime("foo"); err != nil {
- t.Fatal(err)
- } else if have != want {
- t.Errorf("expected GetWithTime to return %d but got %d", want, have)
- } else if now.UnixNano() <= 0 {
- t.Errorf("expected GetWithTime to return a time representable representable as a positive int64 of nanoseconds since the epoch")
- } else if now.Before(before) || now.After(time.Now()) {
- // Note that we make the assumption here that the store is running on
- // the same machine as this test and thus shares a clock. This can be a
- // little tricky in the case of Redis, which could be running
- // elsewhere. The test assumes that it's running either locally on on
- // Travis (where currently the Redis is available on localhost). If new
- // test environments are procured, this may need to be revisited.
- t.Errorf("expected GetWithTime to return a time between the time before the call and the time after the call")
- }
-
- // SetIfNotExists on an existing key
- if set, err := st.SetIfNotExistsWithTTL("foo", 123, 0); err != nil {
- t.Fatal(err)
- } else if set {
- t.Errorf("expected SetIfNotExists on an existing key to fail")
- }
-
- if have, _, err := st.GetWithTime("foo"); err != nil {
- t.Fatal(err)
- } else if have != want {
- t.Errorf("expected GetWithTime to return %d but got %d", want, have)
- }
-
- // SetIfNotExists on a different key
- if set, err := st.SetIfNotExistsWithTTL("bar", 456, 0); err != nil {
- t.Fatal(err)
- } else if !set {
- t.Errorf("expected SetIfNotExists on an empty key to succeed")
- }
-
- // Returns the false on a missing key
- if swapped, err := st.CompareAndSwapWithTTL("baz", 1, 2, 0); err != nil {
- t.Fatal(err)
- } else if swapped {
- t.Errorf("expected CompareAndSwap to fail on a missing key")
- }
-
- // Test a successful CAS
- want = int64(2)
-
- if swapped, err := st.CompareAndSwapWithTTL("foo", 1, want, 0); err != nil {
- t.Fatal(err)
- } else if !swapped {
- t.Errorf("expected CompareAndSwap to succeed")
- }
-
- if have, _, err := st.GetWithTime("foo"); err != nil {
- t.Fatal(err)
- } else if have != want {
- t.Errorf("expected GetWithTime to return %d but got %d", want, have)
- }
-
- // Test an unsuccessful CAS
- if swapped, err := st.CompareAndSwapWithTTL("foo", 1, 2, 0); err != nil {
- t.Fatal(err)
- } else if swapped {
- t.Errorf("expected CompareAndSwap to fail")
- }
-
- if have, _, err := st.GetWithTime("foo"); err != nil {
- t.Fatal(err)
- } else if have != want {
- t.Errorf("expected GetWithTime to return %d but got %d", want, have)
- }
-}
-
-// TestGCRAStoreTTL tests the behavior of TTLs in a GCRAStore implementation.
-func TestGCRAStoreTTL(t *testing.T, st throttled.GCRAStore) {
- ttl := time.Second
- want := int64(1)
- key := "ttl"
-
- if _, err := st.SetIfNotExistsWithTTL(key, want, ttl); err != nil {
- t.Fatal(err)
- }
-
- if have, _, err := st.GetWithTime(key); err != nil {
- t.Fatal(err)
- } else if have != want {
- t.Errorf("expected GetWithTime to return %d, got %d", want, have)
- }
-
- // I can't think of a generic way to test expiration without a sleep
- time.Sleep(ttl + time.Millisecond)
-
- if have, _, err := st.GetWithTime(key); err != nil {
- t.Fatal(err)
- } else if have != -1 {
- t.Errorf("expected GetWithTime to fail on an expired key but got %d", have)
- }
-}
-
-// BenchmarkGCRAStore runs parallel benchmarks against a GCRAStore implementation.
-// Aside from being useful for performance testing, this is useful for finding
-// race conditions with the Go race detector.
-func BenchmarkGCRAStore(b *testing.B, st throttled.GCRAStore) {
- seed := int64(42)
- var attempts, updates int64
-
- b.RunParallel(func(pb *testing.PB) {
- // We need atomic behavior around the RNG or go detects a race in the test
- delta := int64(1)
- seedValue := atomic.AddInt64(&seed, delta) - delta
- gen := rand.New(rand.NewSource(seedValue))
-
- for pb.Next() {
- key := strconv.FormatInt(gen.Int63n(50), 10)
-
- var v int64
- var updated bool
-
- v, _, err := st.GetWithTime(key)
- if v == -1 {
- updated, err = st.SetIfNotExistsWithTTL(key, gen.Int63(), 0)
- if err != nil {
- b.Error(err)
- }
- } else if err != nil {
- b.Error(err)
- } else {
- updated, err = st.CompareAndSwapWithTTL(key, v, gen.Int63(), 0)
- if err != nil {
- b.Error(err)
- }
- }
-
- atomic.AddInt64(&attempts, 1)
- if updated {
- atomic.AddInt64(&updates, 1)
- }
- }
- })
-
- b.Logf("%d/%d update operations succeeed", updates, attempts)
-}