connect method
Connects to the cluster using the provided initial node(s).
This method will perform the following steps:
- Connect to one of the
initialNodesprovided in the constructor. - Call
CLUSTER SLOTSto fetch the topology. - 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);
}
}
}