func main() { runtime.GOMAXPROCS(runtime.NumCPU()) link = linkio.NewLink(linkio.Throughput(rate)) gzipHandler := gziphandler.GzipHandler(http.HandlerFunc(handler)) http.HandleFunc("/data/", rateLimitedHandler) http.Handle("/", gzipHandler) http.Handle("/data/manifest.json", gzipHandler) log.Fatal(http.ListenAndServe(":8000", nil)) }
// LinkSpeed implements ChaosMonkey.LinkSpeed func (j *Jim) LinkSpeed() *linkio.Throughput { rand.Seed(time.Now().Unix()) if rand.Float64() < j.LinkSpeedAffect { lsDiff := j.LinkSpeedMax - j.LinkSpeedMin lsAffect := j.LinkSpeedMin + (lsDiff * rand.Float64()) f := linkio.Throughput(lsAffect) * linkio.BytePerSecond j.logf("Jim: Restricting throughput to %s\n", f) return &f } j.logf("Jim: Allowing unrestricted throughput") return nil }
func rateLimitedHandler(w http.ResponseWriter, r *http.Request) { var filePath string = "" if strings.EqualFold(r.URL.Path, "/") { filePath = "./index.html" } else { filePath = "." + r.URL.Path } var ok error lrw := NewLimitedResponseWriter(w) q := r.URL.Query() if (len(q["rate"])) != 0 { _, ok = strconv.Atoi(q["rate"][0]) if ok == nil { rate, _ = strconv.Atoi(q["rate"][0]) rate = rate * 1024 // in kBytes link.SetThroughput(linkio.Throughput(rate * 6 / 5)) } } if (len(q["latency"])) != 0 { _, ok = strconv.Atoi(q["latency"][0]) if ok == nil { latency, _ = strconv.Atoi(q["latency"][0]) } } if latency != 0 { time.Sleep(time.Duration(latency) * time.Millisecond) } file, err := os.Open(filePath) if err != nil { fmt.Printf("%s not found\n", filePath) w.WriteHeader(http.StatusNotFound) fmt.Fprint(w, "<html><body style='font-size:100px'>four-oh-four</body></html>") return } defer file.Close() //fileStat, err := os.Stat(filePath) //if err != nil { //fmt.Println(err) //} fmt.Printf("Rate limited serve %s with rate %d kbps and latency %d ms\n", filePath, rate/1024, latency) _, filename := path.Split(filePath) //t := fileStat.ModTime() //fmt.Printf("time %+v\n", t) http.ServeContent(lrw, r, filename, time.Now(), file) }
func handler(w http.ResponseWriter, r *http.Request) { var filePath string = "" if strings.EqualFold(r.URL.Path, "/") { filePath = "./index.html" } else { filePath = "." + r.URL.Path } var ok error q := r.URL.Query() if (len(q["rate"])) != 0 { _, ok = strconv.Atoi(q["rate"][0]) if ok == nil { rate, _ = strconv.Atoi(q["rate"][0]) rate = rate * 1024 link.SetThroughput(linkio.Throughput(rate * 6 / 5)) } } if (len(q["latency"])) != 0 { _, ok = strconv.Atoi(q["latency"][0]) if ok == nil { latency, _ = strconv.Atoi(q["latency"][0]) } } file, err := os.Open(filePath) if err != nil { fmt.Printf("%s not found\n", filePath) w.WriteHeader(http.StatusNotFound) fmt.Fprint(w, "<html><body style='font-size:100px'>four-oh-four</body></html>") return } defer file.Close() fileStat, err := os.Stat(filePath) if err != nil { fmt.Println(err) } fmt.Printf("serve %s\n", filePath) _, filename := path.Split(filePath) t := fileStat.ModTime() fmt.Printf("time %+v\n", t) http.ServeContent(w, r, filename, t, file) }