forked from yarpc/yarpc-go
/
peer.go
156 lines (136 loc) · 4.14 KB
/
peer.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package http
import (
"net"
"time"
"go.uber.org/yarpc/api/peer"
"go.uber.org/yarpc/peer/hostport"
)
type httpPeer struct {
*hostport.Peer
transport *Transport
addr string
changed chan struct{}
released chan struct{}
timer *time.Timer
}
func newPeer(pid hostport.PeerIdentifier, t *Transport) *httpPeer {
// Create a defused timer for later use.
timer := time.NewTimer(0)
if !timer.Stop() {
<-timer.C
}
return &httpPeer{
Peer: hostport.NewPeer(pid, t),
transport: t,
addr: pid.Identifier(),
changed: make(chan struct{}, 1),
released: make(chan struct{}, 0),
timer: timer,
}
}
// The HTTP transport polls for whether a peer is available by attempting to
// connect. The transport does not preserve the connection because HTTP servers
// may behave oddly if they don't receive a request immediately.
// Instead, we treat the peer as available until proven otherwise with a fresh
// connection attempt.
func (p *httpPeer) isAvailable() bool {
// If there's no open connection, we probe by connecting.
dialer := &net.Dialer{Timeout: p.transport.connTimeout}
conn, err := dialer.Dial("tcp", p.addr)
if conn != nil {
conn.Close()
}
if conn != nil && err == nil {
return true
}
return false
}
func (p *httpPeer) OnDisconnected() {
p.Peer.SetStatus(peer.Unavailable)
// Kick the state change channel (if it hasn't been kicked already).
select {
case p.changed <- struct{}{}:
default:
}
}
func (p *httpPeer) Release() {
close(p.released)
}
func (p *httpPeer) MaintainConn() {
var attempts uint
backoff := p.transport.connBackoffStrategy.Backoff()
// Wait for start (so we can be certain that we have a channel).
<-p.transport.once.Started()
// Attempt to retain an open connection to each peer so long as it is
// retained.
for {
p.Peer.SetStatus(peer.Connecting)
if p.isAvailable() {
p.Peer.SetStatus(peer.Available)
// Reset on success
attempts = 0
if !p.waitForChange() {
break
}
} else {
p.Peer.SetStatus(peer.Unavailable)
// Back-off on fail
if !p.sleep(backoff.Duration(attempts)) {
break
}
attempts++
}
}
p.Peer.SetStatus(peer.Unavailable)
p.transport.connectorsGroup.Done()
}
// waitForChange waits for the transport to send a peer connection status
// change notification, but exits early if the transport releases the peer or
// stops. waitForChange returns whether it is resuming due to a connection
// status change event.
func (p *httpPeer) waitForChange() (changed bool) {
// Wait for a connection status change
select {
case <-p.changed:
return true
case <-p.released:
return false
case <-p.transport.once.Stopping():
return false
}
}
// sleep waits for a duration, but exits early if the transport releases the
// peer or stops. sleep returns whether it successfully waited the entire
// duration.
func (p *httpPeer) sleep(delay time.Duration) (completed bool) {
p.timer.Reset(delay)
select {
case <-p.timer.C:
return true
case <-p.released:
case <-p.transport.once.Stopping():
}
if !p.timer.Stop() {
<-p.timer.C
}
return false
}