From 75789c31c66b78b242fc3ad780c85c41db7d4c73 Mon Sep 17 00:00:00 2001 From: Victor Elias Date: Mon, 24 Jun 2024 17:42:56 +0100 Subject: [PATCH] [DEV-revert] box-only: Start a local proxy server to simulate an unreliable storage Just proxies to Minio with a random error chance --- main.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/main.go b/main.go index a42acb35b..d9dddc0a9 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "fmt" "log" "math/rand" + "net/http" + "net/http/httputil" "os" "os/signal" "strings" @@ -39,7 +41,35 @@ import ( "golang.org/x/sync/errgroup" ) +func startProxyServer() error { + proxy := &httputil.ReverseProxy{ + Director: func(req *http.Request) { + req.URL.Scheme = "http" + req.URL.Host = "localhost:9000" + }, + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + errorChance := float32(0.5) + if r.Method != "GET" { + errorChance = 0.95 + } + if rand.Float32() < errorChance { + glog.Errorf("Random error for path=%s", r.URL.Path) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + proxy.ServeHTTP(w, r) + }) + + return http.ListenAndServe(":9420", nil) +} + func main() { + go func() { + log.Fatal(startProxyServer()) + }() + err := flag.Set("logtostderr", "true") if err != nil { glog.Fatal(err)