connect method

Future<void> connect()

Connect to redis connection

await redis.connect()

Implementation

Future<void> connect() async {
  _totalRetry++;
  try {
    status = RedisConnectionStatus.connecting;

    /// Create new socket if not exist
    if (_redisSocket == null) {
      if (option.secure == true) {
        _redisSocket = await SecureSocket.connect(option.host, option.port,
            timeout: option.connectTimeout);
      } else {
        _redisSocket = await Socket.connect(option.host, option.port,
            timeout: option.connectTimeout);
      }
    }

    /// Setting socket option to tcp no delay
    /// If tcpNoDelay is enabled, the socket will not buffer data internally,
    /// but instead write each data chunk as an individual TCP packet.
    _redisSocket?.setOption(SocketOption.tcpNoDelay, true);

    /// listening for response
    _listenResponseFromRedis();

    /// Set status as connected
    status = RedisConnectionStatus.connected;

    /// Once socket is connect reset retry count to zero
    _totalRetry = 0;

    /// If username is provided, we need to login before calling other commands
    await _login();

    /// Select database index
    await _selectDatabaseIndex();
  } catch (error) {
    if (error is SocketException) {
      _throwSafeError(
        SocketException(error.message,
            address: InternetAddress(option.host), port: option.port),
      );
      status = RedisConnectionStatus.disconnected;

      /// If error is SocketException, need to reconnect
      await _reconnect();
    } else {
      /// rethrow application logic errors
      rethrow;
    }
  }
}