connect method

  1. @override
Future<void> connect()
override

Connects to the cluster using the provided initial node(s).

This method will perform the following steps:

  1. Connect to one of the initialNodes provided in the constructor.
  2. Call CLUSTER SLOTS to fetch the topology.
  3. Create connection pools for each discovered master node.

Throws KeyscopeConnectionException if it fails to connect or fetch the cluster topology.

Implementation

@override
Future<void> connect() async {
  if (_slotMap != null) return; // Already connected
  if (_isClosed) {
    throw KeyscopeClientException(
        'Client is closed and cannot be reconnected.');
  }

  _hostMap = {}; // Reset NAT map on (re)connect

  // Initial Topology Fetch
  // We treat the initial connection as a "Refresh" to populate the map.
  await _refreshTopology(isInitial: true);

  // Pre-warm Connection Pools
  // \ We must ensure that pools for all discovered master nodes are created immediately.
  // \ This allows methods like pingAll() to work right after connect().
  if (_slotMap != null) {
    for (final node in _slotMap!.masterNodes) {
      _getOrCreatePool(node);
    }
  }
}