Skip to content

Commit 3a80015

Browse files
committed
refactor: minor driver refactor
1 parent e1b2bb1 commit 3a80015

7 files changed

Lines changed: 50 additions & 39 deletions

File tree

packages/verrou/src/drivers/database.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class DatabaseStore implements LockStore {
3333
}
3434

3535
/**
36-
* Create a Knex connection instance
36+
* Create or reuse a Knex connection instance
3737
*/
3838
#createConnection(config: DatabaseStoreOptions) {
3939
if (typeof config.connection === 'string') {
@@ -89,7 +89,7 @@ export class DatabaseStore implements LockStore {
8989
}
9090

9191
/**
92-
* Save a new lock
92+
* Save the lock in the store if not already locked by another owner
9393
*
9494
* We basically rely on primary key constraint to ensure the lock is
9595
* unique.
@@ -117,7 +117,8 @@ export class DatabaseStore implements LockStore {
117117
}
118118

119119
/**
120-
* Delete a lock
120+
* Delete the lock from the store if it is owned by the owner
121+
* Otherwise throws a E_LOCK_NOT_OWNED error
121122
*/
122123
async delete(key: string, owner: string): Promise<void> {
123124
const currentOwner = await this.#getCurrentOwner(key)
@@ -127,14 +128,15 @@ export class DatabaseStore implements LockStore {
127128
}
128129

129130
/**
130-
* Force delete a lock
131+
* Force delete the lock from the store. No check is made on the owner
131132
*/
132-
async forceRelease(key: string) {
133+
async forceDelete(key: string) {
133134
await this.#connection.table(this.#tableName).where('key', key).delete()
134135
}
135136

136137
/**
137-
* Extend a lock
138+
* Extend the lock expiration. Throws an error if the lock is not owned by the owner
139+
* Duration is in milliseconds
138140
*/
139141
async extend(key: string, owner: string, duration: number) {
140142
const updated = await this.#connection
@@ -147,7 +149,7 @@ export class DatabaseStore implements LockStore {
147149
}
148150

149151
/**
150-
* Check if a lock exists
152+
* Check if the lock exists
151153
*/
152154
async exists(key: string) {
153155
await this.#initialized

packages/verrou/src/drivers/dynamodb.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class DynamoDBStore implements LockStore {
6666
}
6767

6868
/**
69-
* Save a lock
69+
* Save the lock in the store if not already locked by another owner
7070
*/
7171
async save(key: string, owner: string, ttl: number | null) {
7272
await this.#initialized
@@ -96,7 +96,8 @@ export class DynamoDBStore implements LockStore {
9696
}
9797

9898
/**
99-
* Delete a lock
99+
* Delete the lock from the store if it is owned by the owner
100+
* Otherwise throws a E_LOCK_NOT_OWNED error
100101
*/
101102
async delete(key: string, owner: string) {
102103
const command = new DeleteItemCommand({
@@ -115,9 +116,9 @@ export class DynamoDBStore implements LockStore {
115116
}
116117

117118
/**
118-
* Force delete a lock
119+
* Force delete the lock from the store. No check is made on the owner
119120
*/
120-
async forceRelease(key: string) {
121+
async forceDelete(key: string) {
121122
const command = new DeleteItemCommand({
122123
TableName: this.#tableName,
123124
Key: { key: { S: key } },
@@ -127,7 +128,7 @@ export class DynamoDBStore implements LockStore {
127128
}
128129

129130
/**
130-
* Check if a lock exists
131+
* Check if the lock exists
131132
*/
132133
async exists(key: string) {
133134
await this.#initialized
@@ -142,6 +143,10 @@ export class DynamoDBStore implements LockStore {
142143
return result.Item !== undefined && !isExpired
143144
}
144145

146+
/**
147+
* Extend the lock expiration. Throws an error if the lock is not owned by the owner
148+
* Duration is in milliseconds
149+
*/
145150
async extend(key: string, owner: string, duration: number) {
146151
const command = new PutItemCommand({
147152
TableName: this.#tableName,

packages/verrou/src/drivers/memory.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ export class MemoryStore implements LockStore {
4949
}
5050

5151
/**
52-
* Extend a lock
52+
* Extend the lock expiration. Throws an error if the lock is not owned by the owner
53+
* Duration is in milliseconds
5354
*/
5455
async extend(key: string, owner: string, duration: number) {
5556
const lock = this.#locks.get(key)
@@ -59,7 +60,7 @@ export class MemoryStore implements LockStore {
5960
}
6061

6162
/**
62-
* Save a lock
63+
* Save the lock in the store if not already locked by another owner
6364
*/
6465
async save(key: string, owner: string, ttl: number | null) {
6566
try {
@@ -77,7 +78,8 @@ export class MemoryStore implements LockStore {
7778
}
7879

7980
/**
80-
* Delete a lock
81+
* Delete the lock from the store if it is owned by the owner
82+
* Otherwise throws a E_LOCK_NOT_OWNED error
8183
*/
8284
async delete(key: string, owner: string) {
8385
const mutex = this.#locks.get(key)
@@ -89,17 +91,17 @@ export class MemoryStore implements LockStore {
8991
}
9092

9193
/**
92-
* Force delete a lock
94+
* Force delete the lock from the store. No check is made on the owner
9395
*/
94-
async forceRelease(key: string) {
96+
async forceDelete(key: string) {
9597
const lock = this.#locks.get(key)
9698
if (!lock) return
9799

98100
lock.releaser?.()
99101
}
100102

101103
/**
102-
* Check if a lock exists
104+
* Check if the lock exists
103105
*/
104106
async exists(key: string) {
105107
const lock = this.#locks.get(key)

packages/verrou/src/drivers/redis.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,21 @@ export class RedisStore implements LockStore {
2525
}
2626

2727
/**
28-
* Delete a lock
28+
* Save the lock in the store if not already locked by another owner
29+
*/
30+
async save(key: string, owner: string, ttl: number | null) {
31+
if (ttl) {
32+
const result = await this.#connection.set(key, owner, 'PX', ttl, 'NX')
33+
return result === 'OK'
34+
}
35+
36+
const result = await this.#connection.setnx(key, owner)
37+
return result === 1
38+
}
39+
40+
/**
41+
* Delete the lock from the store if it is owned by the owner
42+
* Otherwise throws a E_LOCK_NOT_OWNED error
2943
*/
3044
async delete(key: string, owner: string) {
3145
const lua = `
@@ -41,35 +55,23 @@ export class RedisStore implements LockStore {
4155
}
4256

4357
/**
44-
* Force delete a lock
58+
* Force delete the lock from the store. No check is made on the owner
4559
*/
46-
async forceRelease(key: string) {
60+
async forceDelete(key: string) {
4761
await this.#connection.del(key)
4862
}
4963

5064
/**
51-
* Check if a lock exists
65+
* Check if the lock exists
5266
*/
5367
async exists(key: string): Promise<boolean> {
5468
const result = await this.#connection.get(key)
5569
return !!result
5670
}
5771

5872
/**
59-
* Save a lock
60-
*/
61-
async save(key: string, owner: string, ttl: number | null) {
62-
if (ttl) {
63-
const result = await this.#connection.set(key, owner, 'PX', ttl, 'NX')
64-
return result === 'OK'
65-
}
66-
67-
const result = await this.#connection.setnx(key, owner)
68-
return result === 1
69-
}
70-
71-
/**
72-
* Extend a lock
73+
* Extend the lock expiration. Throws an error if the lock is not owned by the owner
74+
* Duration is in milliseconds
7375
*/
7476
async extend(key: string, owner: string, duration: number) {
7577
const lua = `

packages/verrou/src/lock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class Lock {
111111
* Force release the lock
112112
*/
113113
async forceRelease() {
114-
await this.#lockStore.forceRelease(this.#key)
114+
await this.#lockStore.forceDelete(this.#key)
115115
}
116116

117117
/**

packages/verrou/src/types/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export interface LockStore {
8383
/**
8484
* Force delete the lock from the store. No check is made on the owner
8585
*/
86-
forceRelease(key: string): Promise<void>
86+
forceDelete(key: string): Promise<void>
8787

8888
/**
8989
* Check if the lock exists

packages/verrou/test_helpers/null_store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class NullStore implements LockStore {
1313
return true
1414
}
1515

16-
async forceRelease(_key: string): Promise<void> {
16+
async forceDelete(_key: string): Promise<void> {
1717
return
1818
}
1919

0 commit comments

Comments
 (0)