emb 0.2.2 → 0.2.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/README.md +31 -0
- data/lib/emb/client.rb +23 -13
- data/lib/emb/configuration.rb +30 -0
- data/lib/emb.rb +10 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2210ac3c666538429c3d0eccc58c26b94bc87a26f65cd6c3b4ba2f754b5de6fa
|
|
4
|
+
data.tar.gz: f576b3edf8bec7d38d156b35082e86ac19a80324c0f437e78197f674f0156215
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3dc881cd39e7dcdf6898afa215d9d852a638a2df020d225056e861b9f79e57f13001a30c0e88b0b1cf0e997328c9682b6d4f2cdfbd97c7795e1adc6a8262a279
|
|
7
|
+
data.tar.gz: 93aa184f12078697b8f27946567dd3221b99d098f67f207d4cbe3842472583ae76fba8a8aa8bab5a5cba3a432c12eff2a2449d53ff93187a043a6bfd0e864c4d
|
data/README.md
CHANGED
|
@@ -44,6 +44,37 @@ Emb.setup
|
|
|
44
44
|
2. `EMB_URL` environment variable
|
|
45
45
|
3. Default: `redis://localhost:6379`
|
|
46
46
|
|
|
47
|
+
### Global configuration
|
|
48
|
+
|
|
49
|
+
Every client (`Emb.setup`, `Emb.new`, and the lazily-created default client) inherits a
|
|
50
|
+
shared global `Emb::Config` value object set with `Emb.configure`. Settings
|
|
51
|
+
resolve in this order — the first one wins:
|
|
52
|
+
|
|
53
|
+
1. **Explicit per-call option** (`Emb.setup(pool: 10)`)
|
|
54
|
+
2. **`Emb.configure` value**
|
|
55
|
+
3. **Built-in default**
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
Emb.configure do |c|
|
|
59
|
+
c.pool = 8
|
|
60
|
+
c.batch = false # opt out of lazy batching app-wide
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
Emb.configuration # => the shared Emb::Configuration
|
|
64
|
+
Emb::Client.new(pool: 20) # per-call still wins
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`EMB_URL` remains the only environment variable (connection URL fallback, as before);
|
|
68
|
+
`Emb.configure { |c| c.url = ... }` or an explicit `url:` override it.
|
|
69
|
+
|
|
70
|
+
### Out-of-the-box defaults
|
|
71
|
+
|
|
72
|
+
The shipped defaults are benchmark-derived (see `BENCHMARK.md`): **lazy batching is on by
|
|
73
|
+
default** (`batch: true` — each embed coalesces into one `EMB.MULTI`), pool `5`, pure-Ruby
|
|
74
|
+
RESP driver, `protocol: 2`, `reconnect_attempts: 3`. To keep the eager behavior (immediate
|
|
75
|
+
`EMB` per call), opt out globally via `Emb.configure { |c| c.batch = false }` or per client
|
|
76
|
+
with `Emb.new(batch: false)`.
|
|
77
|
+
|
|
47
78
|
### Connection pool
|
|
48
79
|
|
|
49
80
|
```ruby
|
data/lib/emb/client.rb
CHANGED
|
@@ -4,21 +4,17 @@ require 'connection_pool'
|
|
|
4
4
|
require 'redis_client'
|
|
5
5
|
|
|
6
6
|
module Emb
|
|
7
|
-
DEFAULTS = { host: 'localhost', port: 6379, pool: 5 }.freeze
|
|
8
|
-
|
|
9
7
|
class Client
|
|
10
8
|
attr_reader :pool
|
|
11
9
|
|
|
12
|
-
def initialize(pool:
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
redis_options
|
|
18
|
-
redis_options[:protocol] ||= 2
|
|
19
|
-
redis_options[:reconnect_attempts] ||= 3
|
|
10
|
+
def initialize(pool: nil, batch: nil, **redis_options)
|
|
11
|
+
cfg = Emb.configuration
|
|
12
|
+
@batch_enabled = batch.nil? ? cfg.batch : batch
|
|
13
|
+
size = pool.nil? ? cfg.pool : pool
|
|
14
|
+
url = extract_url!(redis_options, cfg)
|
|
15
|
+
redis_options = merged_redis_options(redis_options, cfg, url)
|
|
20
16
|
|
|
21
|
-
@pool = ConnectionPool.new(size:
|
|
17
|
+
@pool = ConnectionPool.new(size: size) do
|
|
22
18
|
RedisClient.new(url: url, **redis_options)
|
|
23
19
|
end
|
|
24
20
|
|
|
@@ -101,9 +97,23 @@ module Emb
|
|
|
101
97
|
|
|
102
98
|
private
|
|
103
99
|
|
|
104
|
-
def
|
|
100
|
+
def merged_redis_options(opts, cfg, url)
|
|
101
|
+
defaults = cfg.to_h
|
|
102
|
+
keys = defaults.keys - %i[url pool batch]
|
|
103
|
+
keys -= %i[host port] if url
|
|
104
|
+
|
|
105
|
+
keys.each do |key|
|
|
106
|
+
opts[key] = defaults[key] if opts[key].nil? && !defaults[key].nil?
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
opts
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def extract_url!(opts, cfg)
|
|
105
113
|
url = opts.delete(:url)
|
|
106
|
-
url
|
|
114
|
+
return url unless url.nil?
|
|
115
|
+
|
|
116
|
+
cfg.url || ENV.fetch('EMB_URL', nil)
|
|
107
117
|
end
|
|
108
118
|
end
|
|
109
119
|
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Emb
|
|
4
|
+
class Configuration
|
|
5
|
+
OPTIONS = %i[
|
|
6
|
+
host port url pool batch driver protocol
|
|
7
|
+
connect_timeout read_timeout write_timeout reconnect_attempts
|
|
8
|
+
].freeze
|
|
9
|
+
|
|
10
|
+
attr_accessor(*OPTIONS)
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
self.host = 'localhost'
|
|
14
|
+
self.port = 6379
|
|
15
|
+
self.url = nil
|
|
16
|
+
self.pool = 5
|
|
17
|
+
self.batch = true
|
|
18
|
+
self.driver = nil
|
|
19
|
+
self.protocol = 2
|
|
20
|
+
self.connect_timeout = nil
|
|
21
|
+
self.read_timeout = nil
|
|
22
|
+
self.write_timeout = nil
|
|
23
|
+
self.reconnect_attempts = 3
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def to_h
|
|
27
|
+
OPTIONS.to_h { |key| [key, public_send(key)] }
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
data/lib/emb.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'emb/version'
|
|
4
|
+
require_relative 'emb/configuration'
|
|
4
5
|
require_relative 'emb/client'
|
|
5
6
|
require_relative 'emb/proxy'
|
|
6
7
|
require_relative 'emb/multi'
|
|
@@ -17,6 +18,15 @@ module Emb
|
|
|
17
18
|
|
|
18
19
|
alias config setup
|
|
19
20
|
|
|
21
|
+
def configuration
|
|
22
|
+
@configuration ||= Configuration.new
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def configure
|
|
26
|
+
yield configuration if block_given?
|
|
27
|
+
configuration
|
|
28
|
+
end
|
|
29
|
+
|
|
20
30
|
def [](name) = default_client[name]
|
|
21
31
|
def batch = default_client.batch
|
|
22
32
|
def models = default_client.models
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: emb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- elcuervo
|
|
@@ -64,6 +64,7 @@ files:
|
|
|
64
64
|
- lib/emb.rb
|
|
65
65
|
- lib/emb/batch.rb
|
|
66
66
|
- lib/emb/client.rb
|
|
67
|
+
- lib/emb/configuration.rb
|
|
67
68
|
- lib/emb/middleware.rb
|
|
68
69
|
- lib/emb/multi.rb
|
|
69
70
|
- lib/emb/proxy.rb
|