Skip to content

Commit

Permalink
core/connpool: infinite scrub() loop
Browse files Browse the repository at this point in the history
reads from the chan are immediately written back in to it, and so the outer
unconditional for-loop spins forever as the chan never runs out of elements.
  • Loading branch information
ignoramous committed Oct 20, 2024
1 parent b8e8cdc commit 3ffc767
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions intra/core/connpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,25 +237,38 @@ func (c *ConnPool[T]) clean() {
}

func (c *ConnPool[T]) scrub() {
for {
if c.closed.Load() {
return
if c.closed.Load() {
return
}

staged := make([]timedconn, 0)
defer func() {
for _, tconn := range staged {
kept := false
select {
case <-c.ctx.Done(): // closed
default:
select {
case c.p <- tconn: // put it back in
kept = true
case <-c.ctx.Done(): // closed
default: // pool full
}
}
if !kept {
CloseConn(tconn.c)
}
}
}()

for {
select {
case tconn := <-c.p:
if fresh(tconn.dob) && readable(tconn.c) {
select {
case c.p <- tconn: // update dob only on Put()
case <-c.ctx.Done(): // stop
CloseConn(tconn.c)
return
default: // full
CloseConn(tconn.c)
}
staged = append(staged, tconn)
} else {
CloseConn(tconn.c)
}
} // next
case <-c.ctx.Done():
return
default:
Expand Down

1 comment on commit 3ffc767

@ignoramous
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.