2
0

Added ConnectionPool

fixes #7
This commit is contained in:
Jack Christensen
2013-04-19 15:48:57 -05:00
parent 310c7f5fd8
commit 6be77b4d64
2 changed files with 158 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
package pgx
type ConnectionPool struct {
connectionChannel chan *Connection
options map[string]string // options used when establishing connection
MaxConnections int
}
// options: options used by Connect
// MaxConnections: max simultaneous connections to use (currently all are immediately connected)
func NewConnectionPool(options map[string]string, MaxConnections int) (p *ConnectionPool, err error) {
p = new(ConnectionPool)
p.connectionChannel = make(chan *Connection, MaxConnections)
p.MaxConnections = MaxConnections
p.options = make(map[string]string)
for k, v := range options {
p.options[k] = v
}
for i := 0; i < p.MaxConnections; i++ {
var c *Connection
c, err = Connect(options)
if err != nil {
return
}
p.connectionChannel <- c
}
return
}
func (p *ConnectionPool) Acquire() (c *Connection) {
c = <-p.connectionChannel
return
}
func (p *ConnectionPool) Release(c *Connection) {
p.connectionChannel <- c
}
func (p *ConnectionPool) Close() {
for i := 0; i < p.MaxConnections; i++ {
c := <-p.connectionChannel
_ = c.Close()
}
}