summaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/examples/interval
diff options
context:
space:
mode:
author=Corey Hulen <corey@hulen.com>2015-11-23 15:53:48 -0800
committer=Corey Hulen <corey@hulen.com>2015-11-23 15:53:48 -0800
commit4f4cd5e63573da4d6edcc7d4213afaca67c19f88 (patch)
treecefbc7af53629d97644ca2f6b2369e9d879f0101 /Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/examples/interval
parentf8a3c9a14edca6df0647d89cf225f2470cbe025c (diff)
downloadchat-4f4cd5e63573da4d6edcc7d4213afaca67c19f88.tar.gz
chat-4f4cd5e63573da4d6edcc7d4213afaca67c19f88.tar.bz2
chat-4f4cd5e63573da4d6edcc7d4213afaca67c19f88.zip
upgrading libs
Diffstat (limited to 'Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/examples/interval')
-rw-r--r--Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/examples/interval/main.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/examples/interval/main.go b/Godeps/_workspace/src/gopkg.in/throttled/throttled.v1/examples/interval/main.go
new file mode 100644
index 000000000..f89067816
--- /dev/null
+++ b/Godeps/_workspace/src/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"
+
+ "github.com/mattermost/platform/Godeps/_workspace/src/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)
+}