-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
42 lines (37 loc) · 992 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"demo/fq"
"log"
"math/rand"
"net/http"
"runtime"
"strconv"
"time"
)
func echoHandler(w http.ResponseWriter, r *http.Request) {
if sleepTimeMillis, err := strconv.Atoi(r.Header.Get("sleep")); err == nil {
sleepTime := time.Millisecond * time.Duration(float64(sleepTimeMillis)*(1+rand.Float64()))
time.Sleep(sleepTime)
}
w.Write([]byte("loktar ogar"))
}
func main() {
runtime.GOMAXPROCS(2)
FQServer(echoHandler)
}
func FQServer(actualHandler http.HandlerFunc) {
http.HandleFunc("/", newHandler(fq.InitQueues(100, 1), actualHandler))
log.Fatal(http.ListenAndServe(":8080", nil))
}
func newHandler(allQueues []*fq.Queue, delegateHandler http.HandlerFunc) http.HandlerFunc {
if len(allQueues) == 0 {
return delegateHandler
}
return func(w http.ResponseWriter, r *http.Request) {
// w.WriteHeader(http.StatusConflict)
// fmt.Println("limitted!!")
// w.Write([]byte("limitted"))
// panic("not matching buckets")
delegateHandler(w, r)
}
}