mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2025-04-13 09:44:40 +00:00
avoid leaking cached notes in WS connection
This commit is contained in:
parent
045ff5d2c0
commit
14a7309cfb
1 changed files with 11 additions and 11 deletions
|
@ -24,6 +24,7 @@ import type Channel from './channel.js';
|
||||||
|
|
||||||
const MAX_CHANNELS_PER_CONNECTION = 32;
|
const MAX_CHANNELS_PER_CONNECTION = 32;
|
||||||
const MAX_SUBSCRIPTIONS_PER_CONNECTION = 256;
|
const MAX_SUBSCRIPTIONS_PER_CONNECTION = 256;
|
||||||
|
const MAX_CACHED_NOTES_PER_CONNECTION = 32;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main stream connection
|
* Main stream connection
|
||||||
|
@ -36,7 +37,7 @@ export default class Connection {
|
||||||
public subscriber: StreamEventEmitter;
|
public subscriber: StreamEventEmitter;
|
||||||
private channels: Channel[] = [];
|
private channels: Channel[] = [];
|
||||||
private subscribingNotes = new Map<string, number>();
|
private subscribingNotes = new Map<string, number>();
|
||||||
private cachedNotes: Packed<'Note'>[] = [];
|
private cachedNotes = new Map<string, Packed<'Note'>>();
|
||||||
public userProfile: MiUserProfile | null = null;
|
public userProfile: MiUserProfile | null = null;
|
||||||
public following: Record<string, Pick<MiFollowing, 'withReplies'> | undefined> = {};
|
public following: Record<string, Pick<MiFollowing, 'withReplies'> | undefined> = {};
|
||||||
public followingChannels: Set<string> = new Set();
|
public followingChannels: Set<string> = new Set();
|
||||||
|
@ -158,15 +159,13 @@ export default class Connection {
|
||||||
@bindThis
|
@bindThis
|
||||||
public cacheNote(note: Packed<'Note'>) {
|
public cacheNote(note: Packed<'Note'>) {
|
||||||
const add = (note: Packed<'Note'>) => {
|
const add = (note: Packed<'Note'>) => {
|
||||||
const existIndex = this.cachedNotes.findIndex(n => n.id === note.id);
|
this.cachedNotes.set(note.id, note);
|
||||||
if (existIndex > -1) {
|
|
||||||
this.cachedNotes[existIndex] = note;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.cachedNotes.unshift(note);
|
while (this.cachedNotes.size > MAX_CACHED_NOTES_PER_CONNECTION) {
|
||||||
if (this.cachedNotes.length > 32) {
|
// Map maintains insertion order, so first key is always the oldest
|
||||||
this.cachedNotes.splice(32);
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
|
const oldestKey = this.cachedNotes.keys().next().value!;
|
||||||
|
this.cachedNotes.delete(oldestKey);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -178,9 +177,9 @@ export default class Connection {
|
||||||
@bindThis
|
@bindThis
|
||||||
private readNote(body: JsonValue | undefined) {
|
private readNote(body: JsonValue | undefined) {
|
||||||
if (!isJsonObject(body)) return;
|
if (!isJsonObject(body)) return;
|
||||||
const id = body.id;
|
const id = body.id as string;
|
||||||
|
|
||||||
const note = this.cachedNotes.find(n => n.id === id);
|
const note = this.cachedNotes.get(id);
|
||||||
if (note == null) return;
|
if (note == null) return;
|
||||||
|
|
||||||
if (this.user && (note.userId !== this.user.id)) {
|
if (this.user && (note.userId !== this.user.id)) {
|
||||||
|
@ -378,5 +377,6 @@ export default class Connection {
|
||||||
this.fetchIntervalId = null;
|
this.fetchIntervalId = null;
|
||||||
this.channels = [];
|
this.channels = [];
|
||||||
this.subscribingNotes.clear();
|
this.subscribingNotes.clear();
|
||||||
|
this.cachedNotes.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue