Skip to content

Instantly share code, notes, and snippets.

@stuartnelson3
Created July 14, 2021 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartnelson3/528efb313bb1aeaeabf72afbb6223846 to your computer and use it in GitHub Desktop.
Save stuartnelson3/528efb313bb1aeaeabf72afbb6223846 to your computer and use it in GitHub Desktop.
reverse proxy to intermittently interrupt requests to /_xpack
package main
import (
"log"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
"time"
)
func main() {
rpURL, err := url.Parse("http://localhost:9200")
if err != nil {
log.Fatal(err)
}
rand.Seed(time.Now().UnixNano())
rp := httputil.NewSingleHostReverseProxy(rpURL)
log.Printf("listening on :9201, proxying to %v\n", rpURL)
log.Fatal(http.ListenAndServe(":9201", wrap(rp)))
}
func wrap(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/_xpack" {
log.Println("hit xpack path")
if rand.Intn(100) < 40 {
log.Println("responding with error")
http.Error(w, `{"error":"whoops"}`, http.StatusInternalServerError)
return
}
}
h.ServeHTTP(w, r)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment