|
| 1 | +# AsyncLocalStorage Migration Plan |
| 2 | + |
| 3 | +This document outlines the minimal changes needed to switch from `cls-hooked` / `express-http-context` to Node.js built-in `AsyncLocalStorage`. |
| 4 | + |
| 5 | +## Summary |
| 6 | + |
| 7 | +- **Files to modify:** 2 |
| 8 | +- **Lines changed:** ~20 |
| 9 | +- **No changes needed to:** Connectors, controllers, handlers, resolvers, tests |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## File 1: `src/util/cls.js` |
| 14 | + |
| 15 | +### BEFORE (current): |
| 16 | +```javascript |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +const httpContext = require('express-http-context'); |
| 20 | + |
| 21 | +const P = require('bluebird'); |
| 22 | +const sequelize = require('sequelize'); |
| 23 | +const clsBluebird = require('cls-bluebird'); |
| 24 | +clsBluebird(httpContext.ns, P); |
| 25 | +sequelize.useCLS(httpContext.ns); |
| 26 | + |
| 27 | +exports.middleware = function (req, res, next) { |
| 28 | + httpContext.set('req', req); |
| 29 | + next(); |
| 30 | +}; |
| 31 | + |
| 32 | +exports.getReq = function () { |
| 33 | + return httpContext.get('req'); |
| 34 | +}; |
| 35 | + |
| 36 | +exports.patchMiddleware = function (fn) { |
| 37 | + return function (req, res, next) { |
| 38 | + return fn(req, res, httpContext.ns.bind(next)); |
| 39 | + }; |
| 40 | +}; |
| 41 | +``` |
| 42 | + |
| 43 | +### AFTER (AsyncLocalStorage): |
| 44 | +```javascript |
| 45 | +'use strict'; |
| 46 | + |
| 47 | +const { AsyncLocalStorage } = require('async_hooks'); |
| 48 | + |
| 49 | +const asyncLocalStorage = new AsyncLocalStorage(); |
| 50 | + |
| 51 | +// NOTE: Removed sequelize.useCLS() - not needed because we pass transactions explicitly |
| 52 | +// NOTE: Removed cls-bluebird - not needed with AsyncLocalStorage |
| 53 | + |
| 54 | +exports.middleware = function (req, res, next) { |
| 55 | + asyncLocalStorage.run({ req }, next); |
| 56 | +}; |
| 57 | + |
| 58 | +exports.getReq = function () { |
| 59 | + const store = asyncLocalStorage.getStore(); |
| 60 | + return store ? store.req : undefined; |
| 61 | +}; |
| 62 | + |
| 63 | +exports.patchMiddleware = function (fn) { |
| 64 | + // AsyncLocalStorage automatically propagates context - no manual binding needed |
| 65 | + return fn; |
| 66 | +}; |
| 67 | +``` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## File 2: `src/routes.js` |
| 72 | + |
| 73 | +### BEFORE (current): |
| 74 | +```javascript |
| 75 | +// Line 8 |
| 76 | +const httpContext = require('express-http-context'); |
| 77 | + |
| 78 | +// Lines 66-67 |
| 79 | +app.use(httpContext.middleware); |
| 80 | +app.use(cls.middleware); |
| 81 | +``` |
| 82 | + |
| 83 | +### AFTER: |
| 84 | +```javascript |
| 85 | +// Line 8 - REMOVE THIS LINE |
| 86 | +// const httpContext = require('express-http-context'); |
| 87 | + |
| 88 | +// Lines 66-67 - REMOVE httpContext.middleware, keep cls.middleware |
| 89 | +// app.use(httpContext.middleware); // DELETE THIS LINE |
| 90 | +app.use(cls.middleware); // KEEP THIS LINE |
| 91 | +``` |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +## Packages that can be removed from package.json (optional, can do later): |
| 96 | + |
| 97 | +```json |
| 98 | +{ |
| 99 | + "dependencies": { |
| 100 | + "cls-bluebird": "...", // Can remove |
| 101 | + "express-http-context": "..." // Can remove |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +Note: Keep these packages for now until you verify everything works. Remove in a follow-up PR. |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## Why this works: |
| 111 | + |
| 112 | +1. **AsyncLocalStorage is built into Node.js** (since v12.17) - no external package needed |
| 113 | +2. **Works with native Promises** - compatible with axios |
| 114 | +3. **`cls.getReq()` still works** - same API, different implementation |
| 115 | +4. **Sequelize doesn't need CLS** - your code already passes `{transaction}` explicitly (see verification below) |
| 116 | +5. **No connector/controller changes needed** - they keep calling `this.getForwardedHeaders()` which calls `cls.getReq()` internally |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## Verification: Sequelize transactions are passed explicitly |
| 121 | + |
| 122 | +The `sequelize.useCLS()` feature auto-binds transactions to queries within a transaction callback. However, this codebase **does not rely on it** - all transactions are passed explicitly. |
| 123 | + |
| 124 | +### Transaction blocks and their operations: |
| 125 | + |
| 126 | +| Location | Operations | Transaction passed? | |
| 127 | +|----------|-----------|---------------------| |
| 128 | +| `exports.create` (L167) | `db.remediation.create`, `storeNewActions` | ✓ Yes | |
| 129 | +| `exports.patch` (L210) | `db.remediation.findOne`, `storeNewActions`, `.save` | ✓ Yes | |
| 130 | +| `exports.patchIssue` (L263) | `db.issue.findOne`, `.save`, `remediationUpdated` | ✓ Yes | |
| 131 | +| `findAndDestroy` (L313) | `findOne`, `.destroy`, `remediationUpdated` | ✓ Yes | |
| 132 | +| `findAllAndDestroy` (L334) | `findAll`, `.destroy`, `remediationUpdated` | ✓ Yes | |
| 133 | + |
| 134 | +### Helper functions that receive transaction: |
| 135 | + |
| 136 | +- `storeNewActions(remediation, add, transaction)` - all db operations pass `{transaction}` |
| 137 | +- `remediationUpdated(req, transaction)` - passes `{transaction}` to its update |
| 138 | + |
| 139 | +### Operations outside transactions (no transaction needed): |
| 140 | + |
| 141 | +These are single atomic operations that don't require transactional consistency: |
| 142 | + |
| 143 | +- `insertRHCPlaybookRun` - single create |
| 144 | +- `insertDispatcherRuns` - single bulkCreate |
| 145 | +- `updateDispatcherRuns` - single update |
| 146 | +- `storePlaybookDefinition` - single create |
| 147 | + |
| 148 | +### Files checked: |
| 149 | + |
| 150 | +- `src/remediations/controller.write.js` - all transaction blocks verified |
| 151 | +- `src/remediations/remediations.queries.js` - no transactions (single operations) |
| 152 | +- `src/generator/generator.controller.js` - no transactions (single operations) |
| 153 | +- `src/admin/admin.controller.js` - no transactions |
| 154 | + |
| 155 | +**Conclusion:** Removing `sequelize.useCLS()` is safe - it was never being relied upon |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +## Testing: |
| 160 | + |
| 161 | +After making these changes: |
| 162 | +1. Run the test suite: `npm test` |
| 163 | +2. Verify HTTP context is available in connectors (headers are forwarded correctly) |
| 164 | +3. Verify database transactions still work (they use explicit `{transaction}` already) |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +## Comparison with explicit `req` passing approach: |
| 169 | + |
| 170 | +| Approach | Files Changed | Complexity | |
| 171 | +|----------|---------------|------------| |
| 172 | +| AsyncLocalStorage | ~2 files | Simple | |
| 173 | +| Explicit `req` passing | ~50+ files | Complex but more explicit | |
| 174 | + |
| 175 | +Both approaches work. AsyncLocalStorage is simpler but keeps implicit context. |
| 176 | +Explicit `req` passing is more work but results in cleaner architecture. |
0 commit comments