redcord 0.1.2 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0c8f566eb89b44bd73c4a444deb313af1004a342a703a0e9f504364aa96df40
4
- data.tar.gz: e77507ef1ebb3a1d2a6ba9f0fc654d25355f5bc685b05e4bc07368957bdc7ec3
3
+ metadata.gz: bf6ea43c415c480fb7b3484deb69005c13f52bb644fef52f5570d078b15baf46
4
+ data.tar.gz: 0ae55fe438326a9fac46364e6ca0652d71da69aadae9dd204477f1ef502211fc
5
5
  SHA512:
6
- metadata.gz: 27d9afec9f1a663516b65096439312bdc313aed672c6476bcf38d0ce98cd9178bebf3b5a46341d69ad6fc6f3f4268655956c6e5170af2f7da8489d68e343dee5
7
- data.tar.gz: 1b011be75251942091acc209189f33d72ff289496a07f5700c140a06916f83ad310e1e804fc3bd60d77815f89b57fd6a66b9efe2d7fabd81d32dfca7f281b7d6
6
+ metadata.gz: 5f45e3e9bb1c575051df21af680a817bff3a1bb2a367adba95258ab23535cb0fb08d024008edfdd16c58e4132bf67037e7f8bb8ce947e494323ef23a33e4ab1a
7
+ data.tar.gz: d99aa9db0fec8580aacb6fa066612b6cc39c4028fb5a2af4c3c2bc03e851c1bdfa58a2eb17c121e148fef171a1913ed5086e4a2abc877fa47149aa701b260092
@@ -37,7 +37,7 @@ module Redcord::Attribute
37
37
  klass.include(InstanceMethods)
38
38
  klass.class_variable_set(:@@index_attributes, Set.new)
39
39
  klass.class_variable_set(:@@range_index_attributes, Set.new)
40
- klass.class_variable_set(:@@custom_index_attributes, Hash.new { |h, k| h[k] = [] })
40
+ klass.class_variable_set(:@@custom_index_attributes, Hash.new)
41
41
  klass.class_variable_set(:@@ttl, nil)
42
42
  klass.class_variable_set(:@@shard_by_attribute, nil)
43
43
  end
@@ -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
@@ -2,6 +2,7 @@
2
2
 
3
3
  # typed: strict
4
4
 
5
+ require 'active_support'
5
6
  require 'active_support/core_ext/array'
6
7
  require 'active_support/core_ext/module'
7
8
 
@@ -98,7 +99,7 @@ class Redcord::Relation
98
99
  extract_query_conditions!,
99
100
  index_attrs: model._script_arg_index_attrs,
100
101
  range_index_attrs: model._script_arg_range_index_attrs,
101
- custom_index_attrs: model._script_arg_custom_index_attrs[custom_index_name],
102
+ custom_index_attrs: model._script_arg_custom_index_attrs[custom_index_name] || [],
102
103
  hash_tag: extract_hash_tag!,
103
104
  custom_index_name: custom_index_name
104
105
  )
@@ -222,7 +223,7 @@ class Redcord::Relation
222
223
  select_attrs: select_attrs,
223
224
  index_attrs: model._script_arg_index_attrs,
224
225
  range_index_attrs: model._script_arg_range_index_attrs,
225
- custom_index_attrs: model._script_arg_custom_index_attrs[custom_index_name],
226
+ custom_index_attrs: model._script_arg_custom_index_attrs[custom_index_name] || [],
226
227
  hash_tag: extract_hash_tag!,
227
228
  custom_index_name: custom_index_name
228
229
  )
@@ -238,7 +239,7 @@ class Redcord::Relation
238
239
  extract_query_conditions!,
239
240
  index_attrs: model._script_arg_index_attrs,
240
241
  range_index_attrs: model._script_arg_range_index_attrs,
241
- custom_index_attrs: model._script_arg_custom_index_attrs[custom_index_name],
242
+ custom_index_attrs: model._script_arg_custom_index_attrs[custom_index_name] || [],
242
243
  hash_tag: extract_hash_tag!,
243
244
  custom_index_name: custom_index_name
244
245
  )
@@ -248,7 +249,7 @@ class Redcord::Relation
248
249
  end
249
250
  end
250
251
 
251
- sig { returns(Redcord::Redis) }
252
+ sig { returns(Redcord::RedisConnection::RedcordClientType) }
252
253
  def redis
253
254
  model.redis
254
255
  end
@@ -81,7 +81,7 @@ module Redcord::Serializer
81
81
  attr_keys.each do |attr_key|
82
82
  next if attr_key == shard_by_attribute
83
83
 
84
- if !custom_index_attributes.empty?
84
+ if !custom_index_attributes.nil? && !custom_index_attributes.empty?
85
85
  if !custom_index_attributes.include?(attr_key)
86
86
  raise(
87
87
  Redcord::AttributeNotIndexed,
@@ -9,15 +9,15 @@ module Redcord::VacuumHelper
9
9
  sig { params(model: T.class_of(Redcord::Base)).void }
10
10
  def self.vacuum(model)
11
11
  model.class_variable_get(:@@index_attributes).each do |index_attr|
12
- puts "Vacuuming index attribute: #{index_attr}"
12
+ puts "Vacuuming index attribute: #{index_attr} for model: #{model.name}"
13
13
  _vacuum_index_attribute(model, index_attr)
14
14
  end
15
15
  model.class_variable_get(:@@range_index_attributes).each do |range_index_attr|
16
- puts "Vacuuming range index attribute: #{range_index_attr}"
16
+ puts "Vacuuming range index attribute: #{range_index_attr} for model: #{model.name}"
17
17
  _vacuum_range_index_attribute(model, range_index_attr)
18
18
  end
19
19
  model.class_variable_get(:@@custom_index_attributes).keys.each do |index_name|
20
- puts "Vacuuming custom index: #{index_name}"
20
+ puts "Vacuuming custom index: #{index_name} for model: #{model.name}"
21
21
  _vacuum_custom_index(model, index_name)
22
22
  end
23
23
  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.5
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
@@ -205,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
205
220
  - !ruby/object:Gem::Version
206
221
  version: '0'
207
222
  requirements: []
208
- rubygems_version: 3.0.8
223
+ rubygems_version: 3.1.6
209
224
  signing_key:
210
225
  specification_version: 4
211
226
  summary: A Ruby ORM like Active Record, but for Redis