redcord 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0c8f566eb89b44bd73c4a444deb313af1004a342a703a0e9f504364aa96df40
4
- data.tar.gz: e77507ef1ebb3a1d2a6ba9f0fc654d25355f5bc685b05e4bc07368957bdc7ec3
3
+ metadata.gz: 9f33907b35b938f14be13d1d20f89f1530d72a28cb55e7e38a53f61743a2bfe5
4
+ data.tar.gz: 1297ac81f293f35572a0015782aa621b1d5ba0e3e691ea86abadbdde26ca094a
5
5
  SHA512:
6
- metadata.gz: 27d9afec9f1a663516b65096439312bdc313aed672c6476bcf38d0ce98cd9178bebf3b5a46341d69ad6fc6f3f4268655956c6e5170af2f7da8489d68e343dee5
7
- data.tar.gz: 1b011be75251942091acc209189f33d72ff289496a07f5700c140a06916f83ad310e1e804fc3bd60d77815f89b57fd6a66b9efe2d7fabd81d32dfca7f281b7d6
6
+ metadata.gz: f4d23f458c6f6de224293010b60f19d7c07199c66eb382e5bd5a7917a429900658bfd42202107c3e72ace588ea526bcf3edb6b56dfeaa7a6b58f3eea4b91dc81
7
+ data.tar.gz: d1aa8dedf1dccad372489072405725fe51e3712b634524c73c574130ab8473bab1fefdedfbd47466c956ab7e96e121153798f08a4250b30e7dc30d33039bf801
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+ require 'connection_pool'
3
+ require_relative 'redis'
4
+
5
+ class Redcord::ConnectionPool
6
+ def initialize(pool_size:, timeout:, **client_options)
7
+ @connection_pool = ::ConnectionPool.new(size: pool_size, timeout: timeout) do
8
+ # Construct a new client every time the block gets called
9
+ Redcord::Redis.new(**client_options, logger: Redcord::Logger.proxy)
10
+ end
11
+ end
12
+
13
+ # Avoid method_missing when possible for better performance
14
+ methods = Set.new(Redcord::Redis.instance_methods(false) + Redis.instance_methods(false))
15
+ methods.each do |method_name|
16
+ define_method method_name do |*args, &blk|
17
+ @connection_pool.with do |redis|
18
+ redis.send(method_name, *args, &blk)
19
+ end
20
+ end
21
+ end
22
+
23
+ def method_missing(method_name, *args, &blk)
24
+ @connection_pool.with do |redis|
25
+ redis.send(method_name, *args, &blk)
26
+ end
27
+ end
28
+ end
@@ -6,11 +6,14 @@ require 'rails'
6
6
 
7
7
  require 'redcord/lua_script_reader'
8
8
  require 'redcord/redis'
9
+ require 'redcord/connection_pool'
9
10
 
10
11
  module Redcord::RedisConnection
11
12
  extend T::Sig
12
13
  extend T::Helpers
13
14
 
15
+ RedcordClientType = T.type_alias { T.any(Redcord::Redis, Redcord::ConnectionPool) }
16
+
14
17
  @connections = T.let(nil, T.nilable(T::Hash[String, T.untyped]))
15
18
  @procs_to_prepare = T.let([], T::Array[Proc])
16
19
 
@@ -29,17 +32,17 @@ module Redcord::RedisConnection
29
32
  (env_config[name.underscore] || env_config['default']).symbolize_keys
30
33
  end
31
34
 
32
- sig { returns(Redcord::Redis) }
35
+ sig { returns(RedcordClientType) }
33
36
  def redis
34
37
  Redcord::RedisConnection.connections[name.underscore] ||= prepare_redis!
35
38
  end
36
39
 
37
- sig { returns(Redcord::Redis) }
40
+ sig { returns(RedcordClientType) }
38
41
  def establish_connection
39
42
  Redcord::RedisConnection.connections[name.underscore] = prepare_redis!
40
43
  end
41
44
 
42
- sig { params(redis: Redis).returns(Redcord::Redis) }
45
+ sig { params(redis: Redis).returns(RedcordClientType) }
43
46
  def redis=(redis)
44
47
  Redcord::RedisConnection.connections[name.underscore] =
45
48
  prepare_redis!(redis)
@@ -50,20 +53,21 @@ module Redcord::RedisConnection
50
53
  # definitions in each Redis query.
51
54
  #
52
55
  # TODO: Replace this with Redcord migrations
53
- sig { params(client: T.nilable(Redis)).returns(Redcord::Redis) }
56
+ sig { params(client: T.nilable(Redis)).returns(RedcordClientType) }
54
57
  def prepare_redis!(client = nil)
55
- return client if client.is_a?(Redcord::Redis)
56
-
57
- client = Redcord::Redis.new(
58
- **(
59
- if client.nil?
60
- connection_config
61
- else
62
- client.instance_variable_get(:@options)
63
- end
64
- ),
65
- logger: Redcord::Logger.proxy,
66
- )
58
+ return client if client.is_a?(Redcord::Redis) || client.is_a?(Redcord::ConnectionPool)
59
+
60
+ options = client.nil? ? connection_config : client.instance_variable_get(:@options)
61
+ client =
62
+ if options[:pool]
63
+ Redcord::ConnectionPool.new(
64
+ pool_size: options[:pool],
65
+ timeout: options[:connection_timeout] || 1.0,
66
+ **options
67
+ )
68
+ else
69
+ Redcord::Redis.new(**options, logger: Redcord::Logger.proxy)
70
+ end
67
71
 
68
72
  client.ping
69
73
  client
@@ -73,7 +77,7 @@ module Redcord::RedisConnection
73
77
  module InstanceMethods
74
78
  extend T::Sig
75
79
 
76
- sig { returns(Redcord::Redis) }
80
+ sig { returns(RedcordClientType) }
77
81
  def redis
78
82
  self.class.redis
79
83
  end
@@ -248,7 +248,7 @@ class Redcord::Relation
248
248
  end
249
249
  end
250
250
 
251
- sig { returns(Redcord::Redis) }
251
+ sig { returns(Redcord::RedisConnection::RedcordClientType) }
252
252
  def redis
253
253
  model.redis
254
254
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redcord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chan Zuckerberg Initiative
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: connection_pool
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.2.3
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.2.3
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: sorbet
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -162,6 +176,7 @@ files:
162
176
  - lib/redcord/attribute.rb
163
177
  - lib/redcord/base.rb
164
178
  - lib/redcord/configurations.rb
179
+ - lib/redcord/connection_pool.rb
165
180
  - lib/redcord/logger.rb
166
181
  - lib/redcord/lua_script_reader.rb
167
182
  - lib/redcord/migration.rb