correct throttle counting

This commit is contained in:
Sergey Cherepanov 2023-06-23 17:39:13 +02:00
parent 49c3178f65
commit e929d5431d
No known key found for this signature in database
GPG Key ID: 87F8EDE8FBDF637C

View File

@ -82,6 +82,7 @@ type peer struct {
inactive []*subConn inactive []*subConn
active map[*subConn]struct{} active map[*subConn]struct{}
subConnRelease chan drpc.Conn subConnRelease chan drpc.Conn
openingWaitCount atomic.Int32
incomingCount atomic.Int32 incomingCount atomic.Int32
acceptCtx context.Context acceptCtx context.Context
@ -103,9 +104,11 @@ func (p *peer) Id() string {
func (p *peer) AcquireDrpcConn(ctx context.Context) (drpc.Conn, error) { func (p *peer) AcquireDrpcConn(ctx context.Context) (drpc.Conn, error) {
p.mu.Lock() p.mu.Lock()
if len(p.inactive) == 0 { if len(p.inactive) == 0 {
wait := p.limiter.wait(len(p.active)) wait := p.limiter.wait(len(p.active) + int(p.openingWaitCount.Load()))
p.mu.Unlock() p.mu.Unlock()
if wait != nil { if wait != nil {
p.openingWaitCount.Add(1)
defer p.openingWaitCount.Add(-1)
// throttle new connection opening // throttle new connection opening
select { select {
case <-ctx.Done(): case <-ctx.Done():