fix(pier): reserve session slots atomically and build servers outside router locks

- an unexported reserver (Reserve, Release) next to updater: the memory
  store reserves under its mutex, the Redis store in one Lua script
  (prune, SCARD, SET PX plus SADD); serveNew reserves instead of Count
  then Put and releases on a failed connect or initialize (A-WR-03)
- serveNew, lookup's rehydration and the stateless getServer build the
  SDK server with no router-wide lock held and insert or adopt under the
  lock afterwards, so a slow guard or database check no longer stalls
  the instance (A-WR-07)
This commit is contained in:
Jakub Zych
2026-10-23 15:56:57 +02:00
parent 2bf9dca603
commit c1c0c77ddc
5 changed files with 223 additions and 36 deletions

View File

@@ -98,6 +98,32 @@ func (m *memoryStore) Count(_ context.Context, binding string) (int, error) {
return n, nil
}
// Reserve counts the binding's live records and writes rec under one lock.
func (m *memoryStore) Reserve(_ context.Context, binding, id string, rec Record, max int, ttl time.Duration) (bool, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.sweepLocked()
n := 0
for _, r := range m.records {
if r.rec.Binding == binding {
n++
}
}
if n >= max {
return false, nil
}
m.records[id] = memoryRecord{rec: rec, expires: m.now().Add(ttl)}
return true, nil
}
// Release drops a reservation.
func (m *memoryStore) Release(_ context.Context, _ string, id string) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.records, id)
return nil
}
func (m *memoryStore) Publish(_ context.Context, ev Event) error {
m.mu.Lock()
subs := append([]func(Event){}, m.subs...)