Files
summercms/modules/pier/export_test.go
Jakub Zych 2bf9dca603 fix(pier): reap stateful sessions whose store record expired
- a reaper started at Mount checks every local session against the store
  every min(idle_ttl/2, 1m) and closes each copy whose record is gone,
  logging the expired step (A-CR-01, A-IN-06)
- the GET stream re-check ends the stream when the record is gone (A-IN-01)
- tool calls served without a session on the stateful path log the
  session as ephemeral (A-IN-06)
- TestPierReaperSmoke and the ReapNow, LocalSessions, StopReaper seams
2026-10-23 15:50:04 +02:00

70 lines
2.1 KiB
Go

package pier
import (
"context"
"time"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bouncer"
"git.golem15.com/golem15/summercms/modules/cabana"
)
// Test seams (package pier, compiled into the test binary only).
// SetBackendTokens replaces the admin personal token resolver, which needs
// a database in production (cabana.ResolveBackendToken).
func (p *Pier) SetBackendTokens(fn func(ctx context.Context, app *backpack.App, raw string) (*bouncer.Principal, *cabana.BackendAPIToken, []string, error)) {
p.identities.backendTokens = fn
}
// Resolve resolves a bearer into an Identity as the endpoint does.
func (p *Pier) Resolve(ctx context.Context, raw string) (Identity, error) {
return p.identities.resolve(ctx, raw)
}
// Ceiling reports whether the read/write ceiling applies to the identity.
func (id Identity) Ceiling() bool { return id.ceiling }
// AdvanceStoreClock moves the memory session store's clock forward by d, so
// a test can expire idle sessions without waiting; a Redis store is moved
// with miniredis.FastForward instead.
func (p *Pier) AdvanceStoreClock(d time.Duration) bool {
ms, ok := p.store.(*memoryStore)
if !ok {
return false
}
ms.mu.Lock()
base := ms.now
ms.now = func() time.Time { return base().Add(d) }
ms.mu.Unlock()
return true
}
// StatelessServers counts the per-identity servers of the stateless path.
func (p *Pier) StatelessServers() int {
p.stateless.mu.Lock()
defer p.stateless.mu.Unlock()
return len(p.stateless.entries)
}
// SetRecheck sets how often a long GET stream re-verifies its bearer.
func (p *Pier) SetRecheck(d time.Duration) { p.recheck = d }
// ReapNow runs one session reaper pass and answers how many local sessions
// it closed.
func (p *Pier) ReapNow(ctx context.Context) int { return p.sessions.reapOnce(ctx) }
// LocalSessions counts the stateful sessions connected on this instance.
func (p *Pier) LocalSessions() int {
p.sessions.mu.Lock()
defer p.sessions.mu.Unlock()
return len(p.sessions.local)
}
// StopReaper ends the session reaper goroutine Mount started.
func (p *Pier) StopReaper() {
if p.stopReap != nil {
p.stopReap()
}
}