Skip to content

Commit b01546c

Browse files
committed
fix(mutex): remove internal state
1 parent 9403439 commit b01546c

File tree

3 files changed

+3
-72
lines changed

3 files changed

+3
-72
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redis-sp",
3-
"version": "1.0.2",
3+
"version": "1.1.2",
44
"description": "Redis synchronization primitives based on redlock algorithm",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/mutex.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,48 +42,28 @@ export default class RedisMutexImpl implements Mutex {
4242
}
4343

4444
public async lock(options?: LockOptions): Promise<void> {
45-
if (this.isLocked) {
46-
throw new Error('Already locked');
47-
}
48-
4945
await withRetries(async () => {
50-
this.isLocked = true;
51-
5246
try {
5347
await this.tryToAcquireLock();
5448
} catch {
5549
await this.tryToReleaseLock();
5650

57-
this.isLocked = false;
58-
5951
throw withRetries.RETRY_SYMBOL;
6052
}
6153
}, options?.retryOptions || this.retryOptions)();
6254
}
6355

6456
public async tryToLock(): Promise<boolean> {
65-
if (this.isLocked) {
66-
throw new Error('Already locked');
67-
}
68-
69-
this.isLocked = true;
70-
7157
try {
7258
await this.tryToAcquireLock();
7359
} catch {
74-
this.isLocked = false;
60+
return false;
7561
}
7662

77-
return this.isLocked;
63+
return true;
7864
}
7965

8066
public async unlock(): Promise<void> {
81-
if (!this.isLocked) {
82-
throw new Error('Not locked');
83-
}
84-
85-
this.isLocked = false;
86-
8767
await waitForFirstN(
8868
this.clients,
8969
async (client) => {
@@ -135,8 +115,6 @@ export default class RedisMutexImpl implements Mutex {
135115
);
136116
}
137117

138-
private isLocked = false;
139-
140118
private readonly expireAfterMillis: number;
141119
private readonly retryOptions: WithRetriesOptions;
142120
private readonly resourceId: string;

test/mutex.test.ts

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -118,53 +118,6 @@ describe('mutex negative test cases. incorrect usage', () => {
118118
jest.resetAllMocks();
119119
});
120120

121-
it('should throw an exception if you try to call `tryToLock` method on a locked mutex', async () => {
122-
const resourceId = '3dd6472f-4e4b-4559-b781-cc5d75c4f6f2';
123-
124-
const client = createRedisClientStub();
125-
126-
asMockedFunction(Lua.tryToAcquireLock).mockResolvedValueOnce('OK');
127-
128-
const mutex = new RedisMutex([client], resourceId);
129-
130-
await mutex.lock();
131-
132-
await expect(mutex.tryToLock()).rejects.toThrowError(new Error('Already locked'));
133-
134-
expect(Lua.tryToAcquireLock).toBeCalledTimes(1);
135-
expect(Lua.tryToReleaseLock).toBeCalledTimes(0);
136-
});
137-
138-
it('should throw an exception if you try to call `lock` method on a locked mutex', async () => {
139-
const resourceId = '3dd6472f-4e4b-4559-b781-cc5d75c4f6f2';
140-
141-
const client = createRedisClientStub();
142-
143-
asMockedFunction(Lua.tryToAcquireLock).mockResolvedValueOnce('OK');
144-
145-
const mutex = new RedisMutex([client], resourceId);
146-
147-
await mutex.lock();
148-
149-
await expect(mutex.lock()).rejects.toThrowError(new Error('Already locked'));
150-
151-
expect(Lua.tryToAcquireLock).toBeCalledTimes(1);
152-
expect(Lua.tryToReleaseLock).toBeCalledTimes(0);
153-
});
154-
155-
it('should throw an exception if you try to call `unlock` method on a unlocked mutex', async () => {
156-
const resourceId = '3dd6472f-4e4b-4559-b781-cc5d75c4f6f2';
157-
158-
const client = createRedisClientStub();
159-
160-
const mutex = new RedisMutex([client], resourceId);
161-
162-
await expect(mutex.unlock()).rejects.toThrowError(new Error('Not locked'));
163-
164-
expect(Lua.tryToAcquireLock).toBeCalledTimes(0);
165-
expect(Lua.tryToReleaseLock).toBeCalledTimes(0);
166-
});
167-
168121
it('should throw an exception on failed quorum', async () => {
169122
const resourceId = '3dd6472f-4e4b-4559-b781-cc5d75c4f6f2';
170123
const options = {

0 commit comments

Comments
 (0)