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 +4 -4
- data/lib/redcord/connection_pool.rb +28 -0
- data/lib/redcord/redis_connection.rb +21 -17
- data/lib/redcord/relation.rb +1 -1
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f33907b35b938f14be13d1d20f89f1530d72a28cb55e7e38a53f61743a2bfe5
|
4
|
+
data.tar.gz: 1297ac81f293f35572a0015782aa621b1d5ba0e3e691ea86abadbdde26ca094a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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(
|
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(
|
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(
|
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(
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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(
|
80
|
+
sig { returns(RedcordClientType) }
|
77
81
|
def redis
|
78
82
|
self.class.redis
|
79
83
|
end
|
data/lib/redcord/relation.rb
CHANGED
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.
|
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
|