mirror of
https://github.com/kennethnym/aris.git
synced 2026-03-24 19:01:17 +00:00
feat: runtime provider hotswap (#82)
Add ability to replace a FeedSourceProvider at runtime and propagate the new source to all active (and pending) user sessions, invalidating their feed caches. Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
@@ -1,29 +1,31 @@
|
||||
import type { FeedSource } from "@aelis/core"
|
||||
|
||||
import type { FeedEnhancer } from "../enhancement/enhance-feed.ts"
|
||||
import type { FeedSourceProviderInput } from "./feed-source-provider.ts"
|
||||
import type { FeedSourceProvider } from "./feed-source-provider.ts"
|
||||
|
||||
import { UserSession } from "./user-session.ts"
|
||||
|
||||
export interface UserSessionManagerConfig {
|
||||
providers: FeedSourceProviderInput[]
|
||||
providers: FeedSourceProvider[]
|
||||
feedEnhancer?: FeedEnhancer | null
|
||||
}
|
||||
|
||||
export class UserSessionManager {
|
||||
private sessions = new Map<string, UserSession>()
|
||||
private sessions = new Map<string, { userId: string; session: UserSession }>()
|
||||
private pending = new Map<string, Promise<UserSession>>()
|
||||
private readonly providers: FeedSourceProviderInput[]
|
||||
private readonly providers = new Map<string, FeedSourceProvider>()
|
||||
private readonly feedEnhancer: FeedEnhancer | null
|
||||
|
||||
constructor(config: UserSessionManagerConfig) {
|
||||
this.providers = config.providers
|
||||
for (const provider of config.providers) {
|
||||
this.providers.set(provider.sourceId, provider)
|
||||
}
|
||||
this.feedEnhancer = config.feedEnhancer ?? null
|
||||
}
|
||||
|
||||
async getOrCreate(userId: string): Promise<UserSession> {
|
||||
const existing = this.sessions.get(userId)
|
||||
if (existing) return existing
|
||||
if (existing) return existing.session
|
||||
|
||||
const inflight = this.pending.get(userId)
|
||||
if (inflight) return inflight
|
||||
@@ -38,7 +40,7 @@ export class UserSessionManager {
|
||||
session.destroy()
|
||||
throw new Error(`Session for user ${userId} was removed during creation`)
|
||||
}
|
||||
this.sessions.set(userId, session)
|
||||
this.sessions.set(userId, { userId, session })
|
||||
return session
|
||||
} finally {
|
||||
this.pending.delete(userId)
|
||||
@@ -46,20 +48,71 @@ export class UserSessionManager {
|
||||
}
|
||||
|
||||
remove(userId: string): void {
|
||||
const session = this.sessions.get(userId)
|
||||
if (session) {
|
||||
session.destroy()
|
||||
const entry = this.sessions.get(userId)
|
||||
if (entry) {
|
||||
entry.session.destroy()
|
||||
this.sessions.delete(userId)
|
||||
}
|
||||
// Cancel any in-flight creation so getOrCreate won't store the session
|
||||
this.pending.delete(userId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces a provider and updates all active sessions.
|
||||
* The new provider must have the same sourceId as an existing one.
|
||||
* For each active session, resolves a new source from the provider.
|
||||
* If the provider fails for a user, the old source is removed from that session.
|
||||
*/
|
||||
async replaceProvider(provider: FeedSourceProvider): Promise<void> {
|
||||
if (!this.providers.has(provider.sourceId)) {
|
||||
throw new Error(
|
||||
`Cannot replace provider "${provider.sourceId}": no existing provider with that sourceId`,
|
||||
)
|
||||
}
|
||||
|
||||
this.providers.set(provider.sourceId, provider)
|
||||
|
||||
const updates: Promise<void>[] = []
|
||||
|
||||
for (const [, { userId, session }] of this.sessions) {
|
||||
updates.push(this.updateSessionSource(provider, userId, session))
|
||||
}
|
||||
|
||||
// Also update sessions that are currently being created so they
|
||||
// don't land in this.sessions with a stale source.
|
||||
for (const [userId, pendingPromise] of this.pending) {
|
||||
updates.push(
|
||||
pendingPromise
|
||||
.then((session) => this.updateSessionSource(provider, userId, session))
|
||||
.catch(() => {
|
||||
// Session creation itself failed — nothing to update.
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
await Promise.all(updates)
|
||||
}
|
||||
|
||||
private async updateSessionSource(
|
||||
provider: FeedSourceProvider,
|
||||
userId: string,
|
||||
session: UserSession,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const newSource = await provider.feedSourceForUser(userId)
|
||||
session.replaceSource(provider.sourceId, newSource)
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[UserSessionManager] replaceProvider("${provider.sourceId}") failed for user ${userId}:`,
|
||||
err,
|
||||
)
|
||||
session.removeSource(provider.sourceId)
|
||||
}
|
||||
}
|
||||
|
||||
private async createSession(userId: string): Promise<UserSession> {
|
||||
const results = await Promise.allSettled(
|
||||
this.providers.map((p) =>
|
||||
typeof p === "function" ? p(userId) : p.feedSourceForUser(userId),
|
||||
),
|
||||
Array.from(this.providers.values()).map((p) => p.feedSourceForUser(userId)),
|
||||
)
|
||||
|
||||
const sources: FeedSource[] = []
|
||||
|
||||
Reference in New Issue
Block a user