threadforge 0.1.0 → 0.2.1

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.
Files changed (359) hide show
  1. package/README.md +52 -20
  2. package/bin/forge.js +2 -1049
  3. package/bin/host-commands.d.ts +2 -0
  4. package/bin/host-commands.d.ts.map +1 -0
  5. package/bin/host-commands.js +7 -8
  6. package/bin/platform-commands.d.ts +2 -0
  7. package/bin/platform-commands.d.ts.map +1 -0
  8. package/bin/platform-commands.js +160 -49
  9. package/dist/cli/base-command.d.ts +12 -0
  10. package/dist/cli/base-command.d.ts.map +1 -0
  11. package/dist/cli/base-command.js +25 -0
  12. package/dist/cli/base-command.js.map +1 -0
  13. package/dist/cli/commands/build.d.ts +10 -0
  14. package/dist/cli/commands/build.d.ts.map +1 -0
  15. package/dist/cli/commands/build.js +110 -0
  16. package/dist/cli/commands/build.js.map +1 -0
  17. package/dist/cli/commands/deploy.d.ts +12 -0
  18. package/dist/cli/commands/deploy.d.ts.map +1 -0
  19. package/dist/cli/commands/deploy.js +143 -0
  20. package/dist/cli/commands/deploy.js.map +1 -0
  21. package/dist/cli/commands/dev.d.ts +10 -0
  22. package/dist/cli/commands/dev.d.ts.map +1 -0
  23. package/dist/cli/commands/dev.js +138 -0
  24. package/dist/cli/commands/dev.js.map +1 -0
  25. package/dist/cli/commands/generate.d.ts +10 -0
  26. package/dist/cli/commands/generate.d.ts.map +1 -0
  27. package/dist/cli/commands/generate.js +76 -0
  28. package/dist/cli/commands/generate.js.map +1 -0
  29. package/dist/cli/commands/host.d.ts +8 -0
  30. package/dist/cli/commands/host.d.ts.map +1 -0
  31. package/dist/cli/commands/host.js +20 -0
  32. package/dist/cli/commands/host.js.map +1 -0
  33. package/dist/cli/commands/init.d.ts +16 -0
  34. package/dist/cli/commands/init.d.ts.map +1 -0
  35. package/dist/cli/commands/init.js +246 -0
  36. package/dist/cli/commands/init.js.map +1 -0
  37. package/dist/cli/commands/platform.d.ts +8 -0
  38. package/dist/cli/commands/platform.d.ts.map +1 -0
  39. package/dist/cli/commands/platform.js +20 -0
  40. package/dist/cli/commands/platform.js.map +1 -0
  41. package/dist/cli/commands/restart.d.ts +8 -0
  42. package/dist/cli/commands/restart.d.ts.map +1 -0
  43. package/dist/cli/commands/restart.js +13 -0
  44. package/dist/cli/commands/restart.js.map +1 -0
  45. package/dist/cli/commands/scaffold/frontend.d.ts +10 -0
  46. package/dist/cli/commands/scaffold/frontend.d.ts.map +1 -0
  47. package/dist/cli/commands/scaffold/frontend.js +130 -0
  48. package/dist/cli/commands/scaffold/frontend.js.map +1 -0
  49. package/dist/cli/commands/scaffold/react.d.ts +7 -0
  50. package/dist/cli/commands/scaffold/react.d.ts.map +1 -0
  51. package/dist/cli/commands/scaffold/react.js +12 -0
  52. package/dist/cli/commands/scaffold/react.js.map +1 -0
  53. package/dist/cli/commands/scale.d.ts +8 -0
  54. package/dist/cli/commands/scale.d.ts.map +1 -0
  55. package/dist/cli/commands/scale.js +13 -0
  56. package/dist/cli/commands/scale.js.map +1 -0
  57. package/dist/cli/commands/start.d.ts +10 -0
  58. package/dist/cli/commands/start.d.ts.map +1 -0
  59. package/dist/cli/commands/start.js +71 -0
  60. package/dist/cli/commands/start.js.map +1 -0
  61. package/dist/cli/commands/status.d.ts +11 -0
  62. package/dist/cli/commands/status.d.ts.map +1 -0
  63. package/dist/cli/commands/status.js +60 -0
  64. package/dist/cli/commands/status.js.map +1 -0
  65. package/dist/cli/commands/stop.d.ts +10 -0
  66. package/dist/cli/commands/stop.d.ts.map +1 -0
  67. package/dist/cli/commands/stop.js +89 -0
  68. package/dist/cli/commands/stop.js.map +1 -0
  69. package/dist/cli/util/config-discovery.d.ts +8 -0
  70. package/dist/cli/util/config-discovery.d.ts.map +1 -0
  71. package/dist/cli/util/config-discovery.js +70 -0
  72. package/dist/cli/util/config-discovery.js.map +1 -0
  73. package/dist/cli/util/config-patcher.d.ts +17 -0
  74. package/dist/cli/util/config-patcher.d.ts.map +1 -0
  75. package/dist/cli/util/config-patcher.js +439 -0
  76. package/dist/cli/util/config-patcher.js.map +1 -0
  77. package/dist/cli/util/frontend-dev.d.ts +8 -0
  78. package/dist/cli/util/frontend-dev.d.ts.map +1 -0
  79. package/dist/cli/util/frontend-dev.js +117 -0
  80. package/dist/cli/util/frontend-dev.js.map +1 -0
  81. package/dist/cli/util/process.d.ts +5 -0
  82. package/dist/cli/util/process.d.ts.map +1 -0
  83. package/dist/cli/util/process.js +17 -0
  84. package/dist/cli/util/process.js.map +1 -0
  85. package/dist/cli/util/templates.d.ts +10 -0
  86. package/dist/cli/util/templates.d.ts.map +1 -0
  87. package/dist/cli/util/templates.js +157 -0
  88. package/dist/cli/util/templates.js.map +1 -0
  89. package/dist/core/AlertSink.d.ts +83 -0
  90. package/dist/core/AlertSink.d.ts.map +1 -0
  91. package/dist/core/AlertSink.js +126 -0
  92. package/dist/core/AlertSink.js.map +1 -0
  93. package/dist/core/DirectMessageBus.d.ts +88 -0
  94. package/dist/core/DirectMessageBus.d.ts.map +1 -0
  95. package/dist/core/DirectMessageBus.js +352 -0
  96. package/dist/core/DirectMessageBus.js.map +1 -0
  97. package/dist/core/EndpointResolver.d.ts +111 -0
  98. package/dist/core/EndpointResolver.d.ts.map +1 -0
  99. package/dist/core/EndpointResolver.js +336 -0
  100. package/dist/core/EndpointResolver.js.map +1 -0
  101. package/dist/core/ForgeContext.d.ts +221 -0
  102. package/dist/core/ForgeContext.d.ts.map +1 -0
  103. package/dist/core/ForgeContext.js +1169 -0
  104. package/dist/core/ForgeContext.js.map +1 -0
  105. package/dist/core/ForgeEndpoints.d.ts +71 -0
  106. package/dist/core/ForgeEndpoints.d.ts.map +1 -0
  107. package/dist/core/ForgeEndpoints.js +442 -0
  108. package/dist/core/ForgeEndpoints.js.map +1 -0
  109. package/dist/core/ForgeHost.d.ts +82 -0
  110. package/dist/core/ForgeHost.d.ts.map +1 -0
  111. package/dist/core/ForgeHost.js +107 -0
  112. package/dist/core/ForgeHost.js.map +1 -0
  113. package/dist/core/ForgePlatform.d.ts +96 -0
  114. package/dist/core/ForgePlatform.d.ts.map +1 -0
  115. package/dist/core/ForgePlatform.js +136 -0
  116. package/dist/core/ForgePlatform.js.map +1 -0
  117. package/dist/core/ForgeWebSocket.d.ts +56 -0
  118. package/dist/core/ForgeWebSocket.d.ts.map +1 -0
  119. package/dist/core/ForgeWebSocket.js +415 -0
  120. package/dist/core/ForgeWebSocket.js.map +1 -0
  121. package/dist/core/Ingress.d.ts +329 -0
  122. package/dist/core/Ingress.d.ts.map +1 -0
  123. package/dist/core/Ingress.js +694 -0
  124. package/dist/core/Ingress.js.map +1 -0
  125. package/dist/core/Interceptors.d.ts +134 -0
  126. package/dist/core/Interceptors.d.ts.map +1 -0
  127. package/dist/core/Interceptors.js +416 -0
  128. package/dist/core/Interceptors.js.map +1 -0
  129. package/dist/core/Logger.d.ts +20 -0
  130. package/dist/core/Logger.d.ts.map +1 -0
  131. package/dist/core/Logger.js +77 -0
  132. package/dist/core/Logger.js.map +1 -0
  133. package/dist/core/MessageBus.d.ts +15 -0
  134. package/dist/core/MessageBus.d.ts.map +1 -0
  135. package/dist/core/MessageBus.js +18 -0
  136. package/dist/core/MessageBus.js.map +1 -0
  137. package/dist/core/Prometheus.d.ts +80 -0
  138. package/dist/core/Prometheus.d.ts.map +1 -0
  139. package/dist/core/Prometheus.js +332 -0
  140. package/dist/core/Prometheus.js.map +1 -0
  141. package/dist/core/RequestContext.d.ts +214 -0
  142. package/dist/core/RequestContext.d.ts.map +1 -0
  143. package/dist/core/RequestContext.js +556 -0
  144. package/dist/core/RequestContext.js.map +1 -0
  145. package/dist/core/Router.d.ts +45 -0
  146. package/dist/core/Router.d.ts.map +1 -0
  147. package/dist/core/Router.js +285 -0
  148. package/dist/core/Router.js.map +1 -0
  149. package/dist/core/RoutingStrategy.d.ts +116 -0
  150. package/dist/core/RoutingStrategy.d.ts.map +1 -0
  151. package/dist/core/RoutingStrategy.js +306 -0
  152. package/dist/core/RoutingStrategy.js.map +1 -0
  153. package/dist/core/RpcConfig.d.ts +72 -0
  154. package/dist/core/RpcConfig.d.ts.map +1 -0
  155. package/dist/core/RpcConfig.js +127 -0
  156. package/dist/core/RpcConfig.js.map +1 -0
  157. package/dist/core/SignatureCache.d.ts +81 -0
  158. package/dist/core/SignatureCache.d.ts.map +1 -0
  159. package/dist/core/SignatureCache.js +172 -0
  160. package/dist/core/SignatureCache.js.map +1 -0
  161. package/dist/core/StaticFileServer.d.ts +34 -0
  162. package/dist/core/StaticFileServer.d.ts.map +1 -0
  163. package/dist/core/StaticFileServer.js +497 -0
  164. package/dist/core/StaticFileServer.js.map +1 -0
  165. package/dist/core/Supervisor.d.ts +198 -0
  166. package/dist/core/Supervisor.d.ts.map +1 -0
  167. package/dist/core/Supervisor.js +1418 -0
  168. package/dist/core/Supervisor.js.map +1 -0
  169. package/dist/core/ThreadAllocator.d.ts +52 -0
  170. package/dist/core/ThreadAllocator.d.ts.map +1 -0
  171. package/dist/core/ThreadAllocator.js +174 -0
  172. package/dist/core/ThreadAllocator.js.map +1 -0
  173. package/dist/core/WorkerChannelManager.d.ts +130 -0
  174. package/dist/core/WorkerChannelManager.d.ts.map +1 -0
  175. package/dist/core/WorkerChannelManager.js +956 -0
  176. package/dist/core/WorkerChannelManager.js.map +1 -0
  177. package/dist/core/config-enums.d.ts +41 -0
  178. package/dist/core/config-enums.d.ts.map +1 -0
  179. package/dist/core/config-enums.js +59 -0
  180. package/dist/core/config-enums.js.map +1 -0
  181. package/dist/core/config.d.ts +159 -0
  182. package/dist/core/config.d.ts.map +1 -0
  183. package/dist/core/config.js +694 -0
  184. package/dist/core/config.js.map +1 -0
  185. package/dist/core/host-config.d.ts +146 -0
  186. package/dist/core/host-config.d.ts.map +1 -0
  187. package/dist/core/host-config.js +312 -0
  188. package/dist/core/host-config.js.map +1 -0
  189. package/dist/core/ipc-errors.d.ts +27 -0
  190. package/dist/core/ipc-errors.d.ts.map +1 -0
  191. package/dist/core/ipc-errors.js +36 -0
  192. package/dist/core/ipc-errors.js.map +1 -0
  193. package/dist/core/network-utils.d.ts +35 -0
  194. package/dist/core/network-utils.d.ts.map +1 -0
  195. package/dist/core/network-utils.js +145 -0
  196. package/dist/core/network-utils.js.map +1 -0
  197. package/dist/core/platform-config.d.ts +142 -0
  198. package/dist/core/platform-config.d.ts.map +1 -0
  199. package/dist/core/platform-config.js +299 -0
  200. package/dist/core/platform-config.js.map +1 -0
  201. package/dist/decorators/ServiceProxy.d.ts +175 -0
  202. package/dist/decorators/ServiceProxy.d.ts.map +1 -0
  203. package/dist/decorators/ServiceProxy.js +969 -0
  204. package/dist/decorators/ServiceProxy.js.map +1 -0
  205. package/dist/decorators/index.d.ts +146 -0
  206. package/dist/decorators/index.d.ts.map +1 -0
  207. package/dist/decorators/index.js +545 -0
  208. package/dist/decorators/index.js.map +1 -0
  209. package/dist/deploy/NginxGenerator.d.ts +165 -0
  210. package/dist/deploy/NginxGenerator.d.ts.map +1 -0
  211. package/dist/deploy/NginxGenerator.js +781 -0
  212. package/dist/deploy/NginxGenerator.js.map +1 -0
  213. package/dist/deploy/PlatformManifestGenerator.d.ts +43 -0
  214. package/dist/deploy/PlatformManifestGenerator.d.ts.map +1 -0
  215. package/dist/deploy/PlatformManifestGenerator.js +80 -0
  216. package/dist/deploy/PlatformManifestGenerator.js.map +1 -0
  217. package/dist/deploy/RouteManifestGenerator.d.ts +42 -0
  218. package/dist/deploy/RouteManifestGenerator.d.ts.map +1 -0
  219. package/dist/deploy/RouteManifestGenerator.js +105 -0
  220. package/dist/deploy/RouteManifestGenerator.js.map +1 -0
  221. package/dist/deploy/index.d.ts +210 -0
  222. package/dist/deploy/index.d.ts.map +1 -0
  223. package/dist/deploy/index.js +918 -0
  224. package/dist/deploy/index.js.map +1 -0
  225. package/dist/frontend/FrontendDevLifecycle.d.ts +26 -0
  226. package/dist/frontend/FrontendDevLifecycle.d.ts.map +1 -0
  227. package/dist/frontend/FrontendDevLifecycle.js +60 -0
  228. package/dist/frontend/FrontendDevLifecycle.js.map +1 -0
  229. package/dist/frontend/FrontendPluginOrchestrator.d.ts +64 -0
  230. package/dist/frontend/FrontendPluginOrchestrator.d.ts.map +1 -0
  231. package/dist/frontend/FrontendPluginOrchestrator.js +167 -0
  232. package/dist/frontend/FrontendPluginOrchestrator.js.map +1 -0
  233. package/dist/frontend/SiteResolver.d.ts +33 -0
  234. package/dist/frontend/SiteResolver.d.ts.map +1 -0
  235. package/dist/frontend/SiteResolver.js +53 -0
  236. package/dist/frontend/SiteResolver.js.map +1 -0
  237. package/dist/frontend/StaticMountRegistry.d.ts +36 -0
  238. package/dist/frontend/StaticMountRegistry.d.ts.map +1 -0
  239. package/dist/frontend/StaticMountRegistry.js +94 -0
  240. package/dist/frontend/StaticMountRegistry.js.map +1 -0
  241. package/dist/frontend/index.d.ts +7 -0
  242. package/dist/frontend/index.d.ts.map +1 -0
  243. package/{src → dist}/frontend/index.js +4 -2
  244. package/dist/frontend/index.js.map +1 -0
  245. package/dist/frontend/pathUtils.d.ts +8 -0
  246. package/dist/frontend/pathUtils.d.ts.map +1 -0
  247. package/dist/frontend/pathUtils.js +17 -0
  248. package/dist/frontend/pathUtils.js.map +1 -0
  249. package/dist/frontend/plugins/index.d.ts +2 -0
  250. package/dist/frontend/plugins/index.d.ts.map +1 -0
  251. package/{src → dist}/frontend/plugins/index.js +1 -1
  252. package/dist/frontend/plugins/index.js.map +1 -0
  253. package/dist/frontend/plugins/viteFrontend.d.ts +51 -0
  254. package/dist/frontend/plugins/viteFrontend.d.ts.map +1 -0
  255. package/dist/frontend/plugins/viteFrontend.js +134 -0
  256. package/dist/frontend/plugins/viteFrontend.js.map +1 -0
  257. package/dist/frontend/types.d.ts +25 -0
  258. package/dist/frontend/types.d.ts.map +1 -0
  259. package/dist/frontend/types.js +2 -0
  260. package/dist/frontend/types.js.map +1 -0
  261. package/dist/index.d.ts +17 -0
  262. package/dist/index.d.ts.map +1 -0
  263. package/dist/index.js +32 -0
  264. package/dist/index.js.map +1 -0
  265. package/dist/internals.d.ts +21 -0
  266. package/dist/internals.d.ts.map +1 -0
  267. package/{src → dist}/internals.js +12 -14
  268. package/dist/internals.js.map +1 -0
  269. package/dist/plugins/PluginManager.d.ts +209 -0
  270. package/dist/plugins/PluginManager.d.ts.map +1 -0
  271. package/dist/plugins/PluginManager.js +365 -0
  272. package/dist/plugins/PluginManager.js.map +1 -0
  273. package/dist/plugins/ScopedPostgres.d.ts +78 -0
  274. package/dist/plugins/ScopedPostgres.d.ts.map +1 -0
  275. package/dist/plugins/ScopedPostgres.js +190 -0
  276. package/dist/plugins/ScopedPostgres.js.map +1 -0
  277. package/dist/plugins/ScopedRedis.d.ts +88 -0
  278. package/dist/plugins/ScopedRedis.d.ts.map +1 -0
  279. package/dist/plugins/ScopedRedis.js +169 -0
  280. package/dist/plugins/ScopedRedis.js.map +1 -0
  281. package/dist/plugins/index.d.ts +289 -0
  282. package/dist/plugins/index.d.ts.map +1 -0
  283. package/dist/plugins/index.js +1942 -0
  284. package/dist/plugins/index.js.map +1 -0
  285. package/dist/plugins/types.d.ts +59 -0
  286. package/dist/plugins/types.d.ts.map +1 -0
  287. package/dist/plugins/types.js +2 -0
  288. package/dist/plugins/types.js.map +1 -0
  289. package/dist/registry/ServiceRegistry.d.ts +305 -0
  290. package/dist/registry/ServiceRegistry.d.ts.map +1 -0
  291. package/dist/registry/ServiceRegistry.js +735 -0
  292. package/dist/registry/ServiceRegistry.js.map +1 -0
  293. package/dist/scaling/ScaleAdvisor.d.ts +214 -0
  294. package/dist/scaling/ScaleAdvisor.d.ts.map +1 -0
  295. package/dist/scaling/ScaleAdvisor.js +526 -0
  296. package/dist/scaling/ScaleAdvisor.js.map +1 -0
  297. package/dist/services/Service.d.ts +164 -0
  298. package/dist/services/Service.d.ts.map +1 -0
  299. package/dist/services/Service.js +106 -0
  300. package/dist/services/Service.js.map +1 -0
  301. package/dist/services/worker-bootstrap.d.ts +15 -0
  302. package/dist/services/worker-bootstrap.d.ts.map +1 -0
  303. package/dist/services/worker-bootstrap.js +744 -0
  304. package/dist/services/worker-bootstrap.js.map +1 -0
  305. package/dist/templates/auth-service.d.ts +42 -0
  306. package/dist/templates/auth-service.d.ts.map +1 -0
  307. package/dist/templates/auth-service.js +54 -0
  308. package/dist/templates/auth-service.js.map +1 -0
  309. package/dist/templates/identity-service.d.ts +50 -0
  310. package/dist/templates/identity-service.d.ts.map +1 -0
  311. package/dist/templates/identity-service.js +62 -0
  312. package/dist/templates/identity-service.js.map +1 -0
  313. package/dist/types/contract.d.ts +120 -0
  314. package/dist/types/contract.d.ts.map +1 -0
  315. package/dist/types/contract.js +69 -0
  316. package/dist/types/contract.js.map +1 -0
  317. package/package.json +84 -22
  318. package/shared/identity.js +26 -0
  319. package/src/core/DirectMessageBus.js +0 -364
  320. package/src/core/EndpointResolver.js +0 -247
  321. package/src/core/ForgeContext.js +0 -2227
  322. package/src/core/ForgeHost.js +0 -122
  323. package/src/core/ForgePlatform.js +0 -145
  324. package/src/core/Ingress.js +0 -768
  325. package/src/core/Interceptors.js +0 -420
  326. package/src/core/MessageBus.js +0 -310
  327. package/src/core/Prometheus.js +0 -305
  328. package/src/core/RequestContext.js +0 -413
  329. package/src/core/RoutingStrategy.js +0 -316
  330. package/src/core/Supervisor.js +0 -1306
  331. package/src/core/ThreadAllocator.js +0 -196
  332. package/src/core/WorkerChannelManager.js +0 -879
  333. package/src/core/config.js +0 -624
  334. package/src/core/host-config.js +0 -311
  335. package/src/core/network-utils.js +0 -166
  336. package/src/core/platform-config.js +0 -308
  337. package/src/decorators/ServiceProxy.js +0 -899
  338. package/src/decorators/index.js +0 -571
  339. package/src/deploy/NginxGenerator.js +0 -865
  340. package/src/deploy/PlatformManifestGenerator.js +0 -96
  341. package/src/deploy/RouteManifestGenerator.js +0 -112
  342. package/src/deploy/index.js +0 -984
  343. package/src/frontend/FrontendDevLifecycle.js +0 -65
  344. package/src/frontend/FrontendPluginOrchestrator.js +0 -187
  345. package/src/frontend/SiteResolver.js +0 -63
  346. package/src/frontend/StaticMountRegistry.js +0 -90
  347. package/src/frontend/plugins/viteFrontend.js +0 -79
  348. package/src/frontend/types.js +0 -35
  349. package/src/index.js +0 -56
  350. package/src/plugins/PluginManager.js +0 -537
  351. package/src/plugins/ScopedPostgres.js +0 -192
  352. package/src/plugins/ScopedRedis.js +0 -142
  353. package/src/plugins/index.js +0 -1729
  354. package/src/registry/ServiceRegistry.js +0 -796
  355. package/src/scaling/ScaleAdvisor.js +0 -442
  356. package/src/services/Service.js +0 -195
  357. package/src/services/worker-bootstrap.js +0 -676
  358. package/src/templates/auth-service.js +0 -65
  359. package/src/templates/identity-service.js +0 -75
@@ -0,0 +1,694 @@
1
+ /**
2
+ * Ingress Protection
3
+ *
4
+ * Multiple layers of defense between the internet and your services.
5
+ * Each layer catches different failure modes:
6
+ *
7
+ * ┌─────────────────────────────────────────────────────────────┐
8
+ * │ INTERNET │
9
+ * └───────────────────────────┬─────────────────────────────────┘
10
+ * ▼
11
+ * ┌─────────────────────────────────────────────────────────────┐
12
+ * │ Layer 1: CONNECTION LIMITER │
13
+ * │ Total open connections capped at N (e.g. 10,000). │
14
+ * │ New connections get TCP RST when full. │
15
+ * │ Prevents file descriptor exhaustion. │
16
+ * └───────────────────────────┬─────────────────────────────────┘
17
+ * ▼
18
+ * ┌─────────────────────────────────────────────────────────────┐
19
+ * │ Layer 2: RATE LIMITER (per client) │
20
+ * │ Token bucket per IP / API key. Burst-friendly. │
21
+ * │ Returns 429 Too Many Requests when exceeded. │
22
+ * │ Prevents single client from consuming all capacity. │
23
+ * └───────────────────────────┬─────────────────────────────────┘
24
+ * ▼
25
+ * ┌─────────────────────────────────────────────────────────────┐
26
+ * │ Layer 3: GLOBAL LOAD SHEDDER │
27
+ * │ Monitors gateway CPU + event loop lag. │
28
+ * │ When overloaded, sheds low-priority requests (503). │
29
+ * │ High-priority requests (health checks, auth) pass through. │
30
+ * │ Prevents cascade failure in the gateway itself. │
31
+ * └───────────────────────────┬─────────────────────────────────┘
32
+ * ▼
33
+ * ┌─────────────────────────────────────────────────────────────┐
34
+ * │ Layer 4: ADAPTIVE CONCURRENCY (per service) │
35
+ * │ Limits in-flight requests to each downstream service. │
36
+ * │ Dynamically adjusts limit based on observed latency. │
37
+ * │ When a service slows down, we send FEWER requests to it. │
38
+ * │ Returns 503 when the service's concurrency window is full. │
39
+ * │ Prevents a slow service from consuming all gateway threads.│
40
+ * └───────────────────────────┬─────────────────────────────────┘
41
+ * ▼
42
+ * ┌─────────────────────────────────────────────────────────────┐
43
+ * │ SERVICE (users, billing, etc.) │
44
+ * └─────────────────────────────────────────────────────────────┘
45
+ *
46
+ * Usage in a gateway service:
47
+ *
48
+ * import { IngressProtection } from 'threadforge/ingress';
49
+ *
50
+ * export default class GatewayService extends Service {
51
+ * async onStart(ctx) {
52
+ * this.ingress = new IngressProtection({
53
+ * maxConnections: 10000,
54
+ * rateLimit: { windowMs: 60000, maxRequests: 100 },
55
+ * loadShedding: { eventLoopThresholdMs: 100 },
56
+ * services: {
57
+ * users: { maxConcurrent: 200 },
58
+ * billing: { maxConcurrent: 50 },
59
+ * notifications: { maxConcurrent: 100 },
60
+ * },
61
+ * });
62
+ *
63
+ * // Apply as middleware
64
+ * ctx.router.use(this.ingress.middleware());
65
+ * }
66
+ * }
67
+ */
68
+ import { EventEmitter } from "node:events";
69
+ import { isPrivateNetwork } from "./ForgeContext.js";
70
+ import { isTrustedProxy } from "./network-utils.js";
71
+ // ─── Rate Limiter (Token Bucket) ────────────────────────────
72
+ /**
73
+ * Per-client rate limiter using the token bucket algorithm.
74
+ *
75
+ * Token bucket is burst-friendly: a client that hasn't made
76
+ * requests in a while accumulates tokens and can burst.
77
+ * A client that's been hammering the API runs out of tokens
78
+ * and gets 429'd.
79
+ *
80
+ * Why token bucket instead of sliding window:
81
+ * - Allows natural burst patterns (page load = 10 requests at once)
82
+ * - Smoother than fixed windows (no "thundering herd" at window reset)
83
+ * - O(1) per request (no sorted sets or sliding counters)
84
+ */
85
+ export class RateLimiter {
86
+ maxTokens;
87
+ refillRate;
88
+ windowMs;
89
+ keyExtractor;
90
+ buckets;
91
+ _cleanupTimer;
92
+ constructor(options = {}) {
93
+ this.maxTokens = options.maxTokens ?? 100;
94
+ this.refillRate = options.refillRate ?? (options.maxRequests ?? 100) / 60;
95
+ this.windowMs = options.windowMs ?? 60000;
96
+ this.keyExtractor = options.keyExtractor ?? defaultKeyExtractor;
97
+ this.buckets = new Map();
98
+ // Periodically clean stale buckets
99
+ this._cleanupTimer = setInterval(() => this._cleanup(), this.windowMs);
100
+ this._cleanupTimer.unref();
101
+ }
102
+ /**
103
+ * Check if a request is allowed.
104
+ */
105
+ check(req) {
106
+ const key = this.keyExtractor(req);
107
+ const now = Date.now();
108
+ let bucket = this.buckets.get(key);
109
+ if (!bucket) {
110
+ bucket = { tokens: this.maxTokens, lastRefill: now };
111
+ this.buckets.set(key, bucket);
112
+ }
113
+ // Refill tokens based on elapsed time
114
+ const elapsed = (now - bucket.lastRefill) / 1000;
115
+ bucket.tokens = Math.min(this.maxTokens, bucket.tokens + elapsed * this.refillRate);
116
+ bucket.lastRefill = now;
117
+ if (bucket.tokens >= 1) {
118
+ bucket.tokens -= 1;
119
+ return {
120
+ allowed: true,
121
+ remaining: Math.floor(bucket.tokens),
122
+ };
123
+ }
124
+ // Rate limited — calculate retry-after
125
+ const tokensNeeded = 1 - bucket.tokens;
126
+ const retryAfterMs = Math.ceil((tokensNeeded / this.refillRate) * 1000);
127
+ return {
128
+ allowed: false,
129
+ remaining: 0,
130
+ retryAfter: retryAfterMs,
131
+ };
132
+ }
133
+ _cleanup() {
134
+ const staleThreshold = Date.now() - this.windowMs * 2;
135
+ for (const [key, bucket] of this.buckets) {
136
+ if (bucket.lastRefill < staleThreshold) {
137
+ this.buckets.delete(key);
138
+ }
139
+ }
140
+ }
141
+ get stats() {
142
+ return {
143
+ activeClients: this.buckets.size,
144
+ maxTokens: this.maxTokens,
145
+ refillRate: this.refillRate,
146
+ };
147
+ }
148
+ stop() {
149
+ if (this._cleanupTimer)
150
+ clearInterval(this._cleanupTimer);
151
+ }
152
+ }
153
+ function isValidIP(str) {
154
+ if (!str)
155
+ return false;
156
+ // IPv4
157
+ if (/^(\d{1,3}\.){3}\d{1,3}$/.test(str)) {
158
+ return str.split(".").every((p) => {
159
+ const n = parseInt(p, 10);
160
+ return n >= 0 && n <= 255;
161
+ });
162
+ }
163
+ // IPv6 (basic check)
164
+ if (/^[0-9a-fA-F:]+$/.test(str) && str.includes(":"))
165
+ return true;
166
+ return false;
167
+ }
168
+ function defaultKeyExtractor(req) {
169
+ const rawAddr = req.socket?.remoteAddress;
170
+ const remoteAddr = rawAddr ?? `unknown_${Math.random().toString(36).slice(2, 10)}`;
171
+ // H1: Only trust X-Forwarded-For from trusted proxies
172
+ // If FORGE_TRUSTED_PROXIES is set, only those addresses are trusted.
173
+ // Otherwise, fall back to trusting private networks for backwards compatibility.
174
+ const trusted = process.env.FORGE_TRUSTED_PROXIES;
175
+ const isProxyTrusted = trusted ? isTrustedProxy(remoteAddr) : rawAddr != null && isPrivateNetwork(remoteAddr);
176
+ if (isProxyTrusted) {
177
+ const xff = req.headers?.["x-forwarded-for"];
178
+ if (xff) {
179
+ const xffStr = Array.isArray(xff) ? xff.join(",") : xff;
180
+ const ips = xffStr
181
+ .split(",")
182
+ .slice(0, 10)
183
+ .map((ip) => ip.trim());
184
+ // Walk right-to-left to find first untrusted IP (the real client)
185
+ for (let i = ips.length - 1; i >= 0; i--) {
186
+ if (!isValidIP(ips[i]))
187
+ continue;
188
+ const ipTrusted = trusted ? isTrustedProxy(ips[i]) : isPrivateNetwork(ips[i]);
189
+ if (ips[i] && !ipTrusted) {
190
+ return ips[i];
191
+ }
192
+ }
193
+ }
194
+ // Fallback to X-Real-IP (commonly set by nginx)
195
+ const realIp = req.headers?.["x-real-ip"];
196
+ const realIpStr = Array.isArray(realIp) ? realIp[0] : realIp;
197
+ if (realIpStr && isValidIP(realIpStr)) {
198
+ return realIpStr;
199
+ }
200
+ return remoteAddr;
201
+ }
202
+ // Direct connection or no trusted proxies configured — use direct remote address
203
+ return remoteAddr;
204
+ }
205
+ // ─── Load Shedder ───────────────────────────────────────────
206
+ /**
207
+ * Global load shedder. Monitors the gateway process health and
208
+ * drops requests when overloaded.
209
+ *
210
+ * Monitors two signals:
211
+ * 1. Event loop lag — if the event loop is delayed by > threshold,
212
+ * the process is CPU-starved. Start shedding.
213
+ * 2. Active requests — if we have too many in-flight, shed.
214
+ *
215
+ * Shedding is priority-based:
216
+ * - CRITICAL: health checks, auth — never shed
217
+ * - HIGH: payment webhooks — shed only at extreme load
218
+ * - NORMAL: regular API calls — shed when overloaded
219
+ * - LOW: analytics, logging — shed first
220
+ */
221
+ export class LoadShedder {
222
+ eventLoopThresholdMs;
223
+ maxActiveRequests;
224
+ checkIntervalMs;
225
+ priorityRules;
226
+ activeRequests;
227
+ eventLoopLag;
228
+ shedThresholds;
229
+ _lastCheck;
230
+ _lagTimerActive;
231
+ _lagTimer;
232
+ constructor(options = {}) {
233
+ this.eventLoopThresholdMs = options.eventLoopThresholdMs ?? 100;
234
+ this.maxActiveRequests = options.maxActiveRequests ?? 5000;
235
+ this.checkIntervalMs = options.checkIntervalMs ?? 500;
236
+ // Default priority rules
237
+ this.priorityRules = [
238
+ { pattern: /^\/(health|ready|live)/, priority: "critical" },
239
+ { pattern: /^\/auth(\/|$)|^\/login(\/|$)|^\/oauth(\/|$)/, priority: "critical" },
240
+ { pattern: /^\/webhook(\/|$)|^\/stripe(\/|$)/, priority: "high" },
241
+ { pattern: /^\/api\//, priority: "normal" },
242
+ { pattern: /^\/analytics|^\/telemetry/, priority: "low" },
243
+ ...(options.priorityRules ?? []),
244
+ ];
245
+ this.activeRequests = 0;
246
+ this.eventLoopLag = 0;
247
+ this._lastCheck = Date.now();
248
+ // Shedding thresholds by priority
249
+ this.shedThresholds = {
250
+ low: { lagMs: 50, loadRatio: 0.6 },
251
+ normal: { lagMs: 100, loadRatio: 0.8 },
252
+ high: { lagMs: 200, loadRatio: 0.95 },
253
+ critical: { lagMs: Infinity, loadRatio: Infinity }, // never shed
254
+ };
255
+ // Start event loop lag measurement using recursive setTimeout to prevent drift
256
+ this._lagTimerActive = true;
257
+ const measureLag = () => {
258
+ if (!this._lagTimerActive)
259
+ return;
260
+ const now = Date.now();
261
+ const expected = this.checkIntervalMs;
262
+ const actual = now - this._lastCheck;
263
+ this.eventLoopLag = Math.max(0, actual - expected);
264
+ this._lastCheck = now;
265
+ this._lagTimer = setTimeout(measureLag, this.checkIntervalMs);
266
+ this._lagTimer.unref();
267
+ };
268
+ this._lagTimer = setTimeout(measureLag, this.checkIntervalMs);
269
+ this._lagTimer.unref();
270
+ }
271
+ /**
272
+ * Should this request be accepted or shed?
273
+ */
274
+ shouldAccept(req) {
275
+ const priority = this._getPriority(req);
276
+ const threshold = this.shedThresholds[priority];
277
+ const loadRatio = this.activeRequests / this.maxActiveRequests;
278
+ // Check event loop lag
279
+ if (this.eventLoopLag > threshold.lagMs) {
280
+ return {
281
+ accept: false,
282
+ reason: `Event loop lag ${this.eventLoopLag}ms exceeds ${threshold.lagMs}ms threshold for ${priority} priority`,
283
+ priority,
284
+ };
285
+ }
286
+ // Check active request count
287
+ if (loadRatio > threshold.loadRatio) {
288
+ return {
289
+ accept: false,
290
+ reason: `Load at ${(loadRatio * 100).toFixed(0)}% exceeds ${(threshold.loadRatio * 100).toFixed(0)}% threshold for ${priority} priority`,
291
+ priority,
292
+ };
293
+ }
294
+ return { accept: true, priority };
295
+ }
296
+ _getPriority(req) {
297
+ const url = req.url ?? "/";
298
+ for (const rule of this.priorityRules) {
299
+ if (rule.pattern.test(url)) {
300
+ return rule.priority;
301
+ }
302
+ }
303
+ return "normal";
304
+ }
305
+ trackRequest() {
306
+ this.activeRequests++;
307
+ return () => {
308
+ this.activeRequests--;
309
+ };
310
+ }
311
+ get stats() {
312
+ return {
313
+ activeRequests: this.activeRequests,
314
+ maxActiveRequests: this.maxActiveRequests,
315
+ eventLoopLagMs: this.eventLoopLag,
316
+ loadPercent: ((this.activeRequests / this.maxActiveRequests) * 100).toFixed(1),
317
+ shedding: this.eventLoopLag > this.shedThresholds.normal.lagMs || this.activeRequests > this.maxActiveRequests * 0.8,
318
+ };
319
+ }
320
+ stop() {
321
+ this._lagTimerActive = false;
322
+ if (this._lagTimer)
323
+ clearTimeout(this._lagTimer);
324
+ }
325
+ }
326
+ // ─── Adaptive Concurrency Limiter ───────────────────────────
327
+ /**
328
+ * Adaptive Concurrency Limiter (per downstream service)
329
+ *
330
+ * The core problem: how many concurrent requests should we send
331
+ * to the users service? Too few = wasted capacity. Too many =
332
+ * overwhelm the service, latency spikes, cascade failure.
333
+ *
334
+ * Fixed limits are fragile — they're either too conservative
335
+ * (wasting capacity during normal load) or too aggressive
336
+ * (causing failures during peak).
337
+ *
338
+ * Instead, we use the Vegas algorithm (from TCP congestion control):
339
+ *
340
+ * 1. Start with a small concurrency window (e.g., 10)
341
+ * 2. Measure round-trip latency for each request
342
+ * 3. Track the minimum observed latency (the "no-load" baseline)
343
+ * 4. If current latency ≈ baseline → increase the window
344
+ * (service has capacity, give it more)
345
+ * 5. If current latency >> baseline → decrease the window
346
+ * (service is queuing, back off)
347
+ *
348
+ * This automatically adapts to:
349
+ * - Fast services (high window, more concurrent requests)
350
+ * - Slow services (low window, fewer concurrent requests)
351
+ * - Services that degrade under load (window shrinks as latency rises)
352
+ * - Services that recover (window grows as latency drops)
353
+ *
354
+ * Inspired by Netflix's concurrency-limits library.
355
+ */
356
+ export class AdaptiveConcurrencyLimiter {
357
+ serviceName;
358
+ limit;
359
+ minLimit;
360
+ maxLimit;
361
+ smoothing;
362
+ tolerance;
363
+ inFlight;
364
+ minLatency;
365
+ smoothedLatency;
366
+ totalRequests;
367
+ totalRejected;
368
+ totalSuccesses;
369
+ totalFailures;
370
+ _latencies;
371
+ _latencyIdx;
372
+ _latencyCount;
373
+ _minLatencyWindow;
374
+ _minLatencyWindowIdx;
375
+ _minLatencyWindowCount;
376
+ _minLatencyObservations;
377
+ constructor(serviceName, options = {}) {
378
+ this.serviceName = serviceName;
379
+ this.limit = options.initialLimit ?? 20;
380
+ this.minLimit = options.minLimit ?? 5;
381
+ this.maxLimit = options.maxLimit ?? 500;
382
+ this.smoothing = options.smoothing ?? 0.2;
383
+ this.tolerance = options.tolerance ?? 2.0;
384
+ this.inFlight = 0;
385
+ this.minLatency = Infinity;
386
+ this.smoothedLatency = 0;
387
+ // Metrics
388
+ this.totalRequests = 0;
389
+ this.totalRejected = 0;
390
+ this.totalSuccesses = 0;
391
+ this.totalFailures = 0;
392
+ // Latency percentiles (ring buffer)
393
+ this._latencies = new Float64Array(1000);
394
+ this._latencyIdx = 0;
395
+ this._latencyCount = 0;
396
+ // Windowed minimum latency reset
397
+ this._minLatencyWindow = new Float64Array(1000);
398
+ this._minLatencyWindowIdx = 0;
399
+ this._minLatencyWindowCount = 0;
400
+ this._minLatencyObservations = 0;
401
+ }
402
+ /**
403
+ * Try to acquire a concurrency slot.
404
+ */
405
+ tryAcquire() {
406
+ this.totalRequests++;
407
+ if (this.inFlight >= this.limit) {
408
+ this.totalRejected++;
409
+ return { acquired: false };
410
+ }
411
+ this.inFlight++;
412
+ const startTime = performance.now();
413
+ const release = (success = true) => {
414
+ this.inFlight--;
415
+ const latency = performance.now() - startTime;
416
+ this._recordLatency(latency, success);
417
+ };
418
+ return { acquired: true, release };
419
+ }
420
+ _recordLatency(latencyMs, success) {
421
+ if (success) {
422
+ this.totalSuccesses++;
423
+ // Track minimum latency with windowed reset
424
+ this._minLatencyWindow[this._minLatencyWindowIdx % this._minLatencyWindow.length] = latencyMs;
425
+ this._minLatencyWindowIdx++;
426
+ this._minLatencyWindowCount = Math.min(this._minLatencyWindowCount + 1, this._minLatencyWindow.length);
427
+ this._minLatencyObservations++;
428
+ if (latencyMs < this.minLatency) {
429
+ this.minLatency = latencyMs;
430
+ }
431
+ // Every 1000 observations, recalculate minLatency from the window
432
+ if (this._minLatencyObservations >= 1000) {
433
+ this._minLatencyObservations = 0;
434
+ let windowMin = Infinity;
435
+ for (let i = 0; i < this._minLatencyWindowCount; i++) {
436
+ if (this._minLatencyWindow[i] < windowMin) {
437
+ windowMin = this._minLatencyWindow[i];
438
+ }
439
+ }
440
+ this.minLatency = windowMin;
441
+ }
442
+ // Exponential moving average
443
+ if (this.smoothedLatency === 0) {
444
+ this.smoothedLatency = latencyMs;
445
+ }
446
+ else {
447
+ this.smoothedLatency = this.smoothing * latencyMs + (1 - this.smoothing) * this.smoothedLatency;
448
+ }
449
+ // Store in ring buffer for percentile calculation
450
+ this._latencies[this._latencyIdx % this._latencies.length] = latencyMs;
451
+ this._latencyIdx++;
452
+ this._latencyCount = Math.min(this._latencyCount + 1, this._latencies.length);
453
+ // Adjust limit using Vegas algorithm
454
+ this._adjustLimit();
455
+ }
456
+ else {
457
+ this.totalFailures++;
458
+ // On failure, aggressively reduce limit
459
+ this.limit = Math.max(this.minLimit, Math.floor(this.limit * 0.75));
460
+ }
461
+ }
462
+ _adjustLimit() {
463
+ if (this.minLatency === Infinity || this.smoothedLatency === 0)
464
+ return;
465
+ const ratio = this.smoothedLatency / this.minLatency;
466
+ if (ratio < this.tolerance) {
467
+ // Latency is close to baseline — we have room, increase slowly
468
+ // The gradient determines how aggressively we grow
469
+ const gradient = Math.max(0, (this.tolerance - ratio) / this.tolerance);
470
+ const increase = Math.ceil(gradient * 2);
471
+ this.limit = Math.min(this.maxLimit, this.limit + increase);
472
+ }
473
+ else {
474
+ // Latency is elevated — service is queuing, reduce
475
+ // Reduce proportionally to how far over the tolerance we are
476
+ const overshoot = ratio / this.tolerance;
477
+ const decrease = Math.ceil(overshoot);
478
+ this.limit = Math.max(this.minLimit, this.limit - decrease);
479
+ }
480
+ }
481
+ get stats() {
482
+ return {
483
+ service: this.serviceName,
484
+ currentLimit: this.limit,
485
+ inFlight: this.inFlight,
486
+ utilization: `${((this.inFlight / this.limit) * 100).toFixed(0)}%`,
487
+ minLatencyMs: this.minLatency === Infinity ? null : this.minLatency.toFixed(2),
488
+ smoothedLatencyMs: this.smoothedLatency.toFixed(2),
489
+ p99LatencyMs: this._getPercentile(0.99)?.toFixed(2) ?? null,
490
+ totalRequests: this.totalRequests,
491
+ totalRejected: this.totalRejected,
492
+ rejectionRate: `${((this.totalRejected / Math.max(1, this.totalRequests)) * 100).toFixed(1)}%`,
493
+ };
494
+ }
495
+ _getPercentile(p) {
496
+ if (this._latencyCount === 0)
497
+ return null;
498
+ const sorted = Array.from(this._latencies.slice(0, this._latencyCount)).sort((a, b) => a - b);
499
+ const idx = Math.floor(sorted.length * p);
500
+ return sorted[idx];
501
+ }
502
+ }
503
+ // ─── IngressProtection (combines all layers) ────────────────
504
+ /**
505
+ * Combined ingress protection middleware.
506
+ *
507
+ * Applies all layers in order: connection limit → rate limit →
508
+ * load shedding → route → adaptive concurrency → service.
509
+ */
510
+ export class IngressProtection extends EventEmitter {
511
+ maxConnections;
512
+ activeConnections;
513
+ rateLimiter;
514
+ loadShedder;
515
+ serviceLimiters;
516
+ _metricsInterval;
517
+ constructor(options = {}) {
518
+ super();
519
+ this.maxConnections = options.maxConnections ?? 10000;
520
+ this.activeConnections = 0;
521
+ this.rateLimiter = new RateLimiter(options.rateLimit ?? {});
522
+ this.loadShedder = new LoadShedder(options.loadShedding ?? {});
523
+ // Per-service adaptive concurrency limiters
524
+ this.serviceLimiters = new Map();
525
+ if (options.services) {
526
+ for (const [name, config] of Object.entries(options.services)) {
527
+ this.serviceLimiters.set(name, new AdaptiveConcurrencyLimiter(name, config));
528
+ }
529
+ }
530
+ this._metricsInterval = setInterval(() => {
531
+ this.emit("metrics", this.stats);
532
+ }, 10000);
533
+ this._metricsInterval.unref();
534
+ }
535
+ /**
536
+ * Get or create a concurrency limiter for a service.
537
+ */
538
+ getServiceLimiter(serviceName) {
539
+ if (!this.serviceLimiters.has(serviceName)) {
540
+ this.serviceLimiters.set(serviceName, new AdaptiveConcurrencyLimiter(serviceName));
541
+ }
542
+ return this.serviceLimiters.get(serviceName);
543
+ }
544
+ /**
545
+ * Returns a middleware function for the ForgeContext router.
546
+ *
547
+ * Usage:
548
+ * ctx.router.use(this.ingress.middleware());
549
+ */
550
+ middleware() {
551
+ return async (req, res, next) => {
552
+ // Layer 1: Connection limit
553
+ if (this.activeConnections >= this.maxConnections) {
554
+ res.writeHead(503, { "Retry-After": "5" });
555
+ res.end(JSON.stringify({ error: "Server at connection capacity" }));
556
+ return;
557
+ }
558
+ this.activeConnections++;
559
+ let connectionCounted = true;
560
+ req.on("close", () => {
561
+ if (connectionCounted) {
562
+ this.activeConnections--;
563
+ connectionCounted = false;
564
+ }
565
+ });
566
+ // Layer 2: Rate limiting
567
+ const rateCheck = this.rateLimiter.check(req);
568
+ if (!rateCheck.allowed) {
569
+ const retryAfterSec = Math.ceil(rateCheck.retryAfter / 1000);
570
+ res.writeHead(429, {
571
+ "Retry-After": String(retryAfterSec),
572
+ "X-RateLimit-Remaining": "0",
573
+ "X-RateLimit-Reset": String(retryAfterSec),
574
+ });
575
+ res.end(JSON.stringify({
576
+ error: "Rate limit exceeded",
577
+ retryAfter: retryAfterSec,
578
+ }));
579
+ if (connectionCounted) {
580
+ connectionCounted = false;
581
+ this.activeConnections--;
582
+ }
583
+ return;
584
+ }
585
+ // Layer 3: Load shedding
586
+ const shedCheck = this.loadShedder.shouldAccept(req);
587
+ if (!shedCheck.accept) {
588
+ res.writeHead(503, { "Retry-After": "1" });
589
+ // Don't expose internal load/lag details to clients
590
+ res.end(JSON.stringify({
591
+ error: "Service temporarily overloaded",
592
+ }));
593
+ if (connectionCounted) {
594
+ connectionCounted = false;
595
+ this.activeConnections--;
596
+ }
597
+ return;
598
+ }
599
+ // Track active request
600
+ const releaseRequest = this.loadShedder.trackRequest();
601
+ // Add headers
602
+ res.setHeader("X-RateLimit-Remaining", String(rateCheck.remaining));
603
+ // Track completion by wrapping res.end — also hook close for premature disconnects
604
+ let requestReleased = false;
605
+ const doRelease = () => {
606
+ if (!requestReleased) {
607
+ requestReleased = true;
608
+ releaseRequest();
609
+ // Clean up close listener to avoid accumulation
610
+ if (typeof res.removeListener === "function") {
611
+ res.removeListener("close", doRelease);
612
+ }
613
+ }
614
+ };
615
+ const origEnd = res.end.bind(res);
616
+ res.end = (...args) => {
617
+ doRelease();
618
+ return origEnd(...args);
619
+ };
620
+ // If client disconnects before res.end(), still release
621
+ if (typeof res.on === "function")
622
+ res.on("close", doRelease);
623
+ // Pass through to router
624
+ if (next)
625
+ next();
626
+ };
627
+ }
628
+ /**
629
+ * Wrap a proxy call with adaptive concurrency limiting.
630
+ *
631
+ * Usage in the gateway:
632
+ * const user = await this.ingress.withConcurrencyLimit('users', () =>
633
+ * this.users.getUser(id)
634
+ * );
635
+ *
636
+ * Or in the proxy interceptor layer:
637
+ * The ServiceProxy automatically calls this if ingress is configured.
638
+ */
639
+ async withConcurrencyLimit(serviceName, fn) {
640
+ const limiter = this.getServiceLimiter(serviceName);
641
+ const slot = limiter.tryAcquire();
642
+ if (!slot.acquired) {
643
+ const err = new Error(`Service "${serviceName}" at concurrency limit (${limiter.limit}). ` +
644
+ `${limiter.inFlight} requests in flight.`);
645
+ err.code = "CONCURRENCY_LIMIT";
646
+ err.statusCode = 503;
647
+ throw err;
648
+ }
649
+ try {
650
+ const result = await fn();
651
+ slot.release(true);
652
+ return result;
653
+ }
654
+ catch (err) {
655
+ slot.release(false);
656
+ throw err;
657
+ }
658
+ }
659
+ get stats() {
660
+ const serviceStats = {};
661
+ for (const [name, limiter] of this.serviceLimiters) {
662
+ serviceStats[name] = limiter.stats;
663
+ }
664
+ return {
665
+ connections: {
666
+ active: this.activeConnections,
667
+ max: this.maxConnections,
668
+ },
669
+ rateLimiter: this.rateLimiter.stats,
670
+ loadShedder: this.loadShedder.stats,
671
+ services: serviceStats,
672
+ };
673
+ }
674
+ /**
675
+ * HTTP handler for ingress stats endpoint.
676
+ * Mount on the metrics server:
677
+ * GET /ingress → full stats
678
+ */
679
+ httpHandler(req, res) {
680
+ if (req.url === "/ingress") {
681
+ res.writeHead(200, { "Content-Type": "application/json" });
682
+ res.end(JSON.stringify(this.stats, null, 2));
683
+ return true;
684
+ }
685
+ return false;
686
+ }
687
+ stop() {
688
+ this.rateLimiter.stop();
689
+ this.loadShedder.stop();
690
+ if (this._metricsInterval)
691
+ clearInterval(this._metricsInterval);
692
+ }
693
+ }
694
+ //# sourceMappingURL=Ingress.js.map