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:
@@ -82,17 +82,22 @@ func (sp *statelessPath) getServer(r *http.Request) *mcp.Server {
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
sp.mu.Lock()
|
||||
defer sp.mu.Unlock()
|
||||
if e, ok := sp.entries[id.Binding]; ok {
|
||||
sp.order.MoveToFront(e.elem)
|
||||
e.mu.Lock()
|
||||
e.identity = id
|
||||
e.mu.Unlock()
|
||||
if e := sp.hit(id.Binding); e != nil {
|
||||
sp.refresh(e, id)
|
||||
return e.server
|
||||
}
|
||||
// Built with no lock held: the guard checks may query a database
|
||||
// (A-WR-07). A concurrent request of the same binding may have
|
||||
// inserted an entry meanwhile; that one is adopted.
|
||||
srv, tools, prompts := sp.p.newServer(r.Context(), id, nil)
|
||||
e := &statelessEntry{binding: id.Binding, server: srv, identity: id, tools: tools, prompts: prompts}
|
||||
sp.mu.Lock()
|
||||
if cur, ok := sp.entries[id.Binding]; ok {
|
||||
sp.order.MoveToFront(cur.elem)
|
||||
sp.mu.Unlock()
|
||||
sp.refresh(cur, id)
|
||||
return cur.server
|
||||
}
|
||||
e.elem = sp.order.PushFront(e)
|
||||
sp.entries[id.Binding] = e
|
||||
for sp.order.Len() > sp.capacity {
|
||||
@@ -104,9 +109,29 @@ func (sp *statelessPath) getServer(r *http.Request) *mcp.Server {
|
||||
sp.order.Remove(last)
|
||||
delete(sp.entries, old.binding)
|
||||
}
|
||||
sp.mu.Unlock()
|
||||
return srv
|
||||
}
|
||||
|
||||
// hit answers the entry of a binding, most recently used now, or nil.
|
||||
func (sp *statelessPath) hit(binding string) *statelessEntry {
|
||||
sp.mu.Lock()
|
||||
defer sp.mu.Unlock()
|
||||
e, ok := sp.entries[binding]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
sp.order.MoveToFront(e.elem)
|
||||
return e
|
||||
}
|
||||
|
||||
// refresh stores the request's identity on an entry.
|
||||
func (sp *statelessPath) refresh(e *statelessEntry, id Identity) {
|
||||
e.mu.Lock()
|
||||
e.identity = id
|
||||
e.mu.Unlock()
|
||||
}
|
||||
|
||||
// matching lists the entries whose identity includes the subject.
|
||||
func (sp *statelessPath) matching(guard string, userID uint) []*statelessEntry {
|
||||
sp.mu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user