connectMultiHostSession function

Future<MultiHostSession> connectMultiHostSession(
  1. List<HostAttachment> hosts
)

Opens one owned VM-service connection for each host, in attach order.

If a later attachment fails, every client opened earlier in the sequence is disposed before the error is rethrown.

Implementation

Future<MultiHostSession> connectMultiHostSession(
  List<HostAttachment> hosts,
) async {
  if (hosts.isEmpty) {
    throw ArgumentError.value(
      hosts,
      'hosts',
      'connectMultiHostSession requires at least one host',
    );
  }

  final List<({String label, VmServiceClient client})> clients =
      <({String label, VmServiceClient client})>[];
  try {
    for (final HostAttachment host in hosts) {
      final VmServiceClient client = await connectVmServiceClient(host.uri);
      clients.add((label: host.label, client: client));
    }
  } on Object {
    for (final ({String label, VmServiceClient client}) connection in clients) {
      await connection.client.dispose();
    }
    rethrow;
  }
  return MultiHostSession.fromVmServiceClients(clients);
}