Files
podinfo/pkg/api/delay.go
Chris Loukas 20a136a73c Revisit random-delay
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
2020-06-25 11:41:21 +03:00

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})
}