Last active
September 27, 2017 19:50
-
-
Save ikouchiha47/3c5d28fb63cc59bf1aee37e8a4caf794 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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