Skip to content

Instantly share code, notes, and snippets.

@ikouchiha47
Last active September 27, 2017 19:50
Show Gist options
  • Save ikouchiha47/3c5d28fb63cc59bf1aee37e8a4caf794 to your computer and use it in GitHub Desktop.
Save ikouchiha47/3c5d28fb63cc59bf1aee37e8a4caf794 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"time"
)
func NewHttpClient() *http.Client {
tr := &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
}
return &http.Client{
Transport: tr,
Timeout: time.Second * 30,
}
}
func main() {
client := NewHttpClient()
for i := 0; i < 6; i++ {
fmt.Println("sending request ", i)
res, err := client.Get("http://localhost:8080")
if err != nil {
fmt.Println("Failed ", err)
}
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
time.Sleep(10 * time.Second)
}
}
package main
import (
"fmt"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Println("hello")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"hello": "world"}`))
}
func main() {
http.HandleFunc("/", hello)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment