You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Accept an optional object with the following properties:
23
27
24
28
-`retry`: An object with the following properties:
25
-
-`timeout`: The maximum time to wait for the lock to be acquired. If the timeout is reached, an `E_LOCK_TIMEOUT` error will be thrown. Defaults to `Infinity`.
29
+
-`timeout`: The maximum time to wait for the lock to be acquired. Defaults to `Infinity`.
26
30
-`delay`: The delay in miliseconds between each retry. Defaults to `250`
27
31
-`attempts`: The maximum number of attempts to acquire the lock.
28
32
33
+
`acquire` will return a boolean indicating if the lock was acquired or not
34
+
35
+
### `acquireImmediately`
36
+
37
+
Try to acquire the lock immediately ( without retrying ). If the lock is already acquired, it will return `false` immediately.
38
+
39
+
```ts
40
+
import { errors } from'@verrou/core'
41
+
42
+
const lock =verrou.createLock('key')
43
+
const acquired =awaitlock.acquireImmediately()
44
+
if (acquired) {
45
+
awaitcriticalSection()
46
+
}
47
+
```
48
+
29
49
### `release`
30
50
31
51
Release the lock. Note that only the lock owner can release the lock.
@@ -39,45 +59,31 @@ await lock.release()
39
59
40
60
### `run`
41
61
42
-
Acquire the lock, run the callback, and release the lock.
62
+
Acquire the lock, run the callback, and release the lock. The method will return a tuple with the first value being a boolean indicating if the lock was acquired or not, and the second value being the result of the callback.
43
63
44
64
```ts
45
65
const lock =verrou.createLock('key')
46
-
awaitlock.run(() => {
66
+
const [executed, result] =awaitlock.run(() => {
47
67
// do something
68
+
return'result'
48
69
})
49
70
```
50
71
72
+
51
73
### `runImmediately`
52
74
53
-
Same as `run`, but try to acquire the lock immediately ( without retrying ). If the lock is already acquired, it will throws a `E_LOCK_ALREADY_ACQUIRED` error.
75
+
Same as `run`, but try to acquire the lock immediately ( without retrying ).
But we are still missing error handling. What if my `doSomething` method throws an error? The lock will never be released. To prevent this, always make sure to wrap your code with a try/catch/finaly block.
@@ -53,10 +53,10 @@ But we are still missing error handling. What if my `doSomething` method throws
In general, you will either use the `retry.attempts` or `retry.timeout` options.
117
117
118
-
### Handling errors
118
+
### Handling lock acquisition failure
119
119
120
-
If ever you can't acquire a lock, an error will be thrown. You can catch it and handle it like this :
120
+
If ever you can't acquire a lock, `acquire` and `acquireImmediately`will return `false`. You can check if the lock was acquired by checking this value.
121
121
122
122
```ts
123
123
import { errors } from'@verrou/core'
124
124
125
-
try {
126
-
awaitlock.acquire()
127
-
} catch (error) {
128
-
if (errorinstanceoferrors.E_LOCK_TIMEOUT) {
129
-
// Handle the error
130
-
}
125
+
const acquired =awaitlock.acquire()
126
+
if (!acquired) {
127
+
return'Lock not acquired'
131
128
}
129
+
```
132
130
133
-
awaitlock.run(async () => {
134
-
// Do your critical code here
135
-
doSomething()
136
-
}).catch(error=> {
137
-
if (errorinstanceoferrors.E_LOCK_TIMEOUT) {
138
-
// Handle the error
139
-
}
131
+
`run` and `runImmediately` methods will return a tuple with the first value being a boolean indicating if the lock was acquired or not.
0 commit comments