openConnection method

  1. @override
Future<ConnManagementScope> openConnection(
  1. Direction direction,
  2. bool useFd,
  3. MultiAddr remoteAddr
)
override

OpenConnection creates a new connection scope not yet associated with any peer; the connection is scoped at the transient scope. The caller owns the returned scope and is responsible for calling Done in order to signify the end of the scope's span.

Implementation

@override
Future<ConnManagementScope> openConnection(Direction direction, bool useFd, MultiAddr remoteAddr) async {
  // TODO: Implement IP-based connection limiting (connLimiter from Go)
  // TODO: Handle allowlisted connections
  // TODO: Determine correct limit (system/transient or allowlisted)

  final connLimit = _limiter.getConnLimits();
  final connName = 'conn-${DateTime.now().millisecondsSinceEpoch}-${remoteAddr.toString().hashCode % 10000}';
  final concreteConnScope = ConnectionScopeImpl(
    this, // Pass ResourceManagerImpl instance
    connLimit,
    connName,
    direction,
    useFd,
    remoteAddr,
    edges: [_transientScope], // TransientScope will propagate to SystemScope
  );

  // Attempt to reserve resources for the connection itself
  // addConn is an internal method of ResourceScopeImpl, ConnectionScopeImpl inherits it.
  try {
    concreteConnScope.addConn(direction, useFd);
  } catch (e) {
    _logger.severe('$e');
    concreteConnScope.done();
    // TODO: metrics.BlockConn(dir, usefd);
    rethrow; // Rethrow the exception caught from addConn
  }

  // TODO: metrics.AllowConn(dir, usefd);
  _logger.fine('Opened connection scope: ${concreteConnScope.name}');
  return concreteConnScope;
}