-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add a cleanup mechanism for old db entries #104
Open
nidhal-labidi
wants to merge
12
commits into
main
Choose a base branch
from
feat/add-cleanup-mechanism-for-old-db-entries
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d261744
feat: delete entries from the DB of the signaling server as soon as t…
nidhal-labidi 12d007a
feat: add automated cleanup mechanism for inactive database entries
nidhal-labidi 9886d7f
Merge branch 'origin/main' into feat/add-cleanup-mechanism-for-old-db…
nidhal-labidi 481b54b
wip: update old test to match the new error message
nidhal-labidi 2edce18
add optional parameters to the FlottformDatabase constructor
nidhal-labidi fce4f55
wip: add optional parameters for the constructor and methods of Flott…
nidhal-labidi 89195be
feat: add test for the cleanup mechanism of FlottformDatabase
nidhal-labidi 6511b0a
feat: use setTimeout instead of setInterval for the cleanup mechanism
nidhal-labidi 9c9b49e
update the properties of FlottformDatabase, update tests to use fake …
nidhal-labidi 742aa3d
update the constructor of FlottformDatabase and tests
nidhal-labidi 338636c
fix: the attribute lastUpdate is being taken care of internally only
nidhal-labidi 8852cc5
feat: add a heartbeat function in the host to keep the connection alive
nidhal-labidi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,12 @@ type EndpointInfo = { | |
session: RTCSessionDescriptionInit; | ||
iceCandidates: RTCIceCandidateInit[]; | ||
}; | ||
lastUpdate: number; | ||
}; | ||
type SafeEndpointInfo = Omit<EndpointInfo, 'hostKey' | 'clientKey'>; | ||
type SafeEndpointInfo = Omit<EndpointInfo, 'hostKey' | 'clientKey' | 'lastUpdate'>; | ||
|
||
const DEFAULT_CLEANUP_PERIOD = 30 * 60 * 1000; | ||
const DEFAULT_ENTRY_TIME_TO_LIVE_IN_MS = 25 * 60 * 1000; | ||
|
||
function createRandomHostKey(): string { | ||
return crypto.randomUUID(); | ||
|
@@ -25,8 +29,52 @@ function createRandomEndpointId(): string { | |
|
||
class FlottformDatabase { | ||
private map = new Map<EndpointId, EndpointInfo>(); | ||
private cleanupTimeoutId: NodeJS.Timeout | null = null; | ||
private cleanupPeriod: number; | ||
private entryTimeToLive: number; | ||
|
||
constructor({ | ||
cleanupPeriod = DEFAULT_CLEANUP_PERIOD, | ||
entryTimeToLive = DEFAULT_ENTRY_TIME_TO_LIVE_IN_MS | ||
}: { | ||
cleanupPeriod?: number; | ||
entryTimeToLive?: number; | ||
} = {}) { | ||
this.cleanupPeriod = cleanupPeriod; | ||
this.entryTimeToLive = entryTimeToLive; | ||
this.startCleanup(); | ||
} | ||
|
||
constructor() {} | ||
private startCleanup() { | ||
this.cleanupTimeoutId = setTimeout(this.cleanupFn.bind(this), this.cleanupPeriod); | ||
} | ||
|
||
private cleanupFn() { | ||
if (this.map && this.map.size !== 0) { | ||
const now = Date.now(); | ||
// Loop over all entries and delete the stale ones. | ||
for (const [endpointId, endpointInfo] of this.map) { | ||
const lastUpdated = endpointInfo.lastUpdate; | ||
if (now - lastUpdated > this.entryTimeToLive) { | ||
this.map.delete(endpointId); | ||
} | ||
} | ||
} | ||
this.cleanupTimeoutId = setTimeout(this.startCleanup.bind(this), this.cleanupPeriod); | ||
} | ||
|
||
private stopCleanup() { | ||
// Clear the interval to stop cleanup | ||
if (this.cleanupTimeoutId) { | ||
clearTimeout(this.cleanupTimeoutId); | ||
this.cleanupTimeoutId = 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 +83,8 @@ class FlottformDatabase { | |
hostInfo: { | ||
session, | ||
iceCandidates: [] | ||
} | ||
}, | ||
lastUpdate: Date.now() | ||
}; | ||
this.map.set(entry.endpointId, entry); | ||
return entry; | ||
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,7 +95,8 @@ class FlottformDatabase { | |
if (!entry) { | ||
throw Error('Endpoint not found'); | ||
} | ||
const { hostKey: _ignore1, clientKey: _ignore2, ...endpoint } = entry; | ||
entry.lastUpdate = Date.now(); | ||
const { hostKey: _ignore1, clientKey: _ignore2, lastUpdate: _ignore3, ...endpoint } = entry; | ||
|
||
return endpoint; | ||
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,11 +122,17 @@ class FlottformDatabase { | |
|
||
const newInfo = { | ||
...existingSession, | ||
hostInfo: { ...existingSession.hostInfo, session, iceCandidates } | ||
hostInfo: { ...existingSession.hostInfo, session, iceCandidates }, | ||
lastUpdate: Date.now() | ||
}; | ||
this.map.set(endpointId, newInfo); | ||
|
||
const { hostKey: _ignore1, clientKey: _ignore2, ...newEndpoint } = newInfo; | ||
const { | ||
hostKey: _ignore1, | ||
clientKey: _ignore2, | ||
lastUpdate: _ignore3, | ||
...newEndpoint | ||
} = newInfo; | ||
|
||
return newEndpoint; | ||
} | ||
|
@@ -105,11 +161,17 @@ class FlottformDatabase { | |
const newInfo = { | ||
...existingSession, | ||
clientKey, | ||
clientInfo: { session, iceCandidates } | ||
clientInfo: { session, iceCandidates }, | ||
lastUpdate: Date.now() | ||
}; | ||
this.map.set(endpointId, newInfo); | ||
|
||
const { hostKey: _ignore1, clientKey: _ignore2, ...newEndpoint } = newInfo; | ||
const { | ||
hostKey: _ignore1, | ||
clientKey: _ignore2, | ||
lastUpdate: _ignore3, | ||
...newEndpoint | ||
} = newInfo; | ||
|
||
return newEndpoint; | ||
} | ||
|
@@ -130,8 +192,14 @@ class FlottformDatabase { | |
} | ||
} | ||
|
||
export async function createFlottformDatabase(): Promise<FlottformDatabase> { | ||
return new FlottformDatabase(); | ||
export async function createFlottformDatabase({ | ||
cleanupPeriod = DEFAULT_CLEANUP_PERIOD, | ||
entryTimeToLive = DEFAULT_ENTRY_TIME_TO_LIVE_IN_MS | ||
}: { | ||
cleanupPeriod?: number; | ||
entryTimeToLive?: number; | ||
} = {}): Promise<FlottformDatabase> { | ||
return new FlottformDatabase({ cleanupPeriod, entryTimeToLive }); | ||
} | ||
|
||
export type { FlottformDatabase }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since
peerkey
isn't used in this error message but I don't see a change to the error itself, was/is this test not working? 🤔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.
Yes that test was not working since as you've said the string 'peerkey' is not used anymore and is replaced by this string: 'clientKey is wrong: Another peer is already connected and you cannot change this info without the correct key anymore. If you lost your key, initiate a new Flottform connection.'
--> I fixed it in this commit: 481b54b