Skip to content

Commit

Permalink
Cache the read head for the client
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Jul 24, 2023
1 parent 923aac6 commit 6068e77
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/pool-service/IrcConnectionPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const TIME_TO_WAIT_BEFORE_PONG = 10000;

// This value is reasonably conservative to avoid trimming too far.
const STREAM_HISTORY_MAXLEN = 10000;

const Config = {
redisUri: process.env.REDIS_URL ?? 'redis://localhost:6379',
metricsHost: (process.env.METRICS_HOST ?? false) as string|false,
Expand Down
10 changes: 9 additions & 1 deletion src/pool-service/IrcPoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { ClientId, ConnectionCreateArgs, HEARTBEAT_EVERY_MS,
PROTOCOL_VERSION,
READ_BUFFER_MAGIC_BYTES,
REDIS_IRC_POOL_COMMAND_IN_STREAM, REDIS_IRC_POOL_COMMAND_OUT_STREAM,
REDIS_IRC_POOL_CONNECTIONS, REDIS_IRC_POOL_HEARTBEAT_KEY, REDIS_IRC_POOL_VERSION_KEY } from "./types";
REDIS_IRC_POOL_CONNECTIONS, REDIS_IRC_POOL_HEARTBEAT_KEY, REDIS_IRC_POOL_VERSION_KEY,
REDIS_IRC_POOL_COMMAND_OUT_STREAM_LAST_READ } from "./types";

import { Logger } from 'matrix-appservice-bridge';
import { EventEmitter } from "stream";
Expand Down Expand Up @@ -46,6 +47,13 @@ export class IrcPoolClient extends (EventEmitter as unknown as new () => TypedEm
});
}

public updateLastRead(msgId: string) {
this.commandStreamId = msgId;
this.redis.set(REDIS_IRC_POOL_COMMAND_OUT_STREAM_LAST_READ, msgId).catch((ex) => {
log.error(`Failed to update last-read to ${msgId}`, ex);
})
}

public async sendCommand<T extends InCommandType>(type: T, payload: InCommandPayload[T]) {
await this.redis.xadd(REDIS_IRC_POOL_COMMAND_IN_STREAM, "*", type, JSON.stringify({
origin_ts: Date.now(),
Expand Down
3 changes: 2 additions & 1 deletion src/pool-service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const REDIS_IRC_POOL_VERSION_KEY = "ircbridge.poolversion";
export const REDIS_IRC_POOL_HEARTBEAT_KEY = "ircbridge.pool.💓";
export const REDIS_IRC_POOL_COMMAND_OUT_STREAM = "ircbridge.stream.command.out";
export const REDIS_IRC_POOL_COMMAND_IN_STREAM = "ircbridge.stream.command.in";
export const REDIS_IRC_POOL_COMMAND_IN_STREAM_LAST_READ = "ircbridge.stream.command.last-read";

export const REDIS_IRC_POOL_COMMAND_OUT_STREAM_LAST_READ = "ircbridge.stream.out.command.last-read";

export const REDIS_IRC_POOL_CONNECTIONS = "ircbridge.connections";
export const REDIS_IRC_CLIENT_STATE_KEY = `ircbridge.clientstate`; //client-id
Expand Down

0 comments on commit 6068e77

Please sign in to comment.