This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
conn.go
126 lines (109 loc) · 2.49 KB
/
conn.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
package pooly
import (
"net"
"time"
)
// Conn abstracts user connections that are part of a Pool.
type Conn struct {
iface interface{}
timer *time.Timer
timerStop chan bool
closed bool
host *Host
gottenAt time.Time
}
// NewConn creates a new connection container, wrapping up a user defined connection object.
func NewConn(i interface{}) *Conn {
return &Conn{
iface: i,
timerStop: make(chan bool),
}
}
// Interface returns an interface referring to the underlying user object.
func (c *Conn) Interface() interface{} {
return c.iface
}
// NetConn is a helper for underlying user objects that satisfy the standard library net.Conn interface.
func (c *Conn) NetConn() net.Conn {
if c.iface == nil {
return nil
}
return c.iface.(net.Conn)
}
func (c *Conn) isClosed() bool {
return c.closed
}
func (c *Conn) setClosed() {
if c.timer != nil {
if c.timer.Stop() {
c.timerStop <- true
}
}
c.closed = true
}
func (c *Conn) setIdle(p *Pool) {
if p.ConnIdleTimeout > 0 {
c.timer = time.NewTimer(p.ConnIdleTimeout)
go func() {
select {
case <-c.timerStop:
return
case <-c.timer.C:
// The connection has been idle for too long,
// send it to the garbage collector
p.gc <- c
}
}()
}
}
func (c *Conn) setActive() bool {
if c.timer != nil {
if !c.timer.Stop() {
return false
}
c.timerStop <- true
}
return true
}
func (c *Conn) setHost(h *Host) {
c.host = h
}
func (c *Conn) setTime(t time.Time) {
c.gottenAt = t
}
func (c *Conn) diffTime() time.Duration {
return time.Now().Sub(c.gottenAt)
}
// Release releases the connection back to its linked service.
// It takes an error state which defines whether or not the connection failed during operation and
// a score between 0 and 1 which describes how well the connection performed (e.g inverse response time, up/down ...).
// If the error state indicates a fatal error (determined by Driver.Temporary), the score is forced to the value 0 (HostDown).
func (c *Conn) Release(err interface{}, score float64) error {
var e error
if c.host == nil {
return ErrNoHostAvailable
}
if score < 0 || score > 1 {
return ErrInvalidArg
}
switch v := err.(type) {
case error:
e = v
case *error:
e = *v
case nil:
e = nil
default:
return ErrInvalidArg
}
h := c.host
c.host = nil
return h.releaseConn(c, e, score)
}
// Address returns the address of the host bound to the connection.
func (c *Conn) Address() string {
if c.host == nil {
return ""
}
return c.host.pool.Address()
}