-
Notifications
You must be signed in to change notification settings - Fork 0
/
50-concurrent-http-post.go
59 lines (48 loc) · 1.29 KB
/
50-concurrent-http-post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
func http_post(addr string, post_message string) (r string, err error) {
t1 := time.Now()
postBody := bytes.NewBuffer([]byte(post_message))
http_client := http.Client{
Timeout: 2 * time.Second,
}
resp, err := http_client.Post(addr, "application/json", postBody)
if err != nil {
t2 := time.Now()
return fmt.Sprintf("[%s] Post %s [%s] msg: %s cost time: %s", t2.Format(time.RFC3339), addr, "failed.", "Post request failed.", t2.Sub(t1)), err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t2 := time.Now()
return fmt.Sprintf("[%s] Post %s [%s] msg: %s cost time: %s", t2.Format(time.RFC3339), addr, "failed.", "Read body failed.", t2.Sub(t1)), err
}
defer resp.Body.Close()
t2 := time.Now()
return fmt.Sprintf("[%s] Post %s [%s] msg: %s cost time: %s", t2.Format(time.RFC3339), addr, "success", body, t2.Sub(t1)), nil
}
func main() {
post_request := ` {"email":"Toby@example.com","name":"Toby"} `
if len(os.Args) < 2 {
return
} else {
ch := make(chan string, 1)
for i := 1; i < len(os.Args); i++ {
url := os.Args[i]
go func(u string) {
r, _ := http_post(u, post_request)
ch <- r
}(url)
}
//check result
for i := 1; i < len(os.Args); i++ {
fmt.Println(<-ch)
}
}
}