Skip to content

Commit

Permalink
clientpool: Check Get after Close and return appropriate error
Browse files Browse the repository at this point in the history
Currently, when Get is Called after Close, we'll just get a nil client
from the pool and the next `c.IsOpen` check will just panic, which makes
it very hard to debug why. Add a check to return an appropriate error.
  • Loading branch information
fishy committed May 24, 2024
1 parent 2a4cb03 commit eec80c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion clientpool/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clientpool

import (
"context"
"errors"
"sync/atomic"
"time"

Expand Down Expand Up @@ -97,7 +98,10 @@ func (cp *channelPool) Get() (client Client, err error) {
}()

select {
case c := <-cp.pool:
case c, ok := <-cp.pool:
if !ok {
return nil, errors.New("clientpool: Get called after Close")
}
if c.IsOpen() {
return c, nil
}
Expand Down
7 changes: 7 additions & 0 deletions clientpool/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,11 @@ func testPool(t *testing.T, pool clientpool.Pool, openerCalled *atomic.Int32, mi
t.Logf("opener called %d times", openerCalled.Load())
},
)

t.Run("get-after-close", func(t *testing.T) {
_, err := pool.Get()
if err == nil {
t.Error("Want pool.Get to return an error, got nil")
}
})
}

0 comments on commit eec80c8

Please sign in to comment.