Skip to content

Commit

Permalink
monolith: implement load_epoch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Oct 16, 2023
1 parent 3b2712e commit 6f586e3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
20 changes: 12 additions & 8 deletions server/balancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ async function onRoomLoad(roomName: string) {
type: "loaded",
payload: {
room: obj,
load_epoch: room.loadEpoch,
},
});
gossipDebounced();
Expand All @@ -299,14 +300,17 @@ function gossip() {
type: "gossip",
payload: {
rooms: roommanager.rooms.map(room => ({
name: room.name,
title: room.title,
description: room.description,
isTemporary: room.isTemporary,
visibility: room.visibility,
queueMode: room.queueMode,
currentSource: room.currentSource,
users: room.users.length,
room: {
name: room.name,
title: room.title,
description: room.description,
isTemporary: room.isTemporary,
visibility: room.visibility,
queueMode: room.queueMode,
currentSource: room.currentSource,
users: room.users.length,
},
load_epoch: room.loadEpoch,
})),
},
});
Expand Down
27 changes: 13 additions & 14 deletions server/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,16 @@ export interface M2BKick {
reason: number;
}

export type MsgB2M =
| { type: "load", payload: B2MLoad }
| { type: "join", payload: B2MJoin }
| { type: "leave", payload: B2MLeave }
| { type: "client_msg", payload: B2MClientMsg };

export type MsgM2B =
| { type: "init", payload: M2BInit }
| { type: "loaded", payload: M2BLoaded }
| { type: "unloaded", payload: M2BUnloaded }
| { type: "gossip", payload: M2BGossip }
| { type: "room_msg", payload: M2BRoomMsg }
| { type: "kick", payload: M2BKick };

export type MsgB2M =
| { type: "load"; payload: B2MLoad }
| { type: "join"; payload: B2MJoin }
| { type: "leave"; payload: B2MLeave }
| { type: "client_msg"; payload: B2MClientMsg };

export type MsgM2B =
| { type: "init"; payload: M2BInit }
| { type: "loaded"; payload: M2BLoaded }
| { type: "unloaded"; payload: M2BUnloaded }
| { type: "gossip"; payload: M2BGossip }
| { type: "room_msg"; payload: M2BRoomMsg }
| { type: "kick"; payload: M2BKick };
1 change: 1 addition & 0 deletions server/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export class Room implements RoomState {
*/
wantSponsorBlock = false;
dontSkipSegmentsUntil: number | null = null;
loadEpoch: number = -1;

constructor(options: Partial<RoomOptions>) {
this.log = getLogger(`room/${options.name}`);
Expand Down
11 changes: 7 additions & 4 deletions server/roommanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { ClientManagerCommand } from "./clientmanager";

export const log = getLogger("roommanager");
export const rooms: Room[] = [];
const LOAD_EPOCH_KEY = "roommanager:load_epoch";

export type RoomManagerEvents = "publish" | "load" | "unload" | "command";
export type RoomManagerEventHandlers<E> = E extends "publish"
Expand All @@ -31,8 +32,10 @@ export type RoomManagerEventHandlers<E> = E extends "publish"
: never;
const bus = new EventEmitter();

function addRoom(room: Room) {
async function addRoom(room: Room) {
rooms.push(room);
const epoch = await redisClient.incr(LOAD_EPOCH_KEY);
room.loadEpoch = epoch;
bus.emit("load", room.name);
}

Expand Down Expand Up @@ -95,7 +98,7 @@ export async function createRoom(options: Partial<RoomOptions> & { name: string
}
await room.update();
await room.sync();
addRoom(room);
await addRoom(room);
log.info(`Room created: ${room.name}`);
}

Expand Down Expand Up @@ -130,7 +133,7 @@ export async function getRoom(
const state = JSON.parse(redisState) as RoomStateFromRedis;
const fixedState = redisStateToState(state);
const room = new Room(fixedState);
addRoom(room);
await addRoom(room);
return ok(room);
}

Expand All @@ -140,7 +143,7 @@ export async function getRoom(
return err(new RoomNotFoundException(roomName));
}
const room = new Room(opts);
addRoom(room);
await addRoom(room);
return ok(room);
}

Expand Down

0 comments on commit 6f586e3

Please sign in to comment.