feat(backend): enforce user-owned sessions
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { OllamaEnsureResult, ThtRunner } from "../tht/tht-runner.js";
|
||||
import type { PrincipalContext } from "../auth/principal.js";
|
||||
|
||||
interface ReadyEntry {
|
||||
expiresAt: number;
|
||||
@@ -20,25 +21,28 @@ export class ReadinessManager {
|
||||
private now: () => number = Date.now,
|
||||
) {}
|
||||
|
||||
ensure(workspace = ""): Promise<OllamaEnsureResult> {
|
||||
const cached = this.ready.get(workspace);
|
||||
ensure(workspace = "", principal?: PrincipalContext): Promise<OllamaEnsureResult> {
|
||||
const key = `${principal?.issuer ?? ""}\0${principal?.subject ?? ""}\0${workspace}`;
|
||||
const cached = this.ready.get(key);
|
||||
if (cached && cached.expiresAt > this.now()) return Promise.resolve(cached.result);
|
||||
if (cached) this.ready.delete(workspace);
|
||||
if (cached) this.ready.delete(key);
|
||||
|
||||
const current = this.inFlight.get(workspace);
|
||||
const current = this.inFlight.get(key);
|
||||
if (current) return current;
|
||||
|
||||
const pending = this.tht.ollamaEnsure(workspace, this.timeoutSec)
|
||||
const runner = principal && typeof (this.tht as any).withPrincipal === "function"
|
||||
? this.tht.withPrincipal(principal) : this.tht;
|
||||
const pending = runner.ollamaEnsure(workspace, this.timeoutSec)
|
||||
.then((result) => {
|
||||
if (result.ok) {
|
||||
this.ready.set(workspace, { result, expiresAt: this.now() + this.ttlMs });
|
||||
this.ready.set(key, { result, expiresAt: this.now() + this.ttlMs });
|
||||
}
|
||||
return result;
|
||||
})
|
||||
.finally(() => {
|
||||
if (this.inFlight.get(workspace) === pending) this.inFlight.delete(workspace);
|
||||
if (this.inFlight.get(key) === pending) this.inFlight.delete(key);
|
||||
});
|
||||
this.inFlight.set(workspace, pending);
|
||||
this.inFlight.set(key, pending);
|
||||
return pending;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user