Skip to content

Commit

Permalink
fix #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
monnand committed Oct 4, 2013
1 parent f6a9e38 commit 9734045
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ func (self *pooledConn) Write(b []byte) (n int, err error) {
}

func (self *pooledConn) Close() error {
defer func() {
if r := recover(); r != nil {
// this will be true if
// self.pool.freeChan
// is closed.
// i.e. the pool is closed
self.conn.Close()
}
}()
req := &freeRequest{self}
self.n++
self.pool.freeChan <- req
Expand Down
1 change: 1 addition & 0 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ func (self *Pool) Close() {

// Get a connection. The function will block the goroutine until there is
// a connection available or an error occured.
// Calling Get() on a closed pool will lead to panic.
func (self *Pool) Get() (conn net.Conn, err error) {
connCh := make(chan *pooledConn)
errCh := make(chan error)
Expand Down
19 changes: 19 additions & 0 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,22 @@ func TestConcurrentAccess(t *testing.T) {
}
wg.Wait()
}

func TestCloseConnAfterClosingPool(t *testing.T) {
max := 10
manager := &fakeConnManager{nil, nil}
pool := NewPool(max, max, manager)
c, err := pool.Get()
if err != nil {
t.Errorf("Error: %v", err)
}

if c == nil {
t.Errorf("nil conn")
}
// Close the pool first.
pool.Close()

// Then close the connection
c.Close()
}

0 comments on commit 9734045

Please sign in to comment.