@backendkit-labs/bulkhead 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +184 -0
- package/README.md +337 -278
- package/package.json +28 -15
package/LICENSE
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and the Work's derivative works.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of discussing and
|
|
55
|
+
improving the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as
|
|
57
|
+
"Not a Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and subsequently
|
|
61
|
+
incorporated within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by the combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a cross-claim
|
|
80
|
+
or counterclaim in a lawsuit) alleging that the Work or any
|
|
81
|
+
Contribution embodied within the Work constitutes direct or contributory
|
|
82
|
+
patent infringement, then any patent licenses granted to You under
|
|
83
|
+
this License for that Work shall terminate as of the date such
|
|
84
|
+
litigation is filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
87
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
88
|
+
modifications, and in Source or Object form, provided that You
|
|
89
|
+
meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
92
|
+
Works a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
98
|
+
that You distribute, all copyright, patent, trademark, and
|
|
99
|
+
attribution notices from the Source form of the Work,
|
|
100
|
+
excluding those notices that do not pertain to any part of
|
|
101
|
+
the Derivative Works; and
|
|
102
|
+
|
|
103
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
104
|
+
distribution, You must include a readable copy of the
|
|
105
|
+
attribution notices contained within such NOTICE file, in
|
|
106
|
+
at least one of the following places: within a NOTICE text
|
|
107
|
+
file distributed as part of the Derivative Works; within
|
|
108
|
+
the Source form or documentation, if provided along with the
|
|
109
|
+
Derivative Works; or, within a display generated by the
|
|
110
|
+
Derivative Works, if and wherever such third-party notices
|
|
111
|
+
normally appear. The contents of the NOTICE file are for
|
|
112
|
+
informational purposes only and do not modify the License.
|
|
113
|
+
You may add Your own attribution notices within Derivative
|
|
114
|
+
Works that You distribute, alongside or in addition to the
|
|
115
|
+
NOTICE text from the Work, provided that such additional
|
|
116
|
+
attribution notices cannot be construed as modifying the License.
|
|
117
|
+
|
|
118
|
+
You may add Your own license statement for Your modifications and
|
|
119
|
+
may provide additional grant of rights to use, copy, modify, and
|
|
120
|
+
distribute Your modifications, or for such Derivative Works as a
|
|
121
|
+
whole, subject to the terms and conditions of this License.
|
|
122
|
+
|
|
123
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
124
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
125
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
126
|
+
this License, without any additional terms or conditions.
|
|
127
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
128
|
+
the terms of any separate license agreement you may have executed
|
|
129
|
+
with Licensor regarding such Contributions.
|
|
130
|
+
|
|
131
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
132
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
133
|
+
except as required for reasonable and customary use in describing the
|
|
134
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
135
|
+
|
|
136
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
137
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
138
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
139
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
140
|
+
implied, including, without limitation, any warranties or conditions
|
|
141
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
142
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
143
|
+
appropriateness of using or reproducing the Work and assume any
|
|
144
|
+
risks associated with Your exercise of permissions under this License.
|
|
145
|
+
|
|
146
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
147
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
148
|
+
unless required by applicable law (such as deliberate and grossly
|
|
149
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
150
|
+
liable to You for damages, including any direct, indirect, special,
|
|
151
|
+
incidental, or exemplary damages of any character arising as a
|
|
152
|
+
result of this License or out of the use or inability to use the
|
|
153
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
154
|
+
work stoppage, computer failure or malfunction, or all other
|
|
155
|
+
commercial damages or losses), even if such Contributor has been
|
|
156
|
+
advised of the possibility of such damages.
|
|
157
|
+
|
|
158
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
159
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
160
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
161
|
+
or other liability obligations and/or rights consistent with this
|
|
162
|
+
License. However, in accepting such obligations, You may act only
|
|
163
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
164
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
165
|
+
defend, and hold each Contributor harmless for any liability
|
|
166
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
167
|
+
of your accepting any such warranty or additional liability.
|
|
168
|
+
|
|
169
|
+
END OF TERMS AND CONDITIONS
|
|
170
|
+
|
|
171
|
+
Copyright 2024-2026 Mairon José Cuello Martínez
|
|
172
|
+
|
|
173
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
174
|
+
you may not use this file except in compliance with the License.
|
|
175
|
+
You may obtain a copy of the License at
|
|
176
|
+
|
|
177
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
178
|
+
|
|
179
|
+
Unless required by applicable law or agreed to in writing, software
|
|
180
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
181
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
182
|
+
See the License for the specific language governing permissions and
|
|
183
|
+
limitations under the License.
|
|
184
|
+
|
package/README.md
CHANGED
|
@@ -1,278 +1,337 @@
|
|
|
1
|
-
# @backendkit-labs/bulkhead
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/@backendkit-labs/bulkhead)
|
|
4
|
-
[](https://github.com/backendkit-dev/backendkit-monorepo/actions/workflows/ci.yml)
|
|
5
|
-
[](LICENSE)
|
|
6
|
-
[](package.json)
|
|
7
|
-
|
|
8
|
-
> Bulkhead concurrency limiting for Node.js — inspired by Resilience4j. Framework-agnostic core with optional NestJS integration.
|
|
9
|
-
|
|
10
|
-
Prevents resource exhaustion and cascading failures by limiting how many operations run simultaneously on a given resource.
|
|
11
|
-
|
|
12
|
-
---
|
|
13
|
-
|
|
14
|
-
## Installation
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
npm install @backendkit-labs/bulkhead
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
|
|
22
|
-
##
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
@
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
@
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
1
|
+
# @backendkit-labs/bulkhead
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@backendkit-labs/bulkhead)
|
|
4
|
+
[](https://github.com/backendkit-dev/backendkit-monorepo/actions/workflows/ci.yml)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](package.json)
|
|
7
|
+
|
|
8
|
+
> Bulkhead concurrency limiting for Node.js — inspired by Resilience4j. Framework-agnostic core with optional NestJS integration.
|
|
9
|
+
|
|
10
|
+
Prevents resource exhaustion and cascading failures by limiting how many operations run simultaneously on a given resource.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @backendkit-labs/bulkhead
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## TypeScript Configuration
|
|
23
|
+
|
|
24
|
+
### Subpath exports (`/nestjs`)
|
|
25
|
+
|
|
26
|
+
This package uses the `exports` field in `package.json` to expose the `/nestjs` subpath. TypeScript's ability to resolve it depends on the `moduleResolution` setting in your `tsconfig.json`.
|
|
27
|
+
|
|
28
|
+
**Modern resolution (recommended) — no extra config needed:**
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"compilerOptions": {
|
|
33
|
+
"moduleResolution": "bundler"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`"bundler"`, `"node16"`, and `"nodenext"` all understand the `exports` field natively. This is the recommended setting for any project using a bundler or NestJS on TypeScript ≥ 5.
|
|
39
|
+
|
|
40
|
+
**Legacy resolution (`"node"`) — add a `paths` alias:**
|
|
41
|
+
|
|
42
|
+
NestJS projects generated before ~2024 default to `"moduleResolution": "node"`, which ignores the `exports` field. Add an explicit alias so TypeScript can find the types:
|
|
43
|
+
|
|
44
|
+
```json
|
|
45
|
+
{
|
|
46
|
+
"compilerOptions": {
|
|
47
|
+
"moduleResolution": "node",
|
|
48
|
+
"paths": {
|
|
49
|
+
"@backendkit-labs/bulkhead/nestjs": [
|
|
50
|
+
"./node_modules/@backendkit-labs/bulkhead/dist/nestjs/index"
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
> **Why?** The `"node"` resolver was designed before subpath exports existed and only reads `main`/`types` at the package root — it ignores the `exports` map entirely. The `paths` alias manually points TypeScript to the correct `.d.ts` file.
|
|
58
|
+
|
|
59
|
+
### NestJS decorator support
|
|
60
|
+
|
|
61
|
+
```json
|
|
62
|
+
{
|
|
63
|
+
"compilerOptions": {
|
|
64
|
+
"experimentalDecorators": true,
|
|
65
|
+
"emitDecoratorMetadata": true
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
And import `reflect-metadata` once at application startup:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
// main.ts
|
|
74
|
+
import 'reflect-metadata';
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
> NestJS CLI scaffolds these automatically. You only need to verify them if setting up a project manually.
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Quick Start — Framework-agnostic
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { Bulkhead } from '@backendkit-labs/bulkhead';
|
|
85
|
+
|
|
86
|
+
const bulkhead = new Bulkhead({
|
|
87
|
+
name: 'payments',
|
|
88
|
+
maxConcurrentCalls: 10,
|
|
89
|
+
maxQueueSize: 50,
|
|
90
|
+
queueTimeoutMs: 5000,
|
|
91
|
+
rejectWhenFull: true,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const result = await bulkhead.execute(() => callPaymentApi());
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Core API
|
|
100
|
+
|
|
101
|
+
### `Bulkhead`
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
const bulkhead = new Bulkhead(config);
|
|
105
|
+
|
|
106
|
+
// Execute a task — waits in queue if at capacity
|
|
107
|
+
await bulkhead.execute(async () => { ... });
|
|
108
|
+
|
|
109
|
+
// Check if capacity is available before executing
|
|
110
|
+
if (bulkhead.canAccept()) { ... }
|
|
111
|
+
|
|
112
|
+
// Current metrics snapshot
|
|
113
|
+
const metrics = bulkhead.getMetrics();
|
|
114
|
+
|
|
115
|
+
// Reset all counters
|
|
116
|
+
bulkhead.resetMetrics();
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### `BulkheadConfig`
|
|
120
|
+
|
|
121
|
+
| Property | Type | Description |
|
|
122
|
+
|----------|------|-------------|
|
|
123
|
+
| `name` | `string` | Identifier for metrics and error messages |
|
|
124
|
+
| `maxConcurrentCalls` | `number` | Max simultaneous executions |
|
|
125
|
+
| `maxQueueSize` | `number` | Max tasks waiting in queue |
|
|
126
|
+
| `queueTimeoutMs` | `number` | Max time a task can wait in queue (ms) |
|
|
127
|
+
| `rejectWhenFull` | `boolean` | Throw immediately when full; if `false`, retries with exponential backoff |
|
|
128
|
+
|
|
129
|
+
### `BulkheadMetrics`
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
{
|
|
133
|
+
name: string;
|
|
134
|
+
activeCalls: number;
|
|
135
|
+
queuedCalls: number;
|
|
136
|
+
maxConcurrentCalls: number;
|
|
137
|
+
maxQueueSize: number;
|
|
138
|
+
totalCalls: number;
|
|
139
|
+
successfulCalls: number;
|
|
140
|
+
failedCalls: number;
|
|
141
|
+
rejectedCalls: number;
|
|
142
|
+
timedOutCalls: number;
|
|
143
|
+
averageDurationMs: number;
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Errors
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { BulkheadRejectedError, BulkheadTimeoutError } from '@backendkit-labs/bulkhead';
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
await bulkhead.execute(task);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
if (error instanceof BulkheadRejectedError) {
|
|
156
|
+
// Queue was full — task was not queued
|
|
157
|
+
}
|
|
158
|
+
if (error instanceof BulkheadTimeoutError) {
|
|
159
|
+
// Task waited too long in queue
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
## BulkheadRegistry
|
|
167
|
+
|
|
168
|
+
Manages named bulkhead instances with sensible defaults for common resource types:
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { BulkheadRegistry } from '@backendkit-labs/bulkhead';
|
|
172
|
+
|
|
173
|
+
const registry = new BulkheadRegistry();
|
|
174
|
+
|
|
175
|
+
// Custom
|
|
176
|
+
const bh = registry.getOrCreate({ name: 'my-service', maxConcurrentCalls: 15 });
|
|
177
|
+
|
|
178
|
+
// Pre-configured factory methods
|
|
179
|
+
const clientBh = registry.getForClient('client-123', '/api/orders'); // 5 concurrent, 20 queued
|
|
180
|
+
const serviceBh = registry.getForService('inventory-service'); // 20 concurrent, 200 queued
|
|
181
|
+
const dbBh = registry.getForDatabase('orders_schema'); // 15 concurrent, 150 queued
|
|
182
|
+
const externalBh = registry.getForHttpExternal('stripe-api'); // 8 concurrent, 50 queued, 10s timeout
|
|
183
|
+
|
|
184
|
+
// Observability
|
|
185
|
+
const all = registry.getAllMetrics();
|
|
186
|
+
const overloaded = registry.getOverloadedBulkheads(); // ≥80% active capacity
|
|
187
|
+
registry.resetAllMetrics();
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
| Method | Concurrent | Queue | Timeout |
|
|
191
|
+
|--------|-----------|-------|---------|
|
|
192
|
+
| `getForClient(id, endpoint?)` | 5 | 20 | 30s |
|
|
193
|
+
| `getForService(name)` | 20 | 200 | 30s |
|
|
194
|
+
| `getForDatabase(schema)` | 15 | 150 | 30s |
|
|
195
|
+
| `getForHttpExternal(name)` | 8 | 50 | 10s |
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
## NestJS Integration
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
npm install @backendkit-labs/bulkhead
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Import `BulkheadModule` into your NestJS application:
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
import { BulkheadModule } from '@backendkit-labs/bulkhead/nestjs';
|
|
209
|
+
|
|
210
|
+
@Module({
|
|
211
|
+
imports: [BulkheadModule],
|
|
212
|
+
})
|
|
213
|
+
export class AppModule {}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Guard — declarative per-route protection
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
import { UseBulkhead, BulkheadGuard } from '@backendkit-labs/bulkhead/nestjs';
|
|
220
|
+
|
|
221
|
+
@Controller('orders')
|
|
222
|
+
export class OrdersController {
|
|
223
|
+
// Shared service-level limit
|
|
224
|
+
@UseBulkhead({ name: 'orders-service' })
|
|
225
|
+
@UseGuards(BulkheadGuard)
|
|
226
|
+
@Get()
|
|
227
|
+
findAll() { ... }
|
|
228
|
+
|
|
229
|
+
// Per-client isolation (reads x-client-id header)
|
|
230
|
+
@UseBulkhead({ name: 'orders-create', perClient: true })
|
|
231
|
+
@UseGuards(BulkheadGuard)
|
|
232
|
+
@Post()
|
|
233
|
+
create() { ... }
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Returns `503 Service Unavailable` when at capacity.
|
|
238
|
+
|
|
239
|
+
### Interceptor — wraps handler execution inside the bulkhead
|
|
240
|
+
|
|
241
|
+
```typescript
|
|
242
|
+
import { BulkheadInterceptor } from '@backendkit-labs/bulkhead/nestjs';
|
|
243
|
+
|
|
244
|
+
// Apply globally
|
|
245
|
+
app.useGlobalInterceptors(new BulkheadInterceptor(registry));
|
|
246
|
+
|
|
247
|
+
// Or per controller / route
|
|
248
|
+
@UseInterceptors(BulkheadInterceptor)
|
|
249
|
+
@Controller('reports')
|
|
250
|
+
export class ReportsController { ... }
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Returns `503` on rejection, `408` on timeout.
|
|
254
|
+
|
|
255
|
+
### Middleware — global HTTP concurrency limit
|
|
256
|
+
|
|
257
|
+
Protects the entire service from being overwhelmed before requests even reach your handlers:
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
import { HttpBulkheadMiddleware } from '@backendkit-labs/bulkhead/nestjs';
|
|
261
|
+
|
|
262
|
+
@Module({ imports: [BulkheadModule] })
|
|
263
|
+
export class AppModule implements NestModule {
|
|
264
|
+
configure(consumer: MiddlewareConsumer) {
|
|
265
|
+
consumer.apply(HttpBulkheadMiddleware).forRoutes('*');
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Configure via environment variables:
|
|
271
|
+
|
|
272
|
+
| Variable | Default | Description |
|
|
273
|
+
|----------|---------|-------------|
|
|
274
|
+
| `HTTP_BULKHEAD_CONCURRENCY` | `50` | Max concurrent requests |
|
|
275
|
+
| `HTTP_BULKHEAD_MAX_QUEUE` | `100` | Max queued requests |
|
|
276
|
+
|
|
277
|
+
Returns `429 Too Many Requests` when the queue is full.
|
|
278
|
+
|
|
279
|
+
### Method Decorator
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
import { WithBulkhead } from '@backendkit-labs/bulkhead/nestjs';
|
|
283
|
+
|
|
284
|
+
@Injectable()
|
|
285
|
+
export class ReportService {
|
|
286
|
+
// Must have bulkheadRegistry injected
|
|
287
|
+
constructor(public readonly bulkheadRegistry: BulkheadRegistry) {}
|
|
288
|
+
|
|
289
|
+
@WithBulkhead({ name: 'report-generation', maxConcurrent: 3 })
|
|
290
|
+
async generateReport(id: string) { ... }
|
|
291
|
+
}
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### Monitoring — BulkheadService
|
|
295
|
+
|
|
296
|
+
```typescript
|
|
297
|
+
import { BulkheadService } from '@backendkit-labs/bulkhead/nestjs';
|
|
298
|
+
|
|
299
|
+
@Controller('health')
|
|
300
|
+
export class HealthController {
|
|
301
|
+
constructor(private readonly bulkheads: BulkheadService) {}
|
|
302
|
+
|
|
303
|
+
@Get('bulkheads')
|
|
304
|
+
getMetrics() {
|
|
305
|
+
return {
|
|
306
|
+
all: this.bulkheads.getAllMetrics(),
|
|
307
|
+
critical: this.bulkheads.getCriticalBulkheads(), // ≥90% active
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
`BulkheadService` also logs a warning every 60 seconds when any bulkhead reaches 90%+ utilization.
|
|
314
|
+
|
|
315
|
+
---
|
|
316
|
+
|
|
317
|
+
## Architecture
|
|
318
|
+
|
|
319
|
+
```
|
|
320
|
+
@backendkit-labs/bulkhead (core — no framework deps)
|
|
321
|
+
Bulkhead queue-based concurrency limiter
|
|
322
|
+
BulkheadRegistry named instances + factory methods
|
|
323
|
+
|
|
324
|
+
@backendkit-labs/bulkhead/nestjs (optional NestJS layer)
|
|
325
|
+
BulkheadModule NestJS module
|
|
326
|
+
BulkheadGuard @UseBulkhead() per-route decorator
|
|
327
|
+
BulkheadInterceptor wraps handler in execute()
|
|
328
|
+
HttpBulkheadMiddleware global HTTP request limiter
|
|
329
|
+
WithBulkhead method-level decorator
|
|
330
|
+
BulkheadService metrics + auto-monitoring
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
---
|
|
334
|
+
|
|
335
|
+
## License
|
|
336
|
+
|
|
337
|
+
Apache-2.0 — [BackendKit Labs](https://github.com/backendkit-dev)
|
package/package.json
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backendkit-labs/bulkhead",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "BackendKit Labs",
|
|
7
|
+
"email": "backendkit.dev@gmail.com"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://github.com/backendkit-dev/backendkit-monorepo",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/backendkit-dev/backendkit-monorepo.git",
|
|
13
|
+
"directory": "packages/bulkhead"
|
|
14
|
+
},
|
|
15
|
+
"description": "Bulkhead concurrency limiting for Node.js — inspired by Resilience4j, with optional NestJS integration",
|
|
5
16
|
"type": "module",
|
|
6
17
|
"main": "./dist/index.cjs",
|
|
7
18
|
"module": "./dist/index.js",
|
|
@@ -44,18 +55,12 @@
|
|
|
44
55
|
"node",
|
|
45
56
|
"backend"
|
|
46
57
|
],
|
|
47
|
-
"author": "Mairon José Cuello Martínez <[backendkit.dev@gmail.com - mmairon@gmail.com]>",
|
|
48
|
-
"license": "MIT",
|
|
49
|
-
"homepage": "https://github.com/backendkit-dev/backendkit-monorepo/tree/master/packages/bulkhead#readme",
|
|
50
|
-
"repository": {
|
|
51
|
-
"type": "git",
|
|
52
|
-
"url": "git+https://github.com/backendkit-dev/backendkit-monorepo.git",
|
|
53
|
-
"directory": "packages/bulkhead"
|
|
54
|
-
},
|
|
55
58
|
"bugs": {
|
|
56
59
|
"url": "https://github.com/backendkit-dev/backendkit-monorepo/issues"
|
|
57
60
|
},
|
|
58
|
-
"publishConfig": {
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
},
|
|
59
64
|
"sideEffects": false,
|
|
60
65
|
"engines": {
|
|
61
66
|
"node": ">=18"
|
|
@@ -67,10 +72,18 @@
|
|
|
67
72
|
"rxjs": ">=7.0.0"
|
|
68
73
|
},
|
|
69
74
|
"peerDependenciesMeta": {
|
|
70
|
-
"@nestjs/common": {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"
|
|
75
|
+
"@nestjs/common": {
|
|
76
|
+
"optional": true
|
|
77
|
+
},
|
|
78
|
+
"@nestjs/core": {
|
|
79
|
+
"optional": true
|
|
80
|
+
},
|
|
81
|
+
"express": {
|
|
82
|
+
"optional": true
|
|
83
|
+
},
|
|
84
|
+
"rxjs": {
|
|
85
|
+
"optional": true
|
|
86
|
+
}
|
|
74
87
|
},
|
|
75
88
|
"devDependencies": {
|
|
76
89
|
"@eslint/js": "^9.39.4",
|