mirror of
https://github.com/stefanprodan/podinfo.git
synced 2026-02-28 17:10:19 +00:00
If enabled it will still delay randomly between 0-5 seconds. However, the functionality to fine grain this is added. Both seconds and milliseconds are supported now. Moreover, min/max values for random delay can be condigured through pflag params
67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package api
|
|
|
|
import (
|
|
"math/rand"
|
|
"net/http"
|
|
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
type RandomDelayMiddleware struct {
|
|
min int
|
|
max int
|
|
unit string
|
|
}
|
|
|
|
func NewRandomDelayMiddleware(minDelay, maxDelay int, delayUnit string) *RandomDelayMiddleware {
|
|
return &RandomDelayMiddleware{
|
|
min: minDelay,
|
|
max: maxDelay,
|
|
unit: delayUnit,
|
|
}
|
|
}
|
|
|
|
func (m *RandomDelayMiddleware) Handler(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var unit time.Duration
|
|
rand.Seed(time.Now().Unix())
|
|
switch m.unit {
|
|
case "s":
|
|
unit = time.Second
|
|
case "ms":
|
|
unit = time.Millisecond
|
|
default:
|
|
unit = time.Second
|
|
}
|
|
|
|
delay := rand.Intn(m.max-m.min) + m.min
|
|
time.Sleep(time.Duration(delay) * unit)
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// Delay godoc
|
|
// @Summary Delay
|
|
// @Description waits for the specified period
|
|
// @Tags HTTP API
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Router /delay/{seconds} [get]
|
|
// @Success 200 {object} api.MapResponse
|
|
func (s *Server) delayHandler(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
|
|
delay, err := strconv.Atoi(vars["wait"])
|
|
if err != nil {
|
|
s.ErrorResponse(w, r, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
time.Sleep(time.Duration(delay) * time.Second)
|
|
|
|
s.JSONResponse(w, r, map[string]int{"delay": delay})
|
|
}
|