-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
139 lines (121 loc) · 2.74 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"flag"
"fmt"
"html"
"log"
"net"
"net/http"
"time"
"github.com/Pushwoosh/go-connection-pool/pkg/connection"
"github.com/Pushwoosh/go-connection-pool/pkg/message"
"github.com/Pushwoosh/go-connection-pool/pkg/pool"
)
func startWebServer() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Millisecond * 50)
_, _ = fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
go func() {
err := http.ListenAndServe("localhost:8080", nil)
if err != nil {
log.Printf("%#v", err)
}
}()
}
type conn struct {
realConn http.Client
ID int
State bool
}
func (c *conn) String() string {
return fmt.Sprintf("<%d,%t>", c.ID, c.Live())
}
func (c *conn) Live() bool {
return c.State
}
func (c *conn) Serve(in chan message.Message, out chan message.Message) {
for m := range in {
mTyped, ok := m.(msg)
if !ok {
continue
}
log.Printf("[%s] sending: %#v\n", c, m)
var err error
if mTyped.resp, err = c.realConn.Get(mTyped.url); err != nil || mTyped.resp.StatusCode != 200 {
mTyped.status = "FAIL"
} else {
mTyped.status = "OK"
}
out <- mTyped
}
log.Printf("[%s] close me!\n", c)
c.State = false
}
type msg struct {
url string
status string
resp *http.Response
}
var (
reqNum = flag.Int("req-num", 1000, "Num of requests")
connNum = flag.Int("conn-num", 100, "Num of parallel connections")
url = flag.String("url", "http://localhost:8080/hello", "Url to requests")
timeoutDuration = time.Second * 5
)
func main() {
flag.Parse()
counter := 0
cfg := pool.Config{
MaxConnections: *connNum,
CheckInterval: timeoutDuration,
Dialer: func() (connection.Connection, error) {
counter++
c := &conn{
ID: counter,
State: true,
realConn: http.Client{
Timeout: timeoutDuration,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: timeoutDuration,
}).Dial,
TLSHandshakeTimeout: timeoutDuration,
},
},
}
log.Printf("New connection: %s\n", c)
return c, nil
},
}
p := pool.NewPool(cfg)
log.Printf("Started with reqNum=%d, url=%s cfg=%#v", *reqNum, *url, cfg)
defer log.Println("Completed")
inChan := make(chan message.Message)
outChan := make(chan message.Message)
go func() {
for count := 0; count < *reqNum; count++ {
inChan <- msg{url: *url}
}
close(inChan)
}()
go func() {
count := 0
for m := range outChan {
count++
mTyped, ok := m.(msg)
if !ok {
continue
}
log.Printf("%d) Receive: %#v\n", count, mTyped.resp)
}
}()
if *url == "http://localhost:8080/hello" {
startWebServer()
}
log.Println("Start serving")
if err := p.Serve(inChan, outChan); err != nil {
log.Printf("%#v\n", err)
}
close(outChan)
}