Skip to content

Commit

Permalink
pool: improve interface and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
fatih committed Jul 18, 2014
1 parent 60731b8 commit 061f030
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Pool [![GoDoc](https://godoc.org/github.com/fatih/pool?status.svg)](http://godoc.org/github.com/fatih/pool) [![Build Status](https://travis-ci.org/fatih/pool.svg)](https://travis-ci.org/fatih/pool)
# Pool [![GoDoc](https://godoc.org/gopkg.in/fatih/pool.v1?status.svg)](https://godoc.org/gopkg.in/fatih/pool.v1) [![Build Status](https://travis-ci.org/fatih/pool.svg)](https://travis-ci.org/fatih/pool)


Pool is a thread safe connection pool for net.Conn interface. It can be used to
Expand Down Expand Up @@ -43,10 +43,10 @@ p.Put(conn)
p.Close()

// currently available connections in the pool
current := p.CurrentCapacity()
current := p.Len()

// maximum capacity of your pool
max := p.MaximumCapacity()
max := p.Cap()
```


Expand Down
9 changes: 4 additions & 5 deletions channel.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Package pool implements a pool of net.Conn interfaces to manage and reuse them.
package pool

import (
Expand Down Expand Up @@ -120,8 +119,8 @@ func (c *ChannelPool) Close() {
}
}

// MaximumCapacity returns the maximum capacity of the pool
func (c *ChannelPool) MaximumCapacity() int { return cap(c.getConns()) }
// Cap returns the maximum capacity of the pool
func (c *ChannelPool) Cap() int { return cap(c.getConns()) }

// CurrentCapacity returns the current capacity of the pool.
func (c *ChannelPool) CurrentCapacity() int { return len(c.getConns()) }
// Len returns the current capacity of the pool.
func (c *ChannelPool) Len() int { return len(c.getConns()) }
30 changes: 15 additions & 15 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func TestPool_Get(t *testing.T) {
}

// after one get, current capacity should be lowered by one.
if p.CurrentCapacity() != (InitialCap - 1) {
if p.Len() != (InitialCap - 1) {
t.Errorf("Get error. Expecting %d, got %d",
(InitialCap - 1), p.CurrentCapacity())
(InitialCap - 1), p.Len())
}

// get them all
Expand All @@ -58,9 +58,9 @@ func TestPool_Get(t *testing.T) {
}
wg.Wait()

if p.CurrentCapacity() != 0 {
if p.Len() != 0 {
t.Errorf("Get error. Expecting %d, got %d",
(InitialCap - 1), p.CurrentCapacity())
(InitialCap - 1), p.Len())
}

_, err = p.Get()
Expand All @@ -78,9 +78,9 @@ func TestPool_Put(t *testing.T) {
p.Put(conn)
}

if p.MaximumCapacity() != MaximumCap {
if p.Cap() != MaximumCap {
t.Errorf("Put error. Expecting %d, got %d",
1, p.CurrentCapacity())
1, p.Len())
}

err := p.Put(nil)
Expand All @@ -100,19 +100,19 @@ func TestPool_MaximumCapacity(t *testing.T) {
p, _ := newChannelPool()
defer p.Close()

if p.MaximumCapacity() != MaximumCap {
t.Errorf("MaximumCapacity error. Expecting %d, got %d",
MaximumCap, p.CurrentCapacity())
if p.Cap() != MaximumCap {
t.Errorf("Cap error. Expecting %d, got %d",
MaximumCap, p.Len())
}
}

func TestPool_UsedCapacity(t *testing.T) {
p, _ := newChannelPool()
defer p.Close()

if p.CurrentCapacity() != InitialCap {
if p.Len() != InitialCap {
t.Errorf("InitialCap error. Expecting %d, got %d",
InitialCap, p.CurrentCapacity())
InitialCap, p.Len())
}
}

Expand Down Expand Up @@ -143,12 +143,12 @@ func TestPool_Close(t *testing.T) {
t.Errorf("Close error, put conn should return an error")
}

if p.CurrentCapacity() != 0 {
t.Errorf("Close error used capacity. Expecting 0, got %d", p.CurrentCapacity())
if p.Len() != 0 {
t.Errorf("Close error used capacity. Expecting 0, got %d", p.Len())
}

if p.MaximumCapacity() != 0 {
t.Errorf("Close error max capacity. Expecting 0, got %d", p.MaximumCapacity())
if p.Cap() != 0 {
t.Errorf("Close error max capacity. Expecting 0, got %d", p.Cap())
}
}

Expand Down
19 changes: 10 additions & 9 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ var (
ErrPoolFull = errors.New("pool is full")
)

// Pool interface describes a pool implementation.
// Pool interface describes a pool implementation. A pool should have maximum
// capacity. An ideal pool is threadsafe and easy to use.
type Pool interface {
// Get returns a new connection from the pool. After using the connection it
// should be put back via the Put() method. If there is no new connection
// available in the pool, a new connection will be created via the Factory()
// method.
// Get returns a new connection from the pool. After using the connection
// it should be put back via the Put() method. If there is no new
// connection available in the pool it's up to the implementer how to act.
// It can create a new connection or return an error.
Get() (net.Conn, error)

// Put puts an existing connection into the pool. If the pool is full or
Expand All @@ -32,9 +33,9 @@ type Pool interface {
// pool is no longer usable.
Close()

// MaximumCapacity returns the maximum capacity of the pool
MaximumCapacity() int
// Cap returns the maximum capacity of the pool
Cap() int

// CurrentCapacity returns the current capacity of the pool.
CurrentCapacity() int
// Len returns the current capacity of the pool.
Len() int
}

0 comments on commit 061f030

Please sign in to comment.