Skip to content

Instantly share code, notes, and snippets.

@KauzClay
Created October 14, 2019 18:20
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 KauzClay/2d84e0ce1d1884a7bf3ca952ebb57630 to your computer and use it in GitHub Desktop.
Save KauzClay/2d84e0ce1d1884a7bf3ca952ebb57630 to your computer and use it in GitHub Desktop.
Testing components for panic reproduction
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
backendServer := &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "this call was relayed by the reverse proxy")
}),
}
fmt.Printf("starting app on port %s\n", port)
err := backendServer.ListenAndServe()
log.Fatal(fmt.Sprintf("BACKEND ERROR: %s", err))
}
package main
import (
"bytes"
"flag"
"fmt"
"net/http"
"strings"
)
func main() {
var n int
// 10 goroutines is a good number to see the panic happen quickly on
// our test environments, though this may not apply to all machines
flag.IntVar(&n, "n", 10, "number of goroutines")
flag.Parse()
// Request needs a larger body to see the error happen more quickly
// It is reproducable with smaller body sizes, but it takes longer to fail
bodyString := strings.Repeat("a", 2048)
client := &http.Client{}
for i := 0; i < n; i++ {
go func() {
for {
buf := bytes.NewBufferString(bodyString)
req, _ := http.NewRequest("POST", "http://127.0.0.1:8080", buf)
req.Header.Add("Expect", "100-continue")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Request Failed: %s\n", err.Error())
} else {
resp.Body.Close()
}
}
}()
}
select {}
}
package main
import (
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"time"
)
func main() {
rpURL, err := url.Parse("http://127.0.0.1:3000")
if err != nil {
log.Fatal(err)
}
reverseProxy := httputil.NewSingleHostReverseProxy(rpURL)
reverseProxy.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 0 * time.Second,
}
frontendProxy := &http.Server{
Addr: ":8080",
Handler: reverseProxy,
}
fmt.Println("Starting proxy on 8080")
err = frontendProxy.ListenAndServe()
log.Fatal(fmt.Sprintf("PROXY FATAL ERROR: %s", err))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment