Class: RedisClient::HiredisConnection

Inherits:
Object
  • Object
show all
Includes:
ConnectionMixin
Defined in:
lib/redis_client/hiredis_connection.rb,
ext/redis_client/hiredis/hiredis_connection.c

Defined Under Namespace

Classes: SSLContext

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, connect_timeout:, read_timeout:, write_timeout:) ⇒ HiredisConnection

Returns a new instance of HiredisConnection.



42
43
44
45
46
47
48
# File 'lib/redis_client/hiredis_connection.rb', line 42

def initialize(config, connect_timeout:, read_timeout:, write_timeout:)
  super(config)
  self.connect_timeout = connect_timeout
  self.read_timeout = read_timeout
  self.write_timeout = write_timeout
  connect
end

Class Method Details

.ssl_context(ssl_params) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/redis_client/hiredis_connection.rb', line 12

def ssl_context(ssl_params)
  unless ssl_params[:ca_file] || ssl_params[:ca_path]
    default_ca_file = OpenSSL::X509::DEFAULT_CERT_FILE
    default_ca_path = OpenSSL::X509::DEFAULT_CERT_DIR

    if File.readable? default_ca_file
      ssl_params[:ca_file] = default_ca_file
    elsif File.directory? default_ca_path
      ssl_params[:ca_path] = default_ca_path
    end
  end

  HiredisConnection::SSLContext.new(
    ca_file: ssl_params[:ca_file],
    ca_path: ssl_params[:ca_path],
    cert: ssl_params[:cert],
    key: ssl_params[:key],
    hostname: ssl_params[:hostname],
  )
end

Instance Method Details

#closeObject



50
51
52
53
# File 'lib/redis_client/hiredis_connection.rb', line 50

def close
  _close
  super
end

#connect_timeout=(timeout) ⇒ Object



72
73
74
75
# File 'lib/redis_client/hiredis_connection.rb', line 72

def connect_timeout=(timeout)
  self.connect_timeout_us = timeout ? (timeout * 1_000_000).to_i : 0
  @connect_timeout = timeout
end

#connected?Boolean

Returns:

  • (Boolean)


651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'ext/redis_client/hiredis/hiredis_connection.c', line 651

static VALUE hiredis_connected_p(VALUE self) {
    CONNECTION(self, connection);

    if (!connection->context) {
        return Qfalse;
    }

    if (connection->context->fd == REDIS_INVALID_FD) {
        return Qfalse;
    }

    return Qtrue;
}

#measure_round_trip_delayObject



881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
# File 'ext/redis_client/hiredis/hiredis_connection.c', line 881

static VALUE hiredis_measure_round_trip_delay(VALUE self) {
    CONNECTION(self, connection);
    ENSURE_CONNECTED(connection);

    hiredis_measure_round_trip_delay_args_t args = {
        .connection = connection,
    };
    rb_thread_call_without_gvl(hiredis_measure_round_trip_delay_safe, &args, RUBY_UBF_IO, 0);

    if (args.return_value != REDIS_OK) {
        hiredis_raise_error_and_disconnect(connection, rb_eRedisClientReadTimeoutError);
        return Qnil; // unreachable;
    }

    return DBL2NUM(diff_timespec_ms(&args.end, &args.start));
}

#read(timeout = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/redis_client/hiredis_connection.rb', line 87

def read(timeout = nil)
  if timeout.nil?
    _read
  else
    previous_timeout = @read_timeout
    self.read_timeout = timeout
    begin
      _read
    ensure
      self.read_timeout = previous_timeout
    end
  end
rescue SystemCallError, IOError => error
  raise connection_error(error.message)
rescue Error => error
  error._set_config(config)
  error._set_retry_attempt(@retry_attempt)
  raise error
end

#read_timeout=(timeout) ⇒ Object



77
78
79
80
# File 'lib/redis_client/hiredis_connection.rb', line 77

def read_timeout=(timeout)
  self.read_timeout_us = timeout ? (timeout * 1_000_000).to_i : 0
  @read_timeout = timeout
end

#reconnectObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/redis_client/hiredis_connection.rb', line 55

def reconnect
  reconnected = begin
    _reconnect(@config.path, @config.ssl_context)
  rescue SystemCallError => error
    host = @config.path || "#{@config.host}:#{@config.port}"
    error_code = error.class.name.split("::").last
    raise CannotConnectError, "Failed to reconnect to #{host} (#{error_code})"
  end

  if reconnected
    true
  else
    # Reusing the hiredis connection didn't work let's create a fresh one
    super
  end
end

#write(command) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/redis_client/hiredis_connection.rb', line 107

def write(command)
  _write(command)
  flush
rescue SystemCallError, IOError => error
  raise connection_error(error.message)
rescue Error => error
  error._set_config(config)
  error._set_retry_attempt(@retry_attempt)
  raise error
end

#write_multi(commands) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/redis_client/hiredis_connection.rb', line 118

def write_multi(commands)
  commands.each do |command|
    _write(command)
  end
  flush
rescue SystemCallError, IOError => error
  raise connection_error(error.message)
rescue Error => error
  error._set_config(config)
  error._set_retry_attempt(@retry_attempt)
  raise error
end

#write_timeout=(timeout) ⇒ Object



82
83
84
85
# File 'lib/redis_client/hiredis_connection.rb', line 82

def write_timeout=(timeout)
  self.write_timeout_us = timeout ? (timeout * 1_000_000).to_i : 0
  @write_timeout = timeout
end