Skip to content

Instantly share code, notes, and snippets.

@Deleplace
Created July 23, 2019 14:28
Show Gist options
  • Save Deleplace/1f4856df8f154263594f9dee105e73df to your computer and use it in GitHub Desktop.
Save Deleplace/1f4856df8f154263594f9dee105e73df to your computer and use it in GitHub Desktop.
Server having a period of freeze or extreme slowness. See https://youtu.be/lJ8ydIuPFeU?t=1274
// Copyright 2019 Google LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"log"
"net/http"
"sync"
"time"
)
var mu sync.Mutex
var n int
var start time.Time
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
n++
if n == 1 {
start = time.Now()
}
current := n
mu.Unlock()
log.Println("Processing", current)
defer log.Println("Finished", current)
fast := 50 * time.Millisecond
factor := 1
t := time.Since(start)
if t > 5*time.Second && t < 10*time.Second {
// Extremely slow period during 5s
factor = 120
// Somewhat slow period during 5s
// factor = 5
}
processDuration := time.Duration(factor) * fast
time.Sleep(processDuration)
fmt.Fprintln(w, "ok", current)
})
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment