Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions log.go

This file was deleted.

49 changes: 0 additions & 49 deletions log_test.go

This file was deleted.

7 changes: 5 additions & 2 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package puddle
import (
"context"
"errors"
"math/bits"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -543,12 +544,14 @@ func (p *Pool[T]) TryAcquire(ctx context.Context) (*Resource[T], error) {
// TODO: Replace this with acquireSem.TryAcquireAll() if it gets to
// upstream. https://github.com/golang/sync/pull/19
func acquireSemAll(sem *semaphore.Weighted, num int) int {
if num <= 0 {
panic("aquireSemAll: num <= 0")
}
Comment on lines +547 to +549
Copy link

@redloaf redloaf Apr 25, 2025

Choose a reason for hiding this comment

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

This is a slight behavior change but is more predictable. If num is negative, sem.TryAcquire can succeed and has the same effect as sem.Release(-num). However, it fails if there are any blocked Acquire calls. Before your changes, this has the potential to result in a sporadic panic for a condition that should probably always result in a panic (as you've written it).

Example and source

if sem.TryAcquire(int64(num)) {
return num
}

var acquired int
for i := int(log2Int(num)); i >= 0; i-- {
for i := bits.Len64(uint64(num)) - 1; i >= 0; i-- {
val := 1 << i
if sem.TryAcquire(int64(val)) {
acquired += val
Expand Down