-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpclient_keepalive.go
More file actions
62 lines (53 loc) · 1.23 KB
/
Copy pathhttpclient_keepalive.go
File metadata and controls
62 lines (53 loc) · 1.23 KB
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
60
61
62
package main
import (
"bufio"
"fmt"
"net"
"net/http"
"net/http/httputil"
"strings"
)
func main() {
sendMessages := []string{"HOGEHOGE", "FUGAFUGA", "PIYOPIYO"}
current := 0
var conn net.Conn = nil
// リトライ用にループで全体を囲む
for {
var err error
// まだコネクションを張ってない or リトライ時
if conn == nil {
conn, err = net.Dial("tcp", "localhost:8888")
if err != nil {
panic(err)
}
fmt.Printf("Access: %v\n", current)
}
// ポストで文字列を送るリクエストを作成
request, err := http.NewRequest("POST", "http://localhost:8888", strings.NewReader(sendMessages[current]))
if err != nil {
panic(err)
}
err = request.Write(conn)
if err != nil {
panic(err)
}
// サーバーから読み込む。タイムアウトはここでエラーになるのでリトライ
response, err := http.ReadResponse(bufio.NewReader(conn), request)
if err != nil {
fmt.Println("Retry")
conn = nil
continue
}
// 結果を表示
dump, err := httputil.DumpResponse(response, true)
if err != nil {
panic(err)
}
fmt.Println(string(dump))
// 全部送信していれば終了
current++
if current == len(sendMessages) {
break
}
}
}