Replace useless locking by atomic variable
This commit is contained in:
committed by
Jack Christensen
parent
93a3f7de51
commit
6214680aa8
@@ -1,6 +1,6 @@
|
|||||||
module github.com/jackc/puddle/v2
|
module github.com/jackc/puddle/v2
|
||||||
|
|
||||||
go 1.18
|
go 1.19
|
||||||
|
|
||||||
require github.com/stretchr/testify v1.8.0
|
require github.com/stretchr/testify v1.8.0
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -125,7 +126,7 @@ type Pool[T any] struct {
|
|||||||
acquireCount int64
|
acquireCount int64
|
||||||
acquireDuration time.Duration
|
acquireDuration time.Duration
|
||||||
emptyAcquireCount int64
|
emptyAcquireCount int64
|
||||||
canceledAcquireCount int64
|
canceledAcquireCount atomic.Int64
|
||||||
|
|
||||||
resetCount int
|
resetCount int
|
||||||
|
|
||||||
@@ -254,7 +255,7 @@ func (p *Pool[T]) Stat() *Stat {
|
|||||||
maxResources: p.maxSize,
|
maxResources: p.maxSize,
|
||||||
acquireCount: p.acquireCount,
|
acquireCount: p.acquireCount,
|
||||||
emptyAcquireCount: p.emptyAcquireCount,
|
emptyAcquireCount: p.emptyAcquireCount,
|
||||||
canceledAcquireCount: p.canceledAcquireCount,
|
canceledAcquireCount: p.canceledAcquireCount.Load(),
|
||||||
acquireDuration: p.acquireDuration,
|
acquireDuration: p.acquireDuration,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,15 +294,11 @@ func (ctx *valueCancelCtx) Value(key any) any { return ctx.valueCtx.Va
|
|||||||
// the problem of it being impossible to create resources when the time to create a resource is greater than any one
|
// the problem of it being impossible to create resources when the time to create a resource is greater than any one
|
||||||
// caller of Acquire is willing to wait.
|
// caller of Acquire is willing to wait.
|
||||||
func (p *Pool[T]) Acquire(ctx context.Context) (*Resource[T], error) {
|
func (p *Pool[T]) Acquire(ctx context.Context) (*Resource[T], error) {
|
||||||
if doneChan := ctx.Done(); doneChan != nil {
|
select {
|
||||||
select {
|
case <-ctx.Done():
|
||||||
case <-ctx.Done():
|
p.canceledAcquireCount.Add(1)
|
||||||
p.cond.L.Lock()
|
return nil, ctx.Err()
|
||||||
p.canceledAcquireCount += 1
|
default:
|
||||||
p.cond.L.Unlock()
|
|
||||||
return nil, ctx.Err()
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.acquire(ctx)
|
return p.acquire(ctx)
|
||||||
@@ -387,9 +384,7 @@ func (p *Pool[T]) acquire(ctx context.Context) (*Resource[T], error) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
p.cond.L.Lock()
|
p.canceledAcquireCount.Add(1)
|
||||||
p.canceledAcquireCount += 1
|
|
||||||
p.cond.L.Unlock()
|
|
||||||
return nil, ctx.Err()
|
return nil, ctx.Err()
|
||||||
case err := <-constructErrCh:
|
case err := <-constructErrCh:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -421,9 +416,7 @@ func (p *Pool[T]) acquire(ctx context.Context) (*Resource[T], error) {
|
|||||||
p.cond.Signal()
|
p.cond.Signal()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
p.cond.L.Lock()
|
p.canceledAcquireCount.Add(1)
|
||||||
p.canceledAcquireCount += 1
|
|
||||||
p.cond.L.Unlock()
|
|
||||||
return nil, ctx.Err()
|
return nil, ctx.Err()
|
||||||
case <-waitChan:
|
case <-waitChan:
|
||||||
}
|
}
|
||||||
@@ -526,15 +519,15 @@ func (p *Pool[T]) CreateResource(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.cond.L.Lock()
|
p.cond.L.Lock()
|
||||||
|
defer p.cond.L.Unlock()
|
||||||
|
|
||||||
// If closed while constructing resource then destroy it and return an error
|
// If closed while constructing resource then destroy it and return an error
|
||||||
if p.closed {
|
if p.closed {
|
||||||
go p.destructResourceValue(res.value)
|
go p.destructResourceValue(res.value)
|
||||||
p.cond.L.Unlock()
|
|
||||||
return ErrClosedPool
|
return ErrClosedPool
|
||||||
}
|
}
|
||||||
p.allResources = append(p.allResources, res)
|
p.allResources = append(p.allResources, res)
|
||||||
p.idleResources = append(p.idleResources, res)
|
p.idleResources = append(p.idleResources, res)
|
||||||
p.cond.L.Unlock()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user