@zintrust/redis-rpc 2.8.3 → 2.9.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.
- package/dist/backend.js +34 -2
- package/package.json +1 -1
package/dist/backend.js
CHANGED
|
@@ -163,6 +163,36 @@ const createConnection = (state, extra = {}) => {
|
|
|
163
163
|
connection.once('end', () => state.connections.delete(connection));
|
|
164
164
|
return connection;
|
|
165
165
|
};
|
|
166
|
+
/**
|
|
167
|
+
* When the caller supplies a `payload.db` that differs from the default state's
|
|
168
|
+
* Redis DB, return (or lazily create) an isolated sub-state that targets that DB.
|
|
169
|
+
*
|
|
170
|
+
* Each sub-state has its own queues/queueEvents/eventLogs Maps so BullMQ Queue
|
|
171
|
+
* objects for different databases are never shared or confused. All sub-states
|
|
172
|
+
* share the top-level `connections` Set for unified shutdown cleanup.
|
|
173
|
+
*/
|
|
174
|
+
const resolveStateForDb = (state, dbStates, payload) => {
|
|
175
|
+
const requestedDb = typeof payload.db === 'number' && Number.isFinite(payload.db) ? payload.db : undefined;
|
|
176
|
+
// No override, or same as server default → use main state unchanged.
|
|
177
|
+
if (requestedDb === undefined || requestedDb === state.connectionOptions.db)
|
|
178
|
+
return state;
|
|
179
|
+
// Return existing derived state for this DB if already created.
|
|
180
|
+
const existing = dbStates.get(requestedDb);
|
|
181
|
+
if (existing)
|
|
182
|
+
return existing;
|
|
183
|
+
// Lazily create an isolated state for this DB index.
|
|
184
|
+
const derived = {
|
|
185
|
+
prefix: state.prefix,
|
|
186
|
+
connectionOptions: { ...state.connectionOptions, db: requestedDb },
|
|
187
|
+
queues: new Map(),
|
|
188
|
+
queueEvents: new Map(),
|
|
189
|
+
eventLogs: new Map(),
|
|
190
|
+
connections: state.connections, // shared — unified cleanup on close()
|
|
191
|
+
services: state.services,
|
|
192
|
+
};
|
|
193
|
+
dbStates.set(requestedDb, derived);
|
|
194
|
+
return derived;
|
|
195
|
+
};
|
|
166
196
|
const queueOptions = (state) => ({
|
|
167
197
|
connection: createConnection(state),
|
|
168
198
|
prefix: state.prefix,
|
|
@@ -725,6 +755,8 @@ export const createRedisRpcBackend = (options = {}) => {
|
|
|
725
755
|
connections: new Set(),
|
|
726
756
|
services: new Map(Object.entries(options.services ?? {})),
|
|
727
757
|
};
|
|
758
|
+
// Per-DB isolated states keyed by integer DB index.
|
|
759
|
+
const dbStates = new Map();
|
|
728
760
|
const backend = Object.freeze({
|
|
729
761
|
prefix: state.prefix,
|
|
730
762
|
dispatch: async (service, method, payload = {}) => {
|
|
@@ -732,11 +764,11 @@ export const createRedisRpcBackend = (options = {}) => {
|
|
|
732
764
|
const normalizedMethod = requireString(method, 'method');
|
|
733
765
|
const body = requireRecord(payload, 'payload');
|
|
734
766
|
if (normalizedService === 'queue' || normalizedService === 'bullmq')
|
|
735
|
-
return dispatchQueue(state, normalizedMethod, body);
|
|
767
|
+
return dispatchQueue(resolveStateForDb(state, dbStates, body), normalizedMethod, body);
|
|
736
768
|
if (normalizedService === 'worker')
|
|
737
769
|
return dispatchWorker(state, normalizedMethod, body);
|
|
738
770
|
if (normalizedService === 'queue-monitor')
|
|
739
|
-
return dispatchMonitor(state, normalizedMethod, body);
|
|
771
|
+
return dispatchMonitor(resolveStateForDb(state, dbStates, body), normalizedMethod, body);
|
|
740
772
|
if (normalizedService === 'redis')
|
|
741
773
|
return dispatchRedis(state, normalizedMethod, body);
|
|
742
774
|
return dispatchCustom(backend, state, normalizedService, normalizedMethod, body);
|
package/package.json
CHANGED