-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport_connection_pool.go
148 lines (129 loc) · 3.08 KB
/
transport_connection_pool.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
package main
import (
"time"
)
type TransportConnectionPool struct {
Transport *NetworkTransport
Node string
Chan chan *TransportConnection
PoolSize int
Closed bool
Profiler *PerformanceProfiler
}
// Get
func (this *TransportConnectionPool) GetConnection() *TransportConnection {
// Timeout
timeout := make(chan bool, 1)
go func() {
time.Sleep(1 * time.Second)
timeout <- true
}()
// Fetch from chan, or create on timeout
var tc *TransportConnection
select {
case tc = <-this.Chan:
if this.Transport.traceLog {
log.Infof("Pool %s hand out %s", this.Transport.serviceName, tc.id)
}
break
case <-timeout:
log.Warnf("Pool %s hand out timeout warning", this.Transport.serviceName)
tc = this._newConnection()
break
}
// Attach profiler
tc.AttachProfiler(this.Profiler.Start())
return tc
}
// Return
func (this *TransportConnectionPool) ReturnConnection(tc *TransportConnection) {
// Closed?
if this.Closed {
if this.Transport.traceLog {
log.Infof("Pool %s received returned %s, closing now", this.Transport.serviceName, tc.id)
}
tc.Close()
return
}
// Finalize profiler
tc.profilerMeasurement.Success()
// Into channel
select {
case this.Chan <- tc:
if this.Transport.traceLog {
log.Infof("Pool %s received returned %s", this.Transport.serviceName, tc.id)
}
break
default:
log.Warnf("Pool %s overflow of returned %s", this.Transport.serviceName, tc.id)
break
}
}
// Discard
func (this *TransportConnectionPool) DiscardConnection(tc *TransportConnection) {
// Close
tc.Close()
// Finalize profiler
tc.profilerMeasurement.Error()
// Log discard
if this.Transport.traceLog {
log.Infof("Pool %s received discarded %s", this.Transport.serviceName, tc.id)
}
// Open new one
go this._addConnection()
}
// Prepare
func (this *TransportConnectionPool) _prepare() {
for i := 0; i < this.PoolSize; i++ {
go this._addConnection()
}
}
// Close all, does not return to pool
func (this *TransportConnectionPool) Close() {
// Set closed flag
this.Closed = true
// Close
for i := 0; i < this.PoolSize; i++ {
select {
case tc := <-this.Chan:
tc.Close()
break
default:
// Ignore
}
}
}
// Add connection
func (this *TransportConnectionPool) _addConnection() {
if this.Closed {
log.Warnf("Not creating connection for closed pool %s", this.Transport.serviceName)
return
}
tc := this._newConnection()
if this.Transport.traceLog {
log.Infof("Pool %s added %s to %s", this.Transport.serviceName, tc.id, this.Node)
}
this.Chan <- tc
}
// New connection
func (this *TransportConnectionPool) _newConnection() *TransportConnection {
tc := newTransportConnection(this)
if this.Transport.traceLog {
log.Infof("Pool %s created %s", this.Transport.serviceName, tc.id)
}
tc.Connect()
return tc
}
// New pool
func newTransportConnectionPool(t *NetworkTransport, node string, n int) *TransportConnectionPool {
o := &TransportConnectionPool{
Node: node,
Transport: t,
Chan: make(chan *TransportConnection, n),
PoolSize: n,
Closed: false,
Profiler: newPerformanceProfiler(),
}
o._prepare()
return o
}