Skip to content

Commit e1b2bb1

Browse files
committed
refactor: move to E_LOCK_NOT_OWNED
1 parent 0669a29 commit e1b2bb1

7 files changed

Lines changed: 26 additions & 41 deletions

File tree

packages/verrou/bin/test.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,10 @@ import { processCLIArgs, configure, run } from '@japa/runner'
77

88
import { BASE_URL } from '../test_helpers/index.js'
99

10-
/*
11-
|--------------------------------------------------------------------------
12-
| Configure tests
13-
|--------------------------------------------------------------------------
14-
|
15-
| The configure method accepts the configuration to configure the Japa
16-
| tests runner.
17-
|
18-
| The first method call "processCLIArgs" process the command line arguments
19-
| and turns them into a config object. Using this method is not mandatory.
20-
|
21-
| Please consult japa.dev/runner-config for the config docs.
22-
*/
2310
processCLIArgs(process.argv.slice(2))
2411
configure({
2512
files: ['tests/**/*.spec.ts'],
2613
plugins: [assert(), expectTypeOf(), fileSystem({ autoClean: true, basePath: BASE_URL })],
2714
})
2815

29-
/*
30-
|--------------------------------------------------------------------------
31-
| Run tests
32-
|--------------------------------------------------------------------------
33-
|
34-
| The following "run" method is required to execute all the tests.
35-
|
36-
*/
3716
run()

packages/verrou/src/drivers/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import knex, { type Knex } from 'knex'
22

3-
import { E_LOCK_NOT_OWNED, E_RELEASE_NOT_OWNED } from '../errors.js'
3+
import { E_LOCK_NOT_OWNED } from '../errors.js'
44
import type { DatabaseStoreOptions, LockStore } from '../types/main.js'
55

66
/**
@@ -121,7 +121,7 @@ export class DatabaseStore implements LockStore {
121121
*/
122122
async delete(key: string, owner: string): Promise<void> {
123123
const currentOwner = await this.#getCurrentOwner(key)
124-
if (currentOwner !== owner) throw new E_RELEASE_NOT_OWNED()
124+
if (currentOwner !== owner) throw new E_LOCK_NOT_OWNED()
125125

126126
await this.#connection.table(this.#tableName).where('key', key).where('owner', owner).delete()
127127
}

packages/verrou/src/drivers/dynamodb.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
ResourceInUseException,
99
} from '@aws-sdk/client-dynamodb'
1010

11+
import { E_LOCK_NOT_OWNED } from '../errors.js'
1112
import type { LockStore, DynamoDbOptions } from '../types/main.js'
12-
import { E_LOCK_NOT_OWNED, E_RELEASE_NOT_OWNED } from '../errors.js'
1313

1414
/**
1515
* Create a DynamoDB store.
@@ -110,7 +110,7 @@ export class DynamoDBStore implements LockStore {
110110
try {
111111
await this.#client.send(command)
112112
} catch (err) {
113-
throw new E_RELEASE_NOT_OWNED()
113+
throw new E_LOCK_NOT_OWNED()
114114
}
115115
}
116116

packages/verrou/src/drivers/memory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Mutex, tryAcquire } from 'async-mutex'
22
import type { MutexInterface } from 'async-mutex'
33

4+
import { E_LOCK_NOT_OWNED } from '../errors.js'
45
import type { LockStore } from '../types/main.js'
5-
import { E_LOCK_NOT_OWNED, E_RELEASE_NOT_OWNED } from '../errors.js'
66

77
type MemoryLockEntry = {
88
mutex: MutexInterface
@@ -82,8 +82,8 @@ export class MemoryStore implements LockStore {
8282
async delete(key: string, owner: string) {
8383
const mutex = this.#locks.get(key)
8484

85-
if (!mutex || !mutex.releaser) throw new E_RELEASE_NOT_OWNED()
86-
if (mutex.owner !== owner) throw new E_RELEASE_NOT_OWNED()
85+
if (!mutex || !mutex.releaser) throw new E_LOCK_NOT_OWNED()
86+
if (mutex.owner !== owner) throw new E_LOCK_NOT_OWNED()
8787

8888
mutex.releaser()
8989
}

packages/verrou/src/drivers/redis.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Redis as IoRedis } from 'ioredis'
22

3-
import { E_LOCK_NOT_OWNED, E_RELEASE_NOT_OWNED } from '../errors.js'
3+
import { E_LOCK_NOT_OWNED } from '../errors.js'
44
import type { LockStore, RedisStoreOptions } from '../types/main.js'
55

66
/**
@@ -37,7 +37,7 @@ export class RedisStore implements LockStore {
3737
`
3838

3939
const result = await this.#connection.eval(lua, 1, key, owner)
40-
if (result === 0) throw new E_RELEASE_NOT_OWNED()
40+
if (result === 0) throw new E_LOCK_NOT_OWNED()
4141
}
4242

4343
/**

packages/verrou/src/errors.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { createError } from '@poppinss/utils'
22

3-
export const E_RELEASE_NOT_OWNED = createError(
4-
`It looks like you are trying to release a lock that is not acquired by you. Make sure to acquire the lock before trying to release it`,
5-
'E_RELEASE_NOT_OWNED',
6-
)
7-
83
export const E_LOCK_TIMEOUT = createError(
94
`Lock was not acquired in the allotted time`,
105
'E_LOCK_TIMEOUT',

packages/verrou/src/types/main.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,27 @@ export interface LockFactoryConfig {
6161

6262
export interface LockStore {
6363
/**
64-
* Save the lock in the store if not already locked
64+
* Save the lock in the store if not already locked by another owner
65+
*
66+
* @param key The key to lock
67+
* @param owner The owner of the lock
68+
* @param ttl The time to live of the lock in milliseconds. Null means no expiration
69+
*
70+
* @returns True if the lock was acquired, false otherwise
6571
*/
66-
save(key: string, owner: string, ttl: number | null | undefined): Promise<boolean>
72+
save(key: string, owner: string, ttl: number | null): Promise<boolean>
6773

6874
/**
69-
* Delete the lock from the store
75+
* Delete the lock from the store if it is owned by the owner
76+
* Otherwise throws a E_LOCK_NOT_OWNED error
77+
*
78+
* @param key The key to delete
79+
* @param owner The owner of the lock
7080
*/
7181
delete(key: string, owner: string): Promise<void>
7282

7383
/**
74-
* Force delete the lock from the store
84+
* Force delete the lock from the store. No check is made on the owner
7585
*/
7686
forceRelease(key: string): Promise<void>
7787

@@ -81,12 +91,13 @@ export interface LockStore {
8191
exists(key: string): Promise<boolean>
8292

8393
/**
84-
* Extend the lock expiration
94+
* Extend the lock expiration. Throws an error if the lock is not owned by the owner
95+
* Duration is in milliseconds
8596
*/
8697
extend(key: string, owner: string, duration: number): Promise<void>
8798

8899
/**
89-
* Disconnect the store
100+
* Disconnect the store from the underlying storage ( when applicable )
90101
*/
91102
disconnect(): Promise<void>
92103
}

0 commit comments

Comments
 (0)