acquireConnection method
Get or create a connection from the pool.
Implementation
Future<PooledConnection?> acquireConnection(String relayUrl) async {
_ensurePoolExists(relayUrl);
final available = _availableConnections[relayUrl] ?? [];
if (available.isNotEmpty) {
final connection = available.removeAt(0);
connection.markAsInUse();
logger.log('Connection reused for relay: $relayUrl');
return connection;
}
final pool = _connectionPools[relayUrl] ?? [];
if (pool.length < maxConnectionsPerRelay) {
final connection = PooledConnection(relayUrl: relayUrl);
pool.add(connection);
_connectionPools[relayUrl] = pool;
connection.markAsInUse();
logger.log(
'New connection created for relay: $relayUrl (${pool.length}/$maxConnectionsPerRelay)',
);
return connection;
}
logger.log(
'Connection pool limit reached for relay: $relayUrl, waiting for available connection',
);
return null;
}