Class: RedisClient

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/redis_client.rb,
lib/redis_client/config.rb,
lib/redis_client/pooled.rb,
lib/redis_client/version.rb,
lib/redis_client/decorator.rb,
lib/redis_client/hash_ring.rb,
lib/redis_client/pid_cache.rb,
lib/redis_client/url_config.rb,
lib/redis_client/middlewares.rb,
lib/redis_client/circuit_breaker.rb,
lib/redis_client/command_builder.rb,
lib/redis_client/ruby_connection.rb,
lib/redis_client/sentinel_config.rb,
lib/redis_client/connection_mixin.rb,
lib/redis_client/ruby_connection/resp3.rb,
lib/redis_client/ruby_connection/buffered_io.rb

Defined Under Namespace

Modules: CommandBuilder, Common, ConnectionMixin, Decorator, Final, HasCode, HasCommand, HasConfig, PIDCache, RESP3, Retriable Classes: BasicMiddleware, CircuitBreaker, CommandError, Config, Error, HashRing, Middlewares, Multi, Pipeline, Pooled, PubSub, RubyConnection, SentinelConfig, URLConfig

Constant Summary collapse

ProtocolError =
Class.new(Error)
UnsupportedServer =
Class.new(Error)
ConnectionError =
Class.new(Error)
CannotConnectError =
Class.new(ConnectionError)
FailoverError =
Class.new(ConnectionError)
TimeoutError =
Class.new(ConnectionError)
ReadTimeoutError =
Class.new(TimeoutError)
WriteTimeoutError =
Class.new(TimeoutError)
CheckoutTimeoutError =
Class.new(TimeoutError)
AuthenticationError =
Class.new(CommandError)
PermissionError =
Class.new(CommandError)
WrongTypeError =
Class.new(CommandError)
OutOfMemoryError =
Class.new(CommandError)
NoScriptError =
Class.new(CommandError)
ReadOnlyError =
Class.new(ConnectionError)
MasterDownError =
Class.new(ConnectionError)
VERSION =
"0.29.0"

Instance Attribute Summary

Attributes included from Common

#config, #connect_timeout, #nodes, #read_timeout, #write_timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#node_for, #nodes_for

Constructor Details

#initialize(config) ⇒ RedisClient

Returns a new instance of RedisClient.



269
270
271
272
273
274
275
# File 'lib/redis_client.rb', line 269

def initialize(config, **)
  super
  @middlewares = config.middlewares_stack.new(self)
  @raw_connection = nil
  @disable_reconnection = false
  @retry_attempt = nil
end

Class Method Details

.config(**kwargs) ⇒ Object



240
241
242
# File 'lib/redis_client.rb', line 240

def config(**kwargs)
  Config.new(client_implementation: self, **kwargs)
end

.default_driverObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/redis_client.rb', line 33

def default_driver
  unless @default_driver
    @driver_definitions.each_key do |name|
      if @default_driver = driver(name)
        break
      end
    rescue LoadError
    end
  end
  @default_driver
end

.default_driver=(name) ⇒ Object



45
46
47
# File 'lib/redis_client.rb', line 45

def default_driver=(name)
  @default_driver = driver(name)
end

.driver(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/redis_client.rb', line 22

def driver(name)
  return name if name.is_a?(Class)

  name = name.to_sym
  unless @driver_definitions.key?(name)
    raise ArgumentError, "Unknown driver #{name.inspect}, expected one of: `#{@driver_definitions.keys.inspect}`"
  end

  @drivers[name] ||= @driver_definitions[name]&.call
end

.new(arg = nil, **kwargs) ⇒ Object



254
255
256
257
258
259
260
# File 'lib/redis_client.rb', line 254

def new(arg = nil, **kwargs)
  if arg.is_a?(Config::Common)
    super
  else
    super(config(**(arg || {}), **kwargs))
  end
end

.nowObject



49
50
51
# File 'lib/redis_client.rb', line 49

def now
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

.now_msObject



53
54
55
# File 'lib/redis_client.rb', line 53

def now_ms
  Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
end

.register(middleware) ⇒ Object



262
263
264
# File 'lib/redis_client.rb', line 262

def register(middleware)
  Middlewares.include(middleware)
end

.register_driver(name, &block) ⇒ Object



18
19
20
# File 'lib/redis_client.rb', line 18

def register_driver(name, &block)
  @driver_definitions[name] = block
end

.ring(*clients, **options) ⇒ Object



248
249
250
251
252
# File 'lib/redis_client.rb', line 248

def ring(*clients, **options)
  clients.flatten!
  require "redis_client/hash_ring" unless defined?(HashRing)
  HashRing.new(clients, **options)
end

.sentinel(**kwargs) ⇒ Object



244
245
246
# File 'lib/redis_client.rb', line 244

def sentinel(**kwargs)
  SentinelConfig.new(client_implementation: self, **kwargs)
end

Instance Method Details

#blocking_call(timeout, *command, **kwargs) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/redis_client.rb', line 421

def blocking_call(timeout, *command, **kwargs)
  command = @command_builder.generate(command, kwargs)
  error = nil
  result = ensure_connected do |connection|
    @middlewares.call(command, config) do
      connection.call(command, timeout)
    end
  rescue ReadTimeoutError => error
    break
  end

  if error
    raise error
  elsif block_given?
    yield result
  else
    result
  end
end

#blocking_call_v(timeout, command) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/redis_client.rb', line 441

def blocking_call_v(timeout, command)
  command = @command_builder.generate(command)
  error = nil
  result = ensure_connected do |connection|
    @middlewares.call(command, config) do
      connection.call(command, timeout)
    end
  rescue ReadTimeoutError => error
    break
  end

  if error
    raise error
  elsif block_given?
    yield result
  else
    result
  end
end

#call(*command, **kwargs) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/redis_client.rb', line 361

def call(*command, **kwargs)
  command = @command_builder.generate(command, kwargs)
  result = ensure_connected do |connection|
    @middlewares.call(command, config) do
      connection.call(command, nil)
    end
  end

  if block_given?
    yield result
  else
    result
  end
end

#call_once(*command, **kwargs) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/redis_client.rb', line 391

def call_once(*command, **kwargs)
  command = @command_builder.generate(command, kwargs)
  result = ensure_connected(retryable: false) do |connection|
    @middlewares.call(command, config) do
      connection.call(command, nil)
    end
  end

  if block_given?
    yield result
  else
    result
  end
end

#call_once_v(command) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/redis_client.rb', line 406

def call_once_v(command)
  command = @command_builder.generate(command)
  result = ensure_connected(retryable: false) do |connection|
    @middlewares.call(command, config) do
      connection.call(command, nil)
    end
  end

  if block_given?
    yield result
  else
    result
  end
end

#call_v(command) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/redis_client.rb', line 376

def call_v(command)
  command = @command_builder.generate(command)
  result = ensure_connected do |connection|
    @middlewares.call(command, config) do
      connection.call(command, nil)
    end
  end

  if block_given?
    yield result
  else
    result
  end
end

#closeObject



501
502
503
504
# File 'lib/redis_client.rb', line 501

def close
  @raw_connection&.close
  self
end

#connected?Boolean

Returns:

  • (Boolean)


497
498
499
# File 'lib/redis_client.rb', line 497

def connected?
  @raw_connection&.revalidate
end

#dbObject



298
299
300
# File 'lib/redis_client.rb', line 298

def db
  config.db
end

#disable_reconnection(&block) ⇒ Object



506
507
508
# File 'lib/redis_client.rb', line 506

def disable_reconnection(&block)
  ensure_connected(retryable: false, &block)
end

#hostObject



302
303
304
# File 'lib/redis_client.rb', line 302

def host
  config.host unless config.path
end

#hscan(key, *args, **kwargs, &block) ⇒ Object



479
480
481
482
483
484
485
486
# File 'lib/redis_client.rb', line 479

def hscan(key, *args, **kwargs, &block)
  unless block_given?
    return to_enum(__callee__, key, *args, **kwargs)
  end

  args = @command_builder.generate(["HSCAN", key, 0] + args, kwargs)
  scan_pairs(2, args, &block)
end

#idObject



286
287
288
# File 'lib/redis_client.rb', line 286

def id
  config.id
end

#idle_timeoutObject



294
295
296
# File 'lib/redis_client.rb', line 294

def idle_timeout
  config.idle_timeout
end

#inspectObject



277
278
279
280
# File 'lib/redis_client.rb', line 277

def inspect
  id_string = " id=#{id}" if id
  "#<#{self.class.name} #{config.server_url}#{id_string}>"
end

#measure_round_trip_delayObject



353
354
355
356
357
358
359
# File 'lib/redis_client.rb', line 353

def measure_round_trip_delay
  ensure_connected do |connection|
    @middlewares.call(["PING"], config) do
      connection.measure_round_trip_delay
    end
  end
end

#multi(watch: nil, &block) ⇒ Object



528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/redis_client.rb', line 528

def multi(watch: nil, &block)
  transaction = nil

  results = if watch
    # WATCH is stateful, so we can't reconnect if it's used, the whole transaction
    # has to be redone.
    ensure_connected(retryable: false) do |connection|
      call("WATCH", *watch)
      begin
        if transaction = build_transaction(&block)
          commands = transaction._commands
          results = @middlewares.call_pipelined(commands, config) do
            connection.call_pipelined(commands, nil)
          end.last
        else
          call("UNWATCH")
          []
        end
      rescue
        call("UNWATCH") if connected? && watch
        raise
      end
    end
  else
    transaction = build_transaction(&block)
    if transaction._empty?
      []
    else
      ensure_connected(retryable: transaction._retryable?) do |connection|
        commands = transaction._commands
        @middlewares.call_pipelined(commands, config) do
          connection.call_pipelined(commands, nil)
        end.last
      end
    end
  end

  if transaction
    transaction._coerce!(results)
  else
    results
  end
end

#passwordObject



318
319
320
# File 'lib/redis_client.rb', line 318

def password
  config.password
end

#pathObject



310
311
312
# File 'lib/redis_client.rb', line 310

def path
  config.path
end

#pipelined(exception: true) {|pipeline| ... } ⇒ Object

Yields:

  • (pipeline)


510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/redis_client.rb', line 510

def pipelined(exception: true)
  pipeline = Pipeline.new(@command_builder)
  yield pipeline

  if pipeline._size == 0
    []
  else
    results = ensure_connected(retryable: pipeline._retryable?) do |connection|
      commands = pipeline._commands
      @middlewares.call_pipelined(commands, config) do
        connection.call_pipelined(commands, pipeline._timeouts, exception: exception)
      end
    end

    pipeline._coerce!(results)
  end
end

#portObject



306
307
308
# File 'lib/redis_client.rb', line 306

def port
  config.port unless config.path
end

#pubsubObject



347
348
349
350
351
# File 'lib/redis_client.rb', line 347

def pubsub
  sub = PubSub.new(ensure_connected, @command_builder)
  @raw_connection = nil
  sub
end

#read_timeout=(timeout) ⇒ Object



337
338
339
340
# File 'lib/redis_client.rb', line 337

def read_timeout=(timeout)
  super
  @raw_connection&.read_timeout = timeout
end

#scan(*args, **kwargs, &block) ⇒ Object



461
462
463
464
465
466
467
468
# File 'lib/redis_client.rb', line 461

def scan(*args, **kwargs, &block)
  unless block_given?
    return to_enum(__callee__, *args, **kwargs)
  end

  args = @command_builder.generate(["SCAN", 0] + args, kwargs)
  scan_list(1, args, &block)
end

#server_urlObject



282
283
284
# File 'lib/redis_client.rb', line 282

def server_url
  config.server_url
end

#sizeObject



322
323
324
# File 'lib/redis_client.rb', line 322

def size
  1
end

#sscan(key, *args, **kwargs, &block) ⇒ Object



470
471
472
473
474
475
476
477
# File 'lib/redis_client.rb', line 470

def sscan(key, *args, **kwargs, &block)
  unless block_given?
    return to_enum(__callee__, key, *args, **kwargs)
  end

  args = @command_builder.generate(["SSCAN", key, 0] + args, kwargs)
  scan_list(2, args, &block)
end

#timeoutObject



290
291
292
# File 'lib/redis_client.rb', line 290

def timeout
  config.read_timeout
end

#timeout=(timeout) ⇒ Object



331
332
333
334
335
# File 'lib/redis_client.rb', line 331

def timeout=(timeout)
  super
  @raw_connection&.read_timeout = timeout
  @raw_connection&.write_timeout = timeout
end

#usernameObject



314
315
316
# File 'lib/redis_client.rb', line 314

def username
  config.username
end

#with(_options = nil) {|_self| ... } ⇒ Object Also known as: then

Yields:

  • (_self)

Yield Parameters:

  • _self (RedisClient)

    the object that the method was called on



326
327
328
# File 'lib/redis_client.rb', line 326

def with(_options = nil)
  yield self
end

#write_timeout=(timeout) ⇒ Object



342
343
344
345
# File 'lib/redis_client.rb', line 342

def write_timeout=(timeout)
  super
  @raw_connection&.write_timeout = timeout
end

#zscan(key, *args, **kwargs, &block) ⇒ Object



488
489
490
491
492
493
494
495
# File 'lib/redis_client.rb', line 488

def zscan(key, *args, **kwargs, &block)
  unless block_given?
    return to_enum(__callee__, key, *args, **kwargs)
  end

  args = @command_builder.generate(["ZSCAN", key, 0] + args, kwargs)
  scan_pairs(2, args, &block)
end