Cache rooms with active connections

This commit is contained in:
Half-Shot 2024-02-06 17:17:55 +00:00
parent 90927a71f4
commit d25f515b49
4 changed files with 43 additions and 6 deletions

View File

@ -684,12 +684,19 @@ export class Bridge {
const queue = new PQueue({
concurrency: 2,
});
// Set up already joined rooms
await queue.addAll(this.botUsersManager.joinedRooms.map((roomId) => async () => {
let allJoinedRooms = await this.storage.getAllRoomsWithActiveConnections();
if (!allJoinedRooms.length) {
allJoinedRooms = this.botUsersManager.joinedRooms;
}
log.info(`Found ${allJoinedRooms.length} active rooms`);
await queue.addAll(allJoinedRooms.map((roomId) => async () => {
log.debug("Fetching state for " + roomId);
try {
await connManager.createConnectionsForRoomId(roomId, false);
this.storage.addRoomHasActiveConnections(roomId);
} catch (ex) {
log.error(`Unable to create connection for ${roomId}`, ex);
return;
@ -735,6 +742,7 @@ export class Bridge {
}
}
const adminRoom = await this.setUpAdminRoom(botUser.intent, roomId, accountData, notifContent || NotifFilter.getDefaultContent());
this.storage.addRoomHasActiveConnections(roomId);
// Call this on startup to set the state
await this.onAdminRoomSettingsChanged(adminRoom, accountData, { admin_user: accountData.admin_user });
log.debug(`Room ${roomId} is connected to: ${adminRoom.toString()}`);
@ -1133,6 +1141,9 @@ export class Bridge {
// Empty object == redacted
if (event.content.disabled === true || Object.keys(event.content).length === 0) {
await this.connectionManager.purgeConnection(connection.roomId, connection.connectionId, false);
if (this.connectionManager.getAllConnectionsForRoom(connection.roomId).length === 0) {
this.storage.removeRoomHasActiveConnections(roomId);
}
} else {
await connection.onStateUpdate?.(event);
}
@ -1144,6 +1155,7 @@ export class Bridge {
// Is anyone interested in this state?
const connection = await this.connectionManager.createConnectionForState(roomId, new StateEvent(event), true);
if (connection) {
this.storage.addRoomHasActiveConnections(roomId);
log.info(`New connected added to ${roomId}: ${connection.toString()}`);
this.connectionManager.push(connection);
}

View File

@ -107,4 +107,14 @@ export class MemoryStorageProvider extends MSP implements IBridgeStorageProvider
public async setGitlabDiscussionThreads(connectionId: string, value: SerializedGitlabDiscussionThreads): Promise<void> {
this.gitlabDiscussionThreads.set(connectionId, value);
}
public addRoomHasActiveConnections(): void {
// no-op: only used for startup speedups
}
public removeRoomHasActiveConnections(): void {
// no-op: only used for startup speedups
}
public async getAllRoomsWithActiveConnections(): Promise<string[]> {
// no-op: only used for startup speedups
return [];
}
}

View File

@ -18,19 +18,15 @@ const GH_ISSUES_REVIEW_DATA_KEY = "gh.issues.review_data";
const FIGMA_EVENT_COMMENT_ID = "figma.comment_event_id";
const STORED_FILES_KEY = "storedfiles.";
const GL_DISCUSSIONTHREADS_KEY = "gl.discussion-threads";
const ACTIVE_ROOMS = "cache.active_rooms";
const STORED_FILES_EXPIRE_AFTER = 24 * 60 * 60; // 24 hours
const COMPLETED_TRANSACTIONS_EXPIRE_AFTER = 24 * 60 * 60; // 24 hours
const ISSUES_EXPIRE_AFTER = 7 * 24 * 60 * 60; // 7 days
const ISSUES_LAST_COMMENT_EXPIRE_AFTER = 14 * 24 * 60 * 60; // 7 days
const WIDGET_TOKENS = "widgets.tokens.";
const WIDGET_USER_TOKENS = "widgets.user-tokens.";
const FEED_GUIDS = "feeds.guids.";
const log = new Logger("RedisASProvider");
export class RedisStorageContextualProvider implements IStorageProvider {
@ -229,4 +225,20 @@ export class RedisStorageProvider extends RedisStorageContextualProvider impleme
public async hasSeenFeedGuid(url: string, guid: string): Promise<boolean> {
return (await this.redis.lpos(`${FEED_GUIDS}${url}`, guid)) != null;
}
public addRoomHasActiveConnections(roomId: string): void {
this.redis.sadd(ACTIVE_ROOMS, roomId).catch((ex) => {
log.warn(`Failed to add ${roomId} to active rooms`, ex);
});
}
public removeRoomHasActiveConnections(roomId: string): void {
this.redis.srem(ACTIVE_ROOMS, roomId).catch((ex) => {
log.warn(`Failed to remove ${roomId} from active rooms`, ex);
});
}
public getAllRoomsWithActiveConnections(): Promise<string[]> {
return this.redis.smembers(ACTIVE_ROOMS);
}
}

View File

@ -28,4 +28,7 @@ export interface IBridgeStorageProvider extends IAppserviceStorageProvider, ISto
storeFeedGuids(url: string, ...guid: string[]): Promise<void>;
hasSeenFeed(url: string, ...guid: string[]): Promise<boolean>;
hasSeenFeedGuid(url: string, guid: string): Promise<boolean>;
addRoomHasActiveConnections(roomId: string): void;
removeRoomHasActiveConnections(roomId: string): void;
getAllRoomsWithActiveConnections(): Promise<string[]>;
}