-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
115 additions
and
18 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {EventEmitter} from 'node:events' | ||
|
||
export interface Locker { | ||
lock(id: string): Promise<void> | ||
unlock(id: string): Promise<void> | ||
} | ||
|
||
export class MemoryLocker implements Locker { | ||
private lockEvent = new EventEmitter() | ||
private locks: Record<string, boolean> = {} | ||
|
||
isLocked(id: string) { | ||
return Boolean(this.locks[id]) | ||
} | ||
|
||
lock(id: string): Promise<void> { | ||
if (!this.isLocked(id)) { | ||
this.locks[id] = true | ||
return Promise.resolve() | ||
} | ||
|
||
return new Promise((resolve) => { | ||
this.lockEvent.once(`unlock:${id}`, () => { | ||
this.locks[id] = true | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
async unlock(id: string): Promise<void> { | ||
delete this.locks[id] | ||
this.lockEvent.emit(`unlock:${id}`) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {MemoryLocker} from '../src/models/Locker' | ||
import * as assert from 'assert' | ||
|
||
describe('MemoryLocker', () => { | ||
it('will hold the lock for subsequent calls until released', async () => { | ||
const locker = new MemoryLocker() | ||
const lockId = 'upload-id-1' | ||
|
||
const date = new Date() | ||
await locker.lock(lockId) | ||
setTimeout(() => { | ||
locker.unlock(lockId) | ||
}, 300) | ||
await locker.lock(lockId) // will wait until the other lock is released | ||
await locker.unlock(lockId) | ||
const endDate = new Date().valueOf() - date.valueOf() | ||
assert.equal(endDate >= 300, true) | ||
}) | ||
}) |