-
Notifications
You must be signed in to change notification settings - Fork 36
/
client.go
86 lines (65 loc) · 1.08 KB
/
client.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
package http2
import (
"container/list"
"sync"
"time"
"github.com/valyala/fasthttp"
)
type Ctx struct {
// Request ...
Request *fasthttp.Request
// Response ...
Response *fasthttp.Response
// Err ...
Err chan error
}
// Client ...
type Client struct {
d *Dialer
onRTT func(time.Duration)
lck sync.Mutex
conns list.List
}
func createClient(d *Dialer) *Client {
cl := &Client{
d: d,
}
return cl
}
func (cl *Client) Do(req *fasthttp.Request, res *fasthttp.Response) (err error) {
var c *Conn
cl.lck.Lock()
var next *list.Element
for e := cl.conns.Front(); c == nil; e = next {
if e != nil {
c = e.Value.(*Conn)
} else {
var err error
if c, err = cl.d.Dial(); err != nil {
cl.lck.Unlock()
return err
}
e = cl.conns.PushFront(c)
}
if !c.CanOpenStream() {
c = nil
next = e.Next()
}
if c != nil && c.Closed() {
next = e.Next()
cl.conns.Remove(e)
c = nil
}
}
cl.lck.Unlock()
ch := make(chan error, 1)
c.Write(&Ctx{
Request: req,
Response: res,
Err: ch,
})
select {
case err = <-ch:
}
return err
}