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 /Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/mem.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 'Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/mem.go')
-rw-r--r-- | Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/mem.go | 90 |
1 files changed, 0 insertions, 90 deletions
diff --git a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/mem.go b/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/mem.go deleted file mode 100644 index 22d200e8d..000000000 --- a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/store/mem.go +++ /dev/null @@ -1,90 +0,0 @@ -package store - -import ( - "sync" - "time" - - "github.com/golang/groupcache/lru" - "gopkg.in/throttled/throttled.v1" -) - -// memStore implements an in-memory Store. -type memStore struct { - sync.Mutex - keys *lru.Cache - m map[string]*counter -} - -// NewMemStore creates a new MemStore. If maxKeys > 0, the number of different keys -// is restricted to the specified amount. In this case, it uses an LRU algorithm to -// evict older keys to make room for newer ones. If a request is made for a key that -// has been evicted, it will be processed as if its count was 0, possibly allowing requests -// that should be denied. -// -// If maxKeys <= 0, there is no limit on the number of keys, which may use an unbounded amount of -// memory depending on the server's load. -// -// The MemStore is only for single-process rate-limiting. To share the rate limit state -// among multiple instances of the web server, use a database- or key-value-based -// store. -// -func NewMemStore(maxKeys int) throttled.Store { - var m *memStore - if maxKeys > 0 { - m = &memStore{ - keys: lru.New(maxKeys), - } - } else { - m = &memStore{ - m: make(map[string]*counter), - } - } - return m -} - -// A counter represents a single entry in the MemStore. -type counter struct { - n int - ts time.Time -} - -// Incr increments the counter for the specified key. It returns the new -// count value and the remaining number of seconds, or an error. -func (ms *memStore) Incr(key string, window time.Duration) (int, int, error) { - ms.Lock() - defer ms.Unlock() - var c *counter - if ms.keys != nil { - v, _ := ms.keys.Get(key) - if v != nil { - c = v.(*counter) - } - } else { - c = ms.m[key] - } - if c == nil { - c = &counter{0, time.Now().UTC()} - } - c.n++ - if ms.keys != nil { - ms.keys.Add(key, c) - } else { - ms.m[key] = c - } - return c.n, throttled.RemainingSeconds(c.ts, window), nil -} - -// Reset resets the counter for the specified key. It sets the count -// to 1 and initializes the timestamp with the current time, in UTC. -// It returns an error if the operation fails. -func (ms *memStore) Reset(key string, win time.Duration) error { - ms.Lock() - defer ms.Unlock() - c := &counter{1, time.Now().UTC()} - if ms.keys != nil { - ms.keys.Add(key, c) - } else { - ms.m[key] = c - } - return nil -} |