Skip to content

Commit f73d5e8

Browse files
committed
docs: add api docs
1 parent 0d9b80e commit f73d5e8

2 files changed

Lines changed: 197 additions & 0 deletions

File tree

docs/content/docs/api.md

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
---
2+
summary: Verrou Locks API
3+
---
4+
5+
# API
6+
7+
Below is the API documentation for Verrou.
8+
9+
## Lock API
10+
11+
### `acquire`
12+
13+
Acquire the lock. If the lock is already acquired, it will wait until it is released or the timeout is reached.
14+
15+
```ts
16+
const lock = verrou.createLock('key', '10s')
17+
await lock.acquire()
18+
await lock.acquire({ retry: { timeout: 1000 } })
19+
```
20+
21+
Accept an optional object with the following properties:
22+
23+
- `retry`: An object with the following properties:
24+
- `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`.
25+
- `delay`: The delay in miliseconds between each retry. Defaults to `250`
26+
- `attempts`: The maximum number of attempts to acquire the lock.
27+
28+
### `release`
29+
30+
Release the lock. Note that only the lock owner can release the lock.
31+
32+
```ts
33+
const lock = verrou.createLock('key')
34+
35+
await lock.acquire()
36+
await lock.release()
37+
```
38+
39+
### `run`
40+
41+
Acquire the lock, run the callback, and release the lock.
42+
43+
```ts
44+
const lock = verrou.createLock('key')
45+
await lock.run({ retry: { timeout: '1000ms' } }, async () => {
46+
// do something
47+
})
48+
```
49+
50+
Accept an optional object with the same properties as `acquire`.
51+
52+
### `isLocked`
53+
54+
Check if the lock is acquired.
55+
56+
```ts
57+
const lock = verrou.createLock('key')
58+
const isLocked = await lock.isLocked()
59+
```
60+
61+
### `getOwner`
62+
63+
Get the owner name of the lock. By default, the owner name is a randomly generated string.
64+
65+
Getting the owner is useful for [sharing the lock between multiple processes](./usage.md#sharing-a-lock-between-multiple-processes)
66+
67+
```ts
68+
const lock = verrou.createLock('key')
69+
const owner = await lock.getOwner()
70+
```
71+
72+
### `isExpired`
73+
74+
Check if the lock is expired.
75+
76+
```ts
77+
const lock = verrou.createLock('key', '10s')
78+
const isExpired = await lock.isExpired()
79+
```
80+
81+
### `extend`
82+
83+
Extend the lock expiration time.
84+
85+
```ts
86+
const lock = verrou.createLock('key', '10s')
87+
await lock.acquire()
88+
89+
// extend the lock expiration time by 10 seconds
90+
// since this is the default expiration time of the lock
91+
await lock.extend()
92+
// extend the lock expiration time by 20 seconds
93+
await lock.extend('20s')
94+
```
95+
96+
Argument is optional and defaults to the lock expiration time.
97+
98+
### `getRemainingTime`
99+
100+
Get the remaining time before the lock expires.
101+
102+
```ts
103+
const lock = verrou.createLock('key', '10s')
104+
await lock.acquire()
105+
106+
const remainingTime = await lock.getRemainingTime()
107+
```
108+
109+
### `forceRelease`
110+
111+
Force the lock to be released, no matter the owner of the lock.
112+
113+
```ts
114+
const lock = verrou.createLock('key')
115+
await lock.forceRelease()
116+
```
117+
118+
## LockFactory API
119+
120+
### `createLock`
121+
122+
Create a lock.
123+
124+
```ts
125+
const lock = verrou.createLock('key')
126+
const lock = verrou.createLock('key', '5m')
127+
const lock = verrou.createLock('key', 30_000)
128+
```
129+
130+
First argument is the lock key. Second argument is optional and is the lock expiration time. By default, the lock expiration time is `30s`.
131+
132+
### `restoreLock`
133+
134+
Restore a lock with a given owner. Useful when sharing a lock between multiple processes. See [Sharing a lock between multiple processes](./usage.md#sharing-a-lock-between-multiple-processes) for more details.
135+
136+
```ts
137+
const lock = verrou.restoreLock('key', 'owner')
138+
```
139+
140+
### `disconnect`
141+
142+
Disconnect from the store if applicable. With a Redis Store, it will close the `ioredis` connection.
143+
144+
```ts
145+
await verrou.disconnect()
146+
```
147+
148+
## Verrou API
149+
150+
Verrou API is a wrapper around the LockFactory API.
151+
152+
```ts
153+
const verrou = new Verrou({
154+
default: 'myRedisStore',
155+
stores: {
156+
myMemoryStore: { driver: memoryStore() },
157+
myRedisStore: { driver: redisStore() },
158+
mySecondMemoryStore: { driver: memoryStore() },
159+
myPostgresStore: { driver: postgresStore() },
160+
},
161+
})
162+
```
163+
164+
Every method available on the `LockFactory` class is available on the `Verrou` class, and will be called on the default store.
165+
166+
If you need to use a different store, you can use the `use` method.
167+
168+
```ts
169+
// use the myRedisStore store, since it's the defined default store
170+
verrou.createLock('key')
171+
// use a store that is not the default store
172+
verrou.use('myMemoryStore').createLock('key')
173+
verrou.use('mySecondMemoryStore').createLock('key')
174+
```
175+
176+
### `use`
177+
178+
As explained above, the `use` method allows you to use a different store than the default store.
179+
180+
```ts
181+
verrou.use('myMemoryStore').createLock('key')
182+
```
183+
184+
### `disconnectAll`
185+
186+
Disconnect from all stores.
187+
188+
```ts
189+
await verrou.disconnectAll()
190+
```
191+

docs/content/docs/db.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
"contentPath": "./drivers.md",
2424
"category": "Guides"
2525
},
26+
{
27+
"permalink": "api",
28+
"title": "API",
29+
"contentPath": "./api.md",
30+
"category": "Guides"
31+
},
2632
{
2733
"permalink": "logging",
2834
"title": "Logging",

0 commit comments

Comments
 (0)