getBestConnection method

PeerConnection? getBestConnection()

Get best connection for a request (based on health and load)

Implementation

PeerConnection? getBestConnection() {
  final activeConnections = getActiveConnections();
  if (activeConnections.isEmpty) {
    return null;
  }

  // Simple implementation: choose connection with least outbound queue size
  PeerConnection? best;
  int bestQueueSize = double.maxFinite.toInt();

  for (final connection in activeConnections) {
    final queueStats = connection.getQueueStats();
    final queueSize = queueStats['size'] as int;

    if (queueSize < bestQueueSize) {
      best = connection;
      bestQueueSize = queueSize;
    }
  }

  return best;
}