Skip to content

Instantly share code, notes, and snippets.

@josephspurrier
Created May 13, 2019 14:57
Show Gist options
  • Save josephspurrier/59947fdf33e0c17af30f8340d69c1206 to your computer and use it in GitHub Desktop.
Save josephspurrier/59947fdf33e0c17af30f8340d69c1206 to your computer and use it in GitHub Desktop.
Go AWS ELB Idle Timeout Test
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("%v %v %v %v\n", time.Now().Format("2006-01-02 03:04:05 PM"), r.RemoteAddr, r.Method, r.URL)
fmt.Fprintf(w, "ok")
})
http.ListenAndServe(":8081", mux)
}
package main
import (
"fmt"
"log"
"net"
"net/http"
"os"
"time"
)
func main() {
fmt.Println("Starting.")
go func() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("%v %v %v %v %v\n", time.Now().Format("2006-01-02 03:04:05 PM"), r.RemoteAddr, r.Method, r.URL, r.Header)
wait := 0
limit := 15
for wait < limit {
fmt.Println("Wait:", wait)
time.Sleep(time.Second * 1)
wait++
}
fmt.Fprintf(w, "Wait Done.")
})
mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok")
})
//http.ListenAndServe(":8080", mux)
server := &http.Server{Addr: ":8080", Handler: mux}
server.IdleTimeout = 2
server.ListenAndServe()
}()
req, err := http.NewRequest("POST", "http://"+os.Getenv("ELBNAME")+":8080", nil)
if err != nil {
log.Println("Error creating POST request: ", err)
return
}
req.Header.Set("Connection", "Keep-Alive")
// setting long timeout for http client since scheduler MUST wait for responses
client := http.Client{
Timeout: time.Hour * 72,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 2 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 3 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableKeepAlives: false,
},
}
res, err := client.Do(req)
if err != nil {
log.Println("Error sending POST request: ", err)
return
}
defer res.Body.Close() //nolint
fmt.Println("Returned:", res.StatusCode)
fmt.Println("Done.")
for {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment