Skip to content

Commit

Permalink
Close ID pool when closing client
Browse files Browse the repository at this point in the history
For every client that `SimpleClientFactory` creates, a goroutine is started for the `idPool`.

When you call `client.Close()` the connection is closed but the `idPool` remains running.

With this change, the `idPool` is properly closed.

Co-authored-by: Adrian Philipp <adri@users.noreply.github.com>
  • Loading branch information
ruudk and adri committed Sep 15, 2020
1 parent 4371ad4 commit 26b1b08
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ type idPool struct {
IDs chan uint16
}

// Close closes the IDs channel
func (p *idPool) Close() {
if p.IDs != nil {
close(p.IDs)
}
}

// AllocID implements Client.AllocID
func (p *idPool) Alloc() uint16 {
return <-p.IDs
Expand Down Expand Up @@ -98,6 +105,11 @@ func newIDs(limit uint32) (p idPool) {
// Ref: https://fast-cgi.github.io/spec#33-records
ids := make(chan uint16)
go func(maxID uint16) {
// Recover when IDs channel is closed
defer func() {
recover()
}()

for i := uint16(0); i < maxID; i++ {
ids <- i
}
Expand Down Expand Up @@ -342,6 +354,7 @@ func (c *client) Do(req *Request) (resp *ResponsePipe, err error) {
// If the inner connection has been closed before,
// this method would do nothing and return nil
func (c *client) Close() (err error) {
c.ids.Close()
if c.conn == nil {
return
}
Expand Down

0 comments on commit 26b1b08

Please sign in to comment.