@absolutejs/sync 2.7.1 → 2.9.0

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/index.js CHANGED
@@ -159,6 +159,7 @@ var createConnectionBroker = (options) => {
159
159
  throw new RangeError(`createConnectionBroker requires maxTotal > 0 (got ${options.maxTotal})`);
160
160
  }
161
161
  const now = options.now ?? Date.now;
162
+ const affinity = options.affinity ?? "shared";
162
163
  const tracer = tracerOrNoop(options.tracerProvider, "@absolutejs/sync");
163
164
  const idle = [];
164
165
  const queue = [];
@@ -215,8 +216,20 @@ var createConnectionBroker = (options) => {
215
216
  resolveDrain?.();
216
217
  }
217
218
  };
218
- const acquireConn = async () => {
219
- let entry = idle.pop();
219
+ const takeIdle = (tenant) => {
220
+ if (affinity === "shared")
221
+ return idle.pop();
222
+ for (let index = idle.length - 1;index >= 0; index -= 1) {
223
+ const entry = idle[index];
224
+ if (entry?.tenant !== tenant)
225
+ continue;
226
+ idle.splice(index, 1);
227
+ return entry;
228
+ }
229
+ return;
230
+ };
231
+ const acquireConn = async (tenant) => {
232
+ let entry = takeIdle(tenant);
220
233
  while (entry !== undefined) {
221
234
  if (options.validate === undefined)
222
235
  return entry.conn;
@@ -230,10 +243,17 @@ var createConnectionBroker = (options) => {
230
243
  return entry.conn;
231
244
  cumulative.validationFailures += 1;
232
245
  await destroyConn(entry.conn);
233
- entry = idle.pop();
246
+ entry = takeIdle(tenant);
234
247
  }
248
+ if (affinity === "tenant")
249
+ while (inUse + idle.length > options.maxTotal) {
250
+ const displaced = idle.shift();
251
+ if (displaced === undefined)
252
+ break;
253
+ await destroyConn(displaced.conn);
254
+ }
235
255
  try {
236
- const conn = await options.create();
256
+ const conn = await options.create(tenant);
237
257
  cumulative.created += 1;
238
258
  return conn;
239
259
  } catch (error) {
@@ -263,7 +283,7 @@ var createConnectionBroker = (options) => {
263
283
  if (disposed) {
264
284
  destroyConn(conn);
265
285
  } else {
266
- idle.push({ conn, idleSince: now() });
286
+ idle.push({ conn, idleSince: now(), tenant });
267
287
  }
268
288
  unreserve(tenant);
269
289
  pump();
@@ -272,7 +292,7 @@ var createConnectionBroker = (options) => {
272
292
  };
273
293
  const grantTo = async (waiter) => {
274
294
  try {
275
- const conn = await acquireConn();
295
+ const conn = await acquireConn(waiter.tenant);
276
296
  waiter.span.setAttribute(QUEUE_WAIT_ATTR, now() - waiter.enqueuedAt);
277
297
  waiter.span.end();
278
298
  waiter.resolve(makeLease(waiter.tenant, conn));
@@ -323,7 +343,7 @@ var createConnectionBroker = (options) => {
323
343
  if (canGrant(tenant)) {
324
344
  reserve(tenant);
325
345
  try {
326
- const conn = await acquireConn();
346
+ const conn = await acquireConn(tenant);
327
347
  span.setAttribute(QUEUE_WAIT_ATTR, now() - startedAt);
328
348
  span.end();
329
349
  return makeLease(tenant, conn);
@@ -3782,5 +3802,5 @@ export {
3782
3802
  ConnectionBrokerDrainedError
3783
3803
  };
3784
3804
 
3785
- //# debugId=422565C3498D831B64756E2164756E21
3805
+ //# debugId=456583ECD822321E64756E2164756E21
3786
3806
  //# sourceMappingURL=index.js.map