|
1 | 1 | import { AssertionError, expect } from 'chai'; |
2 | 2 | import { EventEmitter } from 'events'; |
| 3 | +import { setImmediate } from 'timers'; |
3 | 4 |
|
4 | 5 | import { |
5 | 6 | AbstractCursor, |
@@ -628,20 +629,20 @@ export class EntitiesMap<E = Entity> extends Map<string, E> { |
628 | 629 | if (entity.client.awaitMinPoolSizeMS) { |
629 | 630 | if (client.topology?.s?.servers) { |
630 | 631 | const timeout = Timeout.expires(entity.client.awaitMinPoolSizeMS); |
631 | | - for (const server of client.topology.s.servers.values()) { |
632 | | - const pool = server.pool; |
633 | | - try { |
634 | | - await Promise.race([checkMinPoolSize(pool), timeout]); |
635 | | - } catch (error) { |
636 | | - if (TimeoutError.is(error)) { |
637 | | - throw new AssertionError( |
638 | | - `Timed out waiting for min pool size to be populated within ${entity.client.awaitMinPoolSizeMS}ms` |
639 | | - ); |
640 | | - } |
641 | | - throw error; |
642 | | - } finally { |
643 | | - timeout.clear(); |
| 632 | + const poolSizeChecks = client.topology.s.servers |
| 633 | + .values() |
| 634 | + .map(server => checkMinPoolSize(server.pool)); |
| 635 | + try { |
| 636 | + await Promise.race([Promise.allSettled(poolSizeChecks), timeout]); |
| 637 | + } catch (error) { |
| 638 | + if (TimeoutError.is(error)) { |
| 639 | + throw new AssertionError( |
| 640 | + `Timed out waiting for min pool size to be populated within ${entity.client.awaitMinPoolSizeMS}ms` |
| 641 | + ); |
644 | 642 | } |
| 643 | + throw error; |
| 644 | + } finally { |
| 645 | + timeout.clear(); |
645 | 646 | } |
646 | 647 | } |
647 | 648 | } |
@@ -747,9 +748,13 @@ export class EntitiesMap<E = Entity> extends Map<string, E> { |
747 | 748 |
|
748 | 749 | function checkMinPoolSize(pool: ConnectionPool): Promise<boolean> { |
749 | 750 | return new Promise(resolve => { |
750 | | - while (pool.options.minPoolSize < pool.totalConnectionCount) { |
751 | | - // Just looping until the min pool size is reached. |
752 | | - } |
753 | | - resolve(true); |
| 751 | + const checkSize = () => { |
| 752 | + if (pool.totalConnectionCount >= pool.options.minPoolSize) { |
| 753 | + resolve(true); |
| 754 | + } else { |
| 755 | + setImmediate(checkSize); |
| 756 | + } |
| 757 | + }; |
| 758 | + checkSize(); |
754 | 759 | }); |
755 | 760 | } |
0 commit comments