-
Notifications
You must be signed in to change notification settings - Fork 20
chore: add lock method to LockRepository for managing room locks with disposal functionality #238
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
Changes from all commits
469b6f6
8cc48ee
fbbb4a3
8566f15
0488ae5
dfd8335
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 |
|---|---|---|
|
|
@@ -14,6 +14,32 @@ export class LockRepository { | |
| this.collection.createIndex({ roomId: 1 }, { unique: true }); | ||
| } | ||
|
|
||
| async lock( | ||
| roomId: string, | ||
| instanceId: string, | ||
| ): Promise<{ | ||
| success: boolean; | ||
| update: () => Promise<void>; | ||
| [Symbol.asyncDispose]: () => Promise<void>; | ||
| }> { | ||
| const lock = await this.getLock(roomId, instanceId); | ||
| return { | ||
| success: lock, | ||
| update: async () => { | ||
| if (!lock) { | ||
| return; | ||
| } | ||
| return this.updateLockTimestamp(roomId, instanceId); | ||
| }, | ||
| [Symbol.asyncDispose]: async () => { | ||
| if (!lock) { | ||
| return; | ||
| } | ||
| return this.releaseLock(roomId, instanceId); | ||
| }, | ||
|
Comment on lines
+17
to
+39
Contributor
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. Make If one processing step runs longer than the 2-minute timeout, another instance can take the room. Minimal API shape async lock(
roomId: string,
instanceId: string,
): Promise<{
success: boolean;
- update: () => Promise<void>;
+ update: () => Promise<boolean>;
[Symbol.asyncDispose]: () => Promise<void>;
}> {
const lock = await this.getLock(roomId, instanceId);
return {
success: lock,
update: async () => {
if (!lock) {
- return;
+ return false;
}
return this.updateLockTimestamp(roomId, instanceId);
},async updateLockTimestamp(roomId: string, instanceId: string): Promise<boolean> {
const { matchedCount } = await this.collection.updateOne(
{ roomId, instanceId },
{ $set: { lockedAt: new Date() } },
);
return matchedCount === 1;
}🤖 Prompt for AI Agents |
||
| }; | ||
| } | ||
|
|
||
| async getLock(roomId: string, instanceId: string): Promise<boolean> { | ||
| const timedout = new Date(); | ||
| timedout.setTime(timedout.getTime() - 2 * 60 * 1000); // 2 minutes ago | ||
|
|
||
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.
Handle
lock.update()failure to abort processing.Once
lock.update()is fixed to returnboolean(per the comment onlock.repository.ts), this loop should check the return value and break if the lease was lost:for await (const _ of this.handler(roomId)) { - await lock.update(); + const stillHeld = await lock.update(); + if (!stillHeld) { + // Lost the lock — abort and let another instance continue + break; + } }Otherwise, processing continues on a room that another instance now owns, risking duplicate/conflicting work.
🤖 Prompt for AI Agents