Manager: add CLI argument to add random delay to HTTP responses

add `-delay` CLI argument, which adds a random delay of around 250ms to
all HTTP responses.

The web interface is quite asynchronous in nature, and having more
randomness and more visible delays in there will help development.

This CLI argument should not be used in production.
This commit is contained in:
Sybren A. Stüvel 2022-05-30 15:10:07 +02:00
parent 6f4fc29145
commit 6864ca2e75

View File

@ -7,6 +7,7 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"math/rand"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -42,8 +43,9 @@ import (
) )
var cliArgs struct { var cliArgs struct {
version bool version bool
writeConfig bool writeConfig bool
delayResponses bool
} }
const developmentWebInterfacePort = 8081 const developmentWebInterfacePort = 8081
@ -191,6 +193,9 @@ func buildWebService(
// Ensure panics when serving a web request won't bring down the server. // Ensure panics when serving a web request won't bring down the server.
e.Use(middleware.Recover()) e.Use(middleware.Recover())
// For development of the web interface, to get a less predictable order of asynchronous requests.
e.Use(randomDelayMiddleware)
// Disabled, as it causes issues with "204 No Content" responses. // Disabled, as it causes issues with "204 No Content" responses.
// TODO: investigate & file a bug report. Adding the check on an empty slice // TODO: investigate & file a bug report. Adding the check on an empty slice
// seems to fix it: // seems to fix it:
@ -324,6 +329,8 @@ func parseCliArgs() {
flag.BoolVar(&debug, "debug", false, "Enable debug-level logging.") flag.BoolVar(&debug, "debug", false, "Enable debug-level logging.")
flag.BoolVar(&trace, "trace", false, "Enable trace-level logging.") flag.BoolVar(&trace, "trace", false, "Enable trace-level logging.")
flag.BoolVar(&cliArgs.writeConfig, "write-config", false, "Writes configuration to flamenco-manager.yaml, then exits.") flag.BoolVar(&cliArgs.writeConfig, "write-config", false, "Writes configuration to flamenco-manager.yaml, then exits.")
flag.BoolVar(&cliArgs.delayResponses, "delay", false,
"Add a random delay to any HTTP responses. This aids in development of Flamenco Manager's web frontend.")
flag.Parse() flag.Parse()
@ -403,3 +410,20 @@ func corsOrigins(urls []url.URL) []string {
log.Debug().Str("origins", strings.Join(origins, " ")).Msg("accepted CORS origins") log.Debug().Str("origins", strings.Join(origins, " ")).Msg("accepted CORS origins")
return origins return origins
} }
// randomDelayMiddleware sleeps for a random period of time, as a development tool for frontend work.
func randomDelayMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
err := next(c)
// Delay the response a bit.
var duration int64 = int64(rand.NormFloat64()*250 + 125) // in msec
if duration > 0 {
if duration > 1000 {
duration = 1000 // Cap at one second.
}
time.Sleep(time.Duration(duration) * time.Millisecond)
}
return err
}
}