forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reserved_pool.go
82 lines (69 loc) · 2.04 KB
/
reserved_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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tabletserver
import (
"time"
"github.com/youtube/vitess/go/pools"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/sync2"
)
type ReservedPool struct {
pool *pools.Numbered
lastId sync2.AtomicInt64
connFactory CreateConnectionFunc
}
func NewReservedPool(name string) *ReservedPool {
rp := &ReservedPool{pool: pools.NewNumbered(), lastId: 1}
stats.Publish(name+"Size", stats.IntFunc(rp.pool.Size))
return rp
}
func (rp *ReservedPool) Open(connFactory CreateConnectionFunc) {
rp.connFactory = connFactory
}
func (rp *ReservedPool) Close() {
for _, v := range rp.pool.GetOutdated(time.Duration(0), "for close") {
conn := v.(*reservedConnection)
conn.Close()
rp.pool.Unregister(conn.connectionId)
}
}
func (rp *ReservedPool) CreateConnection() (connectionId int64) {
conn, err := rp.connFactory()
if err != nil {
panic(NewTabletErrorSql(FATAL, err))
}
connectionId = rp.lastId.Add(1)
rconn := &reservedConnection{DBConnection: conn, connectionId: connectionId, pool: rp}
rp.pool.Register(connectionId, rconn)
return connectionId
}
func (rp *ReservedPool) CloseConnection(connectionId int64) {
conn := rp.Get(connectionId).(*reservedConnection)
conn.Close()
rp.pool.Unregister(connectionId)
}
// You must call Recycle on the PoolConnection once done.
func (rp *ReservedPool) Get(connectionId int64) PoolConnection {
v, err := rp.pool.Get(connectionId, "for query")
if err != nil {
panic(NewTabletError(FAIL, "Error getting connection %d: %v", connectionId, err))
}
return v.(*reservedConnection)
}
func (rp *ReservedPool) StatsJSON() string {
return rp.pool.StatsJSON()
}
type reservedConnection struct {
*DBConnection
connectionId int64
pool *ReservedPool
inUse bool
}
func (pr *reservedConnection) Recycle() {
if pr.IsClosed() {
pr.pool.pool.Unregister(pr.connectionId)
} else {
pr.pool.pool.Put(pr.connectionId)
}
}