2025-10-31 13:00:21 -06:00
|
|
|
import type Keyv from 'keyv';
|
|
|
|
|
import { isLeader } from '~/cluster';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base class for MCP registry caches that require distributed leader coordination.
|
|
|
|
|
* Provides helper methods for leader-only operations and success validation.
|
|
|
|
|
* All concrete implementations must provide their own Keyv cache instance.
|
|
|
|
|
*/
|
|
|
|
|
export abstract class BaseRegistryCache {
|
|
|
|
|
protected readonly PREFIX = 'MCP::ServersRegistry';
|
|
|
|
|
protected abstract readonly cache: Keyv;
|
2025-11-26 15:11:36 +01:00
|
|
|
protected readonly leaderOnly: boolean;
|
|
|
|
|
|
|
|
|
|
constructor(leaderOnly?: boolean) {
|
|
|
|
|
this.leaderOnly = leaderOnly ?? false;
|
|
|
|
|
}
|
2025-10-31 13:00:21 -06:00
|
|
|
|
|
|
|
|
protected async leaderCheck(action: string): Promise<void> {
|
|
|
|
|
if (!(await isLeader())) throw new Error(`Only leader can ${action}.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected successCheck(action: string, success: boolean): true {
|
|
|
|
|
if (!success) throw new Error(`Failed to ${action} in cache.`);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async reset(): Promise<void> {
|
2025-11-26 15:11:36 +01:00
|
|
|
if (this.leaderOnly) {
|
|
|
|
|
await this.leaderCheck(`reset ${this.cache.namespace} cache`);
|
|
|
|
|
}
|
2025-10-31 13:00:21 -06:00
|
|
|
await this.cache.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|