Class: RedisClient::Pooled

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/redis_client/pooled.rb

Constant Summary collapse

EMPTY_HASH =
{}.freeze

Instance Attribute Summary

Attributes included from Common

#config, #connect_timeout, #id, #read_timeout, #write_timeout

Instance Method Summary collapse

Methods included from Common

#timeout=

Constructor Details

#initialize(config, id: config.id, connect_timeout: config.connect_timeout, read_timeout: config.read_timeout, write_timeout: config.write_timeout, **kwargs) ⇒ Pooled

Returns a new instance of Pooled.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/redis_client/pooled.rb', line 11

def initialize(
  config,
  id: config.id,
  connect_timeout: config.connect_timeout,
  read_timeout: config.read_timeout,
  write_timeout: config.write_timeout,
  **kwargs
)
  super(config, id: id, connect_timeout: connect_timeout, read_timeout: read_timeout, write_timeout: write_timeout)
  @pool_kwargs = kwargs
  @pool = new_pool
  @mutex = Mutex.new
end

Instance Method Details

#closeObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/redis_client/pooled.rb', line 37

def close
  if @pool
    @mutex.synchronize do
      pool = @pool
      @pool = nil
      pool&.shutdown(&:close)
    end
  end
  nil
end

#sizeObject



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

def size
  pool.size
end

#with(options = EMPTY_HASH) ⇒ Object Also known as: then



25
26
27
28
29
30
31
32
33
34
# File 'lib/redis_client/pooled.rb', line 25

def with(options = EMPTY_HASH)
  pool.with(options) do |client|
    client.connect_timeout = connect_timeout
    client.read_timeout = read_timeout
    client.write_timeout = write_timeout
    yield client
  end
rescue ConnectionPool::TimeoutError => error
  raise CheckoutTimeoutError, "Couldn't checkout a connection in time: #{error.message}"
end