Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.