diff options
author | Christopher Speller <crspeller@gmail.com> | 2016-05-12 23:56:07 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2016-05-12 23:56:07 -0400 |
commit | 38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8 (patch) | |
tree | a4fde09672192b97d453ad605b030bd5a10c5a45 /vendor/gopkg.in/throttled/throttled.v1/examples | |
parent | 84d2482ddbff9564c9ad75b2d30af66e3ddfd44d (diff) | |
download | chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.tar.gz chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.tar.bz2 chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.zip |
Moving to glide
Diffstat (limited to 'vendor/gopkg.in/throttled/throttled.v1/examples')
9 files changed, 526 insertions, 0 deletions
diff --git a/vendor/gopkg.in/throttled/throttled.v1/examples/README.md b/vendor/gopkg.in/throttled/throttled.v1/examples/README.md new file mode 100644 index 000000000..6b12dad20 --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/examples/README.md @@ -0,0 +1,12 @@ +# Examples + +This directory contains examples for all the throttlers implemented by the throttled package, as well as an example of a custom limiter. + +* custom/ : implements a custom limiter that allows requests to path /a on even seconds, and on path /b on odd seconds. +* interval-many/ : implements a common interval throttler to control two different handlers, one for path /a and another for path /b, so that requests to any one of the handlers go through at the specified interval. +* interval-vary/ : implements an interval throttler that varies by path, so that requests to each different path goes through at the specified interval. +* interval/ : implements an interval throttler so that any request goes through at the specified interval, regardless of path or any other criteria. +* memstats/ : implements a memory-usage throttler that limits access based on current memory statistics. +* rate-limit/ : implements a rate-limiter throttler that varies by path, so that the number of requests allowed are counted based on the requested path. + +Each example app supports a number of command-line flags. Run the example with the -h flag to display usage and defaults. diff --git a/vendor/gopkg.in/throttled/throttled.v1/examples/custom/main.go b/vendor/gopkg.in/throttled/throttled.v1/examples/custom/main.go new file mode 100644 index 000000000..b3fe993e8 --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/examples/custom/main.go @@ -0,0 +1,90 @@ +package main + +import ( + "flag" + "fmt" + "log" + "math/rand" + "net/http" + "sync" + "time" + + "gopkg.in/throttled/throttled.v1" +) + +var ( + delayRes = flag.Duration("delay-response", 0, "delay the response by a random duration between 0 and this value") + output = flag.String("output", "v", "type of output, one of `v`erbose, `q`uiet, `ok`-only, `ko`-only") +) + +// Custom limiter: allow requests to the /a path on even seconds only, and +// allow access to the /b path on odd seconds only. +// +// Yes this is absurd. A more realistic case could be to allow requests to some +// contest page only during a limited time window. +type customLimiter struct { +} + +func (c *customLimiter) Start() { + // No-op +} + +func (c *customLimiter) Limit(w http.ResponseWriter, r *http.Request) (<-chan bool, error) { + s := time.Now().Second() + ch := make(chan bool, 1) + ok := (r.URL.Path == "/a" && s%2 == 0) || (r.URL.Path == "/b" && s%2 != 0) + ch <- ok + if *output == "v" { + log.Printf("Custom Limiter: Path=%s, Second=%d; ok? %v", r.URL.Path, s, ok) + } + return ch, nil +} + +func main() { + flag.Parse() + + var h http.Handler + var ok, ko int + var mu sync.Mutex + + // Keep the start time to print since-time + start := time.Now() + // Create the custom throttler using our custom limiter + t := throttled.Custom(&customLimiter{}) + // Set its denied handler + t.DeniedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ko" { + log.Printf("KO: %s", time.Since(start)) + } + throttled.DefaultDeniedHandler.ServeHTTP(w, r) + mu.Lock() + defer mu.Unlock() + ko++ + }) + // Throttle the OK handler + rand.Seed(time.Now().Unix()) + h = t.Throttle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ok" { + log.Printf("ok: %s", time.Since(start)) + } + if *delayRes > 0 { + wait := time.Duration(rand.Intn(int(*delayRes))) + time.Sleep(wait) + } + w.WriteHeader(200) + mu.Lock() + defer mu.Unlock() + ok++ + })) + + // Print stats once in a while + go func() { + for _ = range time.Tick(10 * time.Second) { + mu.Lock() + log.Printf("ok: %d, ko: %d", ok, ko) + mu.Unlock() + } + }() + fmt.Println("server listening on port 9000") + http.ListenAndServe(":9000", h) +} diff --git a/vendor/gopkg.in/throttled/throttled.v1/examples/interval-many/main.go b/vendor/gopkg.in/throttled/throttled.v1/examples/interval-many/main.go new file mode 100644 index 000000000..51a4ca023 --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/examples/interval-many/main.go @@ -0,0 +1,79 @@ +package main + +import ( + "flag" + "fmt" + "log" + "math/rand" + "net/http" + "sync" + "time" + + "gopkg.in/throttled/throttled.v1" +) + +var ( + delay = flag.Duration("delay", 200*time.Millisecond, "delay between calls") + bursts = flag.Int("bursts", 10, "number of bursts allowed") + delayRes = flag.Duration("delay-response", 0, "delay the response by a random duration between 0 and this value") + output = flag.String("output", "v", "type of output, one of `v`erbose, `q`uiet, `ok`-only, `ko`-only") +) + +func main() { + flag.Parse() + + var ok, ko int + var mu sync.Mutex + + // Keep start time to log since-time + start := time.Now() + + // Create the interval throttle + t := throttled.Interval(throttled.D(*delay), *bursts, nil, 0) + // Set its denied handler + t.DeniedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ko" { + log.Printf("%s: KO: %s", r.URL.Path, time.Since(start)) + } + throttled.DefaultDeniedHandler.ServeHTTP(w, r) + mu.Lock() + defer mu.Unlock() + ko++ + }) + // Create OK handlers + rand.Seed(time.Now().Unix()) + makeHandler := func(ix int) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ok" { + log.Printf("handler %d: %s: ok: %s", ix, r.URL.Path, time.Since(start)) + } + if *delayRes > 0 { + wait := time.Duration(rand.Intn(int(*delayRes))) + time.Sleep(wait) + } + w.WriteHeader(200) + mu.Lock() + defer mu.Unlock() + ok++ + }) + } + // Throttle them using the same interval throttler + h1 := t.Throttle(makeHandler(1)) + h2 := t.Throttle(makeHandler(2)) + + // Handle two paths + mux := http.NewServeMux() + mux.Handle("/a", h1) + mux.Handle("/b", h2) + + // Print stats once in a while + go func() { + for _ = range time.Tick(10 * time.Second) { + mu.Lock() + log.Printf("ok: %d, ko: %d", ok, ko) + mu.Unlock() + } + }() + fmt.Println("server listening on port 9000") + http.ListenAndServe(":9000", mux) +} diff --git a/vendor/gopkg.in/throttled/throttled.v1/examples/interval-vary/main.go b/vendor/gopkg.in/throttled/throttled.v1/examples/interval-vary/main.go new file mode 100644 index 000000000..f43cdc122 --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/examples/interval-vary/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "flag" + "fmt" + "log" + "math/rand" + "net/http" + "sync" + "time" + + "gopkg.in/throttled/throttled.v1" +) + +var ( + delay = flag.Duration("delay", 200*time.Millisecond, "delay between calls") + bursts = flag.Int("bursts", 10, "number of bursts allowed") + maxkeys = flag.Int("max-keys", 1000, "maximum number of keys") + delayRes = flag.Duration("delay-response", 0, "delay the response by a random duration between 0 and this value") + output = flag.String("output", "v", "type of output, one of `v`erbose, `q`uiet, `ok`-only, `ko`-only") +) + +func main() { + flag.Parse() + + var h http.Handler + var ok, ko int + var mu sync.Mutex + + // Keep the start time to print since-time + start := time.Now() + + // Create the interval throttler + t := throttled.Interval(throttled.D(*delay), *bursts, &throttled.VaryBy{ + Path: true, + }, *maxkeys) + // Set the denied handler + t.DeniedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ko" { + log.Printf("KO: %s", time.Since(start)) + } + throttled.DefaultDeniedHandler.ServeHTTP(w, r) + mu.Lock() + defer mu.Unlock() + ko++ + }) + + // Throttle the OK handler + rand.Seed(time.Now().Unix()) + h = t.Throttle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ok" { + log.Printf("%s: ok: %s", r.URL.Path, time.Since(start)) + } + if *delayRes > 0 { + wait := time.Duration(rand.Intn(int(*delayRes))) + time.Sleep(wait) + } + w.WriteHeader(200) + mu.Lock() + defer mu.Unlock() + ok++ + })) + + // Print stats once in a while + go func() { + for _ = range time.Tick(10 * time.Second) { + mu.Lock() + log.Printf("ok: %d, ko: %d", ok, ko) + mu.Unlock() + } + }() + fmt.Println("server listening on port 9000") + http.ListenAndServe(":9000", h) +} diff --git a/vendor/gopkg.in/throttled/throttled.v1/examples/interval-vary/siege-urls b/vendor/gopkg.in/throttled/throttled.v1/examples/interval-vary/siege-urls new file mode 100644 index 000000000..9a2d0d312 --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/examples/interval-vary/siege-urls @@ -0,0 +1,4 @@ +http://localhost:9000/a +http://localhost:9000/b +http://localhost:9000/c + diff --git a/vendor/gopkg.in/throttled/throttled.v1/examples/interval/main.go b/vendor/gopkg.in/throttled/throttled.v1/examples/interval/main.go new file mode 100644 index 000000000..ef8ee2cb8 --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/examples/interval/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "flag" + "fmt" + "log" + "math/rand" + "net/http" + "sync" + "time" + + "gopkg.in/throttled/throttled.v1" +) + +var ( + delay = flag.Duration("delay", 200*time.Millisecond, "delay between calls") + bursts = flag.Int("bursts", 10, "number of bursts allowed") + delayRes = flag.Duration("delay-response", 0, "delay the response by a random duration between 0 and this value") + output = flag.String("output", "v", "type of output, one of `v`erbose, `q`uiet, `ok`-only, `ko`-only") +) + +func main() { + flag.Parse() + + var h http.Handler + var ok, ko int + var mu sync.Mutex + + // Keep the start time to print since-time + start := time.Now() + // Create the interval throttler + t := throttled.Interval(throttled.D(*delay), *bursts, nil, 0) + // Set its denied handler + t.DeniedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ko" { + log.Printf("KO: %s", time.Since(start)) + } + throttled.DefaultDeniedHandler.ServeHTTP(w, r) + mu.Lock() + defer mu.Unlock() + ko++ + }) + // Throttle the OK handler + rand.Seed(time.Now().Unix()) + h = t.Throttle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ok" { + log.Printf("ok: %s", time.Since(start)) + } + if *delayRes > 0 { + wait := time.Duration(rand.Intn(int(*delayRes))) + time.Sleep(wait) + } + w.WriteHeader(200) + mu.Lock() + defer mu.Unlock() + ok++ + })) + + // Print stats once in a while + go func() { + for _ = range time.Tick(10 * time.Second) { + mu.Lock() + log.Printf("ok: %d, ko: %d", ok, ko) + mu.Unlock() + } + }() + fmt.Println("server listening on port 9000") + http.ListenAndServe(":9000", h) +} diff --git a/vendor/gopkg.in/throttled/throttled.v1/examples/memstats/main.go b/vendor/gopkg.in/throttled/throttled.v1/examples/memstats/main.go new file mode 100644 index 000000000..50d4cc69b --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/examples/memstats/main.go @@ -0,0 +1,97 @@ +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "log" + "math/rand" + "net/http" + "runtime" + "sync" + "time" + + "gopkg.in/throttled/throttled.v1" +) + +var ( + numgc = flag.Int("gc", 0, "number of GC runs") + mallocs = flag.Int("mallocs", 0, "number of mallocs") + total = flag.Int("total", 0, "total number of bytes allocated") + allocs = flag.Int("allocs", 0, "number of bytes allocated") + refrate = flag.Duration("refresh", 0, "refresh rate of the memory stats") + delayRes = flag.Duration("delay-response", 0, "delay the response by a random duration between 0 and this value") + output = flag.String("output", "v", "type of output, one of `v`erbose, `q`uiet, `ok`-only, `ko`-only") +) + +func main() { + flag.Parse() + + var h http.Handler + var ok, ko int + var mu sync.Mutex + + // Keep the start time to print since-time + start := time.Now() + // Create the thresholds struct + thresh := throttled.MemThresholds(&runtime.MemStats{ + NumGC: uint32(*numgc), + Mallocs: uint64(*mallocs), + TotalAlloc: uint64(*total), + Alloc: uint64(*allocs), + }) + if *output != "q" { + log.Printf("thresholds: NumGC: %d, Mallocs: %d, Alloc: %dKb, Total: %dKb", thresh.NumGC, thresh.Mallocs, thresh.Alloc/1024, thresh.TotalAlloc/1024) + } + // Create the MemStats throttler + t := throttled.MemStats(thresh, *refrate) + // Set its denied handler + t.DeniedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ko" { + log.Printf("KO: %s", time.Since(start)) + } + throttled.DefaultDeniedHandler.ServeHTTP(w, r) + mu.Lock() + defer mu.Unlock() + ko++ + }) + + // Throttle the OK handler + rand.Seed(time.Now().Unix()) + h = t.Throttle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ok" { + log.Printf("ok: %s", time.Since(start)) + } + if *delayRes > 0 { + wait := time.Duration(rand.Intn(int(*delayRes))) + time.Sleep(wait) + } + // Read the whole file in memory, to actually use 64Kb (instead of streaming to w) + b, err := ioutil.ReadFile("test-file") + if err != nil { + throttled.Error(w, r, err) + return + } + _, err = w.Write(b) + if err != nil { + throttled.Error(w, r, err) + } + mu.Lock() + defer mu.Unlock() + ok++ + })) + + // Print stats once in a while + go func() { + var mem runtime.MemStats + for _ = range time.Tick(10 * time.Second) { + mu.Lock() + runtime.ReadMemStats(&mem) + log.Printf("ok: %d, ko: %d", ok, ko) + log.Printf("TotalAllocs: %d Kb, Allocs: %d Kb, Mallocs: %d, NumGC: %d", mem.TotalAlloc/1024, mem.Alloc/1024, mem.Mallocs, mem.NumGC) + mu.Unlock() + } + }() + fmt.Println("server listening on port 9000") + http.ListenAndServe(":9000", h) +} diff --git a/vendor/gopkg.in/throttled/throttled.v1/examples/memstats/test-file b/vendor/gopkg.in/throttled/throttled.v1/examples/memstats/test-file Binary files differnew file mode 100644 index 000000000..c97c12f9b --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/examples/memstats/test-file diff --git a/vendor/gopkg.in/throttled/throttled.v1/examples/rate-limit/main.go b/vendor/gopkg.in/throttled/throttled.v1/examples/rate-limit/main.go new file mode 100644 index 000000000..b00119f63 --- /dev/null +++ b/vendor/gopkg.in/throttled/throttled.v1/examples/rate-limit/main.go @@ -0,0 +1,101 @@ +package main + +import ( + "flag" + "fmt" + "log" + "math/rand" + "net/http" + "sync" + "time" + + "github.com/garyburd/redigo/redis" + "gopkg.in/throttled/throttled.v1" + "gopkg.in/throttled/throttled.v1/store" +) + +var ( + requests = flag.Int("requests", 10, "number of requests allowed in the time window") + window = flag.Duration("window", time.Minute, "time window for the limit of requests") + storeType = flag.String("store", "mem", "store to use, one of `mem` or `redis` (on default localhost port)") + delayRes = flag.Duration("delay-response", 0, "delay the response by a random duration between 0 and this value") + output = flag.String("output", "v", "type of output, one of `v`erbose, `q`uiet, `ok`-only, `ko`-only") +) + +func main() { + flag.Parse() + + var h http.Handler + var ok, ko int + var mu sync.Mutex + var st throttled.Store + + // Keep the start time to print since-time + start := time.Now() + // Create the rate-limit store + switch *storeType { + case "mem": + st = store.NewMemStore(0) + case "redis": + st = store.NewRedisStore(setupRedis(), "throttled:", 0) + default: + log.Fatalf("unsupported store: %s", *storeType) + } + // Create the rate-limit throttler, varying on path + t := throttled.RateLimit(throttled.Q{Requests: *requests, Window: *window}, &throttled.VaryBy{ + Path: true, + }, st) + + // Set its denied handler + t.DeniedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ko" { + log.Printf("KO: %s", time.Since(start)) + } + throttled.DefaultDeniedHandler.ServeHTTP(w, r) + mu.Lock() + defer mu.Unlock() + ko++ + }) + + // Throttle the OK handler + rand.Seed(time.Now().Unix()) + h = t.Throttle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if *output == "v" || *output == "ok" { + log.Printf("ok: %s", time.Since(start)) + } + if *delayRes > 0 { + wait := time.Duration(rand.Intn(int(*delayRes))) + time.Sleep(wait) + } + w.WriteHeader(200) + mu.Lock() + defer mu.Unlock() + ok++ + })) + + // Print stats once in a while + go func() { + for _ = range time.Tick(10 * time.Second) { + mu.Lock() + log.Printf("ok: %d, ko: %d", ok, ko) + mu.Unlock() + } + }() + fmt.Println("server listening on port 9000") + http.ListenAndServe(":9000", h) +} + +func setupRedis() *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 +} |