-
Notifications
You must be signed in to change notification settings - Fork 0
Add a cleanup mechanism for old db entries #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
d261744
12d007a
9886d7f
481b54b
2edce18
fce4f55
89195be
6511b0a
9c9b49e
742aa3d
338636c
8852cc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,7 @@ type EndpointInfo = { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| session: RTCSessionDescriptionInit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| iceCandidates: RTCIceCandidateInit[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lastUpdate: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| type SafeEndpointInfo = Omit<EndpointInfo, 'hostKey' | 'clientKey'>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -25,8 +26,40 @@ function createRandomEndpointId(): string { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| class FlottformDatabase { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| private map = new Map<EndpointId, EndpointInfo>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| private cleanupIntervalId: NodeJS.Timeout | null = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| private cleanupPeriod = 30 * 60 * 1000; // (30 minutes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| private entryTTL = 25 * 60 * 1000; // Time-to-Live for each entry (25 minutes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| constructor() {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| constructor() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.startCleanup(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| private startCleanup() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.cleanupIntervalId = setInterval(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Loop over all entries and delete the stale ones. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const now = Date.now(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const [endpointId, endpointInfo] of this.map) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const lastUpdated = endpointInfo.lastUpdate; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (now - lastUpdated > this.entryTTL) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.map.delete(endpointId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log(`Cleaned up stale entry: ${endpointId}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, this.cleanupPeriod); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| private stopCleanup() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Clear the interval to stop cleanup | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.cleanupIntervalId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| clearInterval(this.cleanupIntervalId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.cleanupIntervalId = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Stop the cleanup when the database is no longer needed | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| destroy() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.stopCleanup(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| async createEndpoint({ session }: { session: RTCSessionDescriptionInit }): Promise<EndpointInfo> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const entry = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -35,7 +68,8 @@ class FlottformDatabase { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| hostInfo: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| iceCandidates: [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lastUpdate: Date.now() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.map.set(entry.endpointId, entry); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return entry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is exposing |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -46,6 +80,7 @@ class FlottformDatabase { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!entry) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw Error('Endpoint not found'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| entry.lastUpdate = Date.now(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { hostKey: _ignore1, clientKey: _ignore2, ...endpoint } = entry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return endpoint; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -72,7 +107,8 @@ class FlottformDatabase { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| const newInfo = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...existingSession, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| hostInfo: { ...existingSession.hostInfo, session, iceCandidates } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| hostInfo: { ...existingSession.hostInfo, session, iceCandidates }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lastUpdate: Date.now() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.map.set(endpointId, newInfo); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -105,7 +141,8 @@ class FlottformDatabase { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| const newInfo = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...existingSession, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| clientKey, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| clientInfo: { session, iceCandidates } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| clientInfo: { session, iceCandidates }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lastUpdate: Date.now() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.map.set(endpointId, newInfo); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing
this.baseApishouldn't be necessary if we usethis.baseApiin the function instead of a parameter