-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconnection.go
116 lines (104 loc) · 2.76 KB
/
connection.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
// Copyright (c) 2014-2015 Oliver Eilhard. All rights reserved.
// Use of this source code is governed by the MIT license.
// See LICENSE file for details.
package balancers
import (
"net/http"
"net/url"
"sync"
"time"
)
// Connection is a single connection to a host. It is defined by a URL.
// It also maintains state in the form that a connection can be broken.
// TODO(oe) Not sure if this abstraction is necessary.
type Connection interface {
// URL to the host.
URL() *url.URL
// IsBroken must return true if the connection to URL is currently not available.
IsBroken() bool
}
// HttpConnection is a HTTP connection to a host.
// It implements the Connection interface and can be used by balancer
// implementations.
type HttpConnection struct {
sync.Mutex
url *url.URL
broken bool
heartbeatDuration time.Duration
heartbeatStop chan bool
}
// NewHttpConnection creates a new HTTP connection to the given URL.
func NewHttpConnection(url *url.URL) *HttpConnection {
c := &HttpConnection{
url: url,
heartbeatDuration: DefaultHeartbeatDuration,
heartbeatStop: make(chan bool),
}
c.checkBroken()
go c.heartbeat()
return c
}
// Close this connection.
func (c *HttpConnection) Close() error {
c.Lock()
defer c.Unlock()
c.heartbeatStop <- true // wait for heartbeat ticker to stop
c.broken = false
return nil
}
// HeartbeatDuration sets the duration in which the connection is checked.
func (c *HttpConnection) HeartbeatDuration(d time.Duration) *HttpConnection {
c.Lock()
defer c.Unlock()
c.heartbeatStop <- true // wait for heartbeat ticker to stop
c.broken = false
c.heartbeatDuration = d
go c.heartbeat()
return c
}
// heartbeat periodically checks if the connection is broken.
func (c *HttpConnection) heartbeat() {
ticker := time.NewTicker(c.heartbeatDuration)
for {
select {
case <-ticker.C:
c.checkBroken()
case <-c.heartbeatStop:
return
}
}
}
// checkBroken checks if the HTTP connection is alive.
func (c *HttpConnection) checkBroken() {
c.Lock()
defer c.Unlock()
// TODO(oe) Can we use HEAD?
req, err := http.NewRequest("GET", c.url.String(), nil)
if err != nil {
c.broken = true
return
}
// Add UA to heartbeat requests.
req.Header.Add("User-Agent", UserAgent)
// Use a standard HTTP client with a timeout of 5 seconds.
cl := &http.Client{Timeout: 5 * time.Second}
res, err := cl.Do(req)
if err == nil {
defer res.Body.Close()
if res.StatusCode == http.StatusOK {
c.broken = false
} else {
c.broken = true
}
} else {
c.broken = true
}
}
// URL returns the URL of the HTTP connection.
func (c *HttpConnection) URL() *url.URL {
return c.url
}
// IsBroken returns true if the HTTP connection is currently broken.
func (c *HttpConnection) IsBroken() bool {
return c.broken
}