From fe97baec8d28ad29c9c3ecdb6ee2c77d0aa52fe9 Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Wed, 17 Apr 2013 21:26:25 +0000 Subject: [PATCH] fix setting memcache counters for the first time --- dos.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dos.go b/dos.go index ae9e707..fb70ea6 100644 --- a/dos.go +++ b/dos.go @@ -13,6 +13,7 @@ var ( dosKeyNamespace = "dos" memcacheAddr = flag.String("memcache", "127.0.0.1:11211", "Memcache address") + whitelistedAddrs = flag.String("whitelist-addrs", "127.0.0.1", "Whitelisted addresses (no rate limiting), comma separated") ) type Limiter struct { @@ -31,7 +32,7 @@ func NewLimiter(param string, count int, period int, extractfn func(*http.Reques Count: count, Period: period, extractValue: extractfn, - whitelistedValues: make([]string, 0), + whitelistedValues: strings.Split(*whitelistedAddrs, ","), mc: memcache.New(*memcacheAddr), } } @@ -58,7 +59,7 @@ func (l *Limiter) Limit(handler http.Handler) http.Handler { result, err := l.mc.Increment(key, 1) if err != nil { result = 1 - if err = l.mc.Add(&memcache.Item{Key: key, Value: []byte{0, 0, 0, 0}, Expiration: int32(l.Period)}); err != nil { + if err = l.mc.Add(&memcache.Item{Key: key, Value: []byte("1"), Expiration: int32(l.Period)}); err != nil { // Possible race for who adds to the cache first. if result, err = l.mc.Increment(key, 1); err != nil { log.Printf("Memcache failed for '%s': %s", key, err) -- GitLab