Skip to content

Commit 35f712d

Browse files
committed
update utilities tone
1 parent f0838d6 commit 35f712d

File tree

4 files changed

+57
-190
lines changed

4 files changed

+57
-190
lines changed

apps/website/docs/guide/14-useful-utilities/01-rate-imiter.mdx

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
---
22
title: Rate Limiter
3-
description:
4-
Control request frequency with configurable limits and intervals
53
---
64

7-
# Rate Limiter
5+
CommandKit provides a rate limiter utility that controls the frequency
6+
of operations such as user actions or API requests. This helps prevent
7+
system overload by enforcing configurable request limits.
88

9-
Think of a rate limiter like a traffic light for your application. It
10-
controls how often something can happen - like how many times a user
11-
can click a button or make an API request. This prevents your system
12-
from being overwhelmed.
13-
14-
## Basic Usage
9+
## Basic usage
1510

1611
The simplest way to use rate limiting is with the default settings:
1712

@@ -29,10 +24,9 @@ if (allowed) {
2924
}
3025
```
3126

32-
## Custom Configuration
27+
## Custom configuration
3328

34-
Sometimes you need different limits for different situations. You can
35-
create a custom rate limiter with specific settings:
29+
You can create a custom rate limiter with specific settings:
3630

3731
```typescript
3832
import { createRateLimiter } from 'commandkit/ratelimit';
@@ -46,9 +40,9 @@ const apiLimiter = createRateLimiter({
4640
const allowed = await apiLimiter.limit('api:endpoint');
4741
```
4842

49-
## Advanced Usage
43+
## Advanced usage
5044

51-
### Checking Remaining Requests
45+
### Checking remaining requests
5246

5347
You can check how many requests a user has left and when the limit
5448
resets:
@@ -68,10 +62,9 @@ console.log(
6862
);
6963
```
7064

71-
### Manual Reset
65+
### Manual reset
7266

73-
In some cases, you might want to reset a user's rate limit (like after
74-
they upgrade their account):
67+
You can manually reset a user's rate limit when needed:
7568

7669
```typescript
7770
import { resetRateLimit } from 'commandkit/ratelimit';
@@ -80,11 +73,11 @@ import { resetRateLimit } from 'commandkit/ratelimit';
8073
await resetRateLimit('user:123');
8174
```
8275

83-
### Using External Storage
76+
### Using external storage
8477

85-
By default, rate limiters store data in memory. If you're running
86-
multiple servers, you'll want to use external storage like Redis so
87-
all servers can share the same rate limit information:
78+
Rate limiters store data in memory by default. For multi-server
79+
deployments, use external storage like Redis to share rate limit data
80+
across all servers:
8881

8982
```typescript
9083
import { RateLimiter, RateLimitStorage } from 'commandkit/ratelimit';
@@ -115,30 +108,8 @@ const limiter = createRateLimiter({
115108
});
116109
```
117110

118-
## Default Settings
111+
## Default settings
119112

120113
- **Max Requests**: 10 requests
121114
- **Time Window**: 60 seconds (1 minute)
122115
- **Storage**: In-memory (works for single-server applications)
123-
124-
## Common Use Cases
125-
126-
- **API Rate Limiting**: Prevent users from making too many API calls
127-
- **User Action Throttling**: Limit how often users can click buttons
128-
or submit forms
129-
- **Resource Access Control**: Control access to expensive operations
130-
- **Spam Prevention**: Stop automated bots from overwhelming your
131-
system
132-
133-
## Tips for Beginners
134-
135-
1. **Start Simple**: Use the default `ratelimit()` function for basic
136-
needs
137-
2. **Choose Good Keys**: Use descriptive keys like `user:123` or
138-
`api:endpoint` to make debugging easier
139-
3. **Set Reasonable Limits**: Don't make limits too strict or too
140-
loose - find the right balance
141-
4. **Handle Rejection**: Always check if the rate limit allows the
142-
action before proceeding
143-
5. **Consider Your Users**: Think about legitimate use cases when
144-
setting limits

apps/website/docs/guide/14-useful-utilities/02-mutex.mdx

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
---
22
title: Mutex
3-
description:
4-
Ensure exclusive access to shared resources with async mutex locks
53
---
64

7-
# Mutex
5+
A mutex (mutual exclusion) ensures that only one operation can access
6+
a shared resource at a time. This prevents race conditions and data
7+
corruption when multiple operations attempt to modify the same
8+
resource simultaneously.
89

9-
A mutex is like a "do not disturb" sign for your data. It ensures that
10-
only one operation can access a shared resource at a time. Imagine
11-
multiple people trying to edit the same document - a mutex makes sure
12-
only one person can edit it at once.
13-
14-
## Basic Usage
10+
## Basic usage
1511

1612
The easiest way to use a mutex is with the `withMutex` function, which
1713
automatically handles locking and unlocking:
@@ -26,7 +22,7 @@ const result = await withMutex('shared-resource', async () => {
2622
});
2723
```
2824

29-
## Custom Configuration
25+
## Custom configuration
3026

3127
You can create a custom mutex with different timeout settings:
3228

@@ -42,9 +38,9 @@ const result = await mutex.withLock('resource', async () => {
4238
});
4339
```
4440

45-
## Advanced Usage
41+
## Advanced usage
4642

47-
### Manual Lock Management
43+
### Manual lock management
4844

4945
Sometimes you need more control over when locks are acquired and
5046
released:
@@ -69,10 +65,9 @@ const locked = await isLocked('resource');
6965
console.log(`Resource is ${locked ? 'locked' : 'available'}`);
7066
```
7167

72-
### Cancelling Operations
68+
### Cancelling operations
7369

74-
You can cancel a mutex operation if it takes too long or if you need
75-
to stop it for any reason:
70+
You can cancel a mutex operation using an AbortSignal:
7671

7772
```typescript
7873
import { withMutex } from 'commandkit/mutex';
@@ -96,11 +91,10 @@ try {
9691
}
9792
```
9893

99-
### Using External Storage
94+
### Using external storage
10095

101-
By default, mutexes store lock information in memory. If you're
102-
running multiple servers, you'll want to use external storage like
103-
Redis:
96+
Mutexes store lock information in memory by default. For multi-server
97+
deployments, use external storage like Redis:
10498

10599
```typescript
106100
import { Mutex, MutexStorage } from 'commandkit/mutex';
@@ -129,33 +123,7 @@ const mutex = createMutex({
129123
});
130124
```
131125

132-
## Default Settings
126+
## Default settings
133127

134128
- **Timeout**: 30 seconds (30000ms)
135129
- **Storage**: In-memory (works for single-server applications)
136-
137-
## Common Use Cases
138-
139-
- **Database Transactions**: Ensure only one operation can modify data
140-
at a time
141-
- **File System Access**: Prevent multiple operations from writing to
142-
the same file
143-
- **Configuration Updates**: Make sure configuration changes don't
144-
conflict
145-
- **Cache Invalidation**: Control when cache is cleared to prevent
146-
race conditions
147-
- **Resource Pool Management**: Manage access to limited resources
148-
149-
## Tips for Beginners
150-
151-
1. **Use `withMutex` When Possible**: It automatically handles
152-
cleanup, so you don't forget to release locks
153-
2. **Set Reasonable Timeouts**: Don't make timeouts too short (might
154-
fail unnecessarily) or too long (might hang forever)
155-
3. **Use Descriptive Names**: Give your resources meaningful names
156-
like `user:123:profile` or `database:users`
157-
4. **Handle Errors**: Always handle cases where lock acquisition fails
158-
5. **Think About Deadlocks**: Be careful not to create situations
159-
where two operations wait for each other
160-
6. **Consider Your Setup**: Use external storage if you have multiple
161-
servers

apps/website/docs/guide/14-useful-utilities/03-semaphore.mdx

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
---
22
title: Semaphore
3-
description:
4-
Control concurrent access to limited resources with async semaphores
53
---
64

7-
# Semaphore
5+
A semaphore controls the number of operations that can access a
6+
resource concurrently. It maintains a set number of permits, allowing
7+
only that many operations to proceed simultaneously while others wait
8+
in queue.
89

9-
A semaphore is like a parking lot with a limited number of spaces. It
10-
allows a specific number of operations to happen at the same time, but
11-
no more. Perfect for controlling access to limited resources like
12-
database connections.
13-
14-
## Basic Usage
10+
## Basic usage
1511

1612
The easiest way to use a semaphore is with the `withPermit` function,
1713
which automatically handles getting and releasing permits:
@@ -26,7 +22,7 @@ const result = await withPermit('database-connection', async () => {
2622
});
2723
```
2824

29-
## Custom Configuration
25+
## Custom configuration
3026

3127
You can create a semaphore with specific limits and timeout settings:
3228

@@ -46,9 +42,9 @@ const result = await semaphore.withPermit(
4642
);
4743
```
4844

49-
## Advanced Usage
45+
## Advanced usage
5046

51-
### Manual Permit Management
47+
### Manual permit management
5248

5349
Sometimes you need more control over when permits are acquired and
5450
released:
@@ -77,10 +73,9 @@ const available = await getAvailablePermits('resource');
7773
console.log(`${available} permits available`);
7874
```
7975

80-
### Cancelling Operations
76+
### Cancelling operations
8177

82-
You can cancel a semaphore operation if it takes too long or if you
83-
need to stop it for any reason:
78+
You can cancel a semaphore operation using an AbortSignal:
8479

8580
```typescript
8681
import { withPermit } from 'commandkit/semaphore';
@@ -104,7 +99,7 @@ try {
10499
}
105100
```
106101

107-
### Monitoring Semaphore State
102+
### Monitoring semaphore state
108103

109104
You can check how many permits are being used and how many are
110105
available:
@@ -123,11 +118,10 @@ console.log(
123118
);
124119
```
125120

126-
### Using External Storage
121+
### Using external storage
127122

128-
By default, semaphores store permit information in memory. If you're
129-
running multiple servers, you'll want to use external storage like
130-
Redis:
123+
Semaphores store permit information in memory by default. For
124+
multi-server deployments, use external storage like Redis:
131125

132126
```typescript
133127
import { Semaphore, SemaphoreStorage } from 'commandkit/semaphore';
@@ -158,37 +152,8 @@ const semaphore = createSemaphore({
158152
});
159153
```
160154

161-
## Default Settings
155+
## Default settings
162156

163157
- **Permits**: 1 (sequential access)
164158
- **Timeout**: 30 seconds (30000ms)
165159
- **Storage**: In-memory (works for single-server applications)
166-
167-
## Common Use Cases
168-
169-
- **Database Connection Pooling**: Limit how many database connections
170-
are used at once
171-
- **API Rate Limiting with Concurrency**: Allow multiple API calls but
172-
not too many
173-
- **File Upload Throttling**: Control how many files can be uploaded
174-
simultaneously
175-
- **External Service Access**: Limit calls to third-party services
176-
- **Resource Pool Management**: Manage access to limited resources
177-
like memory or CPU
178-
179-
## Tips for Beginners
180-
181-
1. **Use `withPermit` When Possible**: It automatically handles
182-
cleanup, so you don't forget to release permits
183-
2. **Set Appropriate Limits**: Don't set too many permits (might
184-
overwhelm resources) or too few (might be too slow)
185-
3. **Monitor Usage**: Keep an eye on how many permits are being used
186-
to optimize performance
187-
4. **Use Descriptive Names**: Give your resources meaningful names
188-
like `database:main` or `api:external`
189-
5. **Handle Errors**: Always handle cases where permit acquisition
190-
fails
191-
6. **Consider Your Resources**: Set permit limits based on what your
192-
system can actually handle
193-
7. **Think About Your Setup**: Use external storage if you have
194-
multiple servers

0 commit comments

Comments
 (0)