feat: 为执行后端增加独立容量池
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
// Package backendpool manages runtime capacity independently for each executor backend.
|
||||
package backendpool
|
||||
|
||||
import "sync"
|
||||
|
||||
type Pool struct {
|
||||
mu sync.Mutex
|
||||
capacity int
|
||||
active map[string]struct{}
|
||||
}
|
||||
|
||||
func New(capacity int) *Pool {
|
||||
return &Pool{capacity: capacity, active: make(map[string]struct{})}
|
||||
}
|
||||
|
||||
// Acquire reserves a backend slot without blocking. Redelivery of the same
|
||||
// assignment is idempotent and succeeds even while the pool is full.
|
||||
func (p *Pool) Acquire(assignmentID string) bool {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if _, exists := p.active[assignmentID]; exists {
|
||||
return true
|
||||
}
|
||||
if assignmentID == "" || len(p.active) >= p.capacity {
|
||||
return false
|
||||
}
|
||||
p.active[assignmentID] = struct{}{}
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *Pool) Restore(assignmentID string) {
|
||||
if assignmentID == "" {
|
||||
return
|
||||
}
|
||||
p.mu.Lock()
|
||||
p.active[assignmentID] = struct{}{}
|
||||
p.mu.Unlock()
|
||||
}
|
||||
|
||||
func (p *Pool) Release(assignmentID string) {
|
||||
p.mu.Lock()
|
||||
delete(p.active, assignmentID)
|
||||
p.mu.Unlock()
|
||||
}
|
||||
|
||||
func (p *Pool) Active() int {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
return len(p.active)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package backendpool
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPoolSeparatesRuntimeCapacityFromDeliveries(t *testing.T) {
|
||||
pool := New(2)
|
||||
if !pool.Acquire("one") || !pool.Acquire("two") || pool.Acquire("three") {
|
||||
t.Fatal("capacity was not enforced")
|
||||
}
|
||||
if !pool.Acquire("one") {
|
||||
t.Fatal("redelivery must be idempotent")
|
||||
}
|
||||
pool.Release("one")
|
||||
if !pool.Acquire("three") || pool.Active() != 2 {
|
||||
t.Fatalf("active=%d", pool.Active())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreMayTemporarilyExceedReducedCapacity(t *testing.T) {
|
||||
pool := New(1)
|
||||
pool.Restore("one")
|
||||
pool.Restore("two")
|
||||
if pool.Active() != 2 || pool.Acquire("three") {
|
||||
t.Fatalf("active=%d", pool.Active())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user