Skip to content

Commit 73c795e

Browse files
committed
Optimizes looking up all applications
Routinely, we load all applications and then load their individual data hashes to get the subscriptions. This creates an N+1 number of requests when dispatching requests. Pipelining should minimize network traffic.
1 parent c79610b commit 73c795e

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
9+
### Changed
10+
- Pipelines fetching all queue subscriptions when using `QueueBus::Application.all`
11+
712
## [0.11.0]
813

914
### Added

lib/queue_bus/application.rb

+35-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,22 @@ class Application
77
class << self
88
def all
99
# note the names arent the same as we started with
10-
::QueueBus.redis { |redis| redis.smembers(app_list_key).collect { |val| new(val) } }
10+
::QueueBus.redis do |redis|
11+
app_keys = redis.smembers(app_list_key)
12+
apps = app_keys.collect { |val| new(val) }
13+
14+
hashes = redis.pipelined do
15+
apps.each do |app|
16+
redis.hgetall(app.redis_key)
17+
end
18+
end
19+
20+
apps.zip(hashes).each do |app, hash|
21+
app._hydrate_redis_hash(hash)
22+
end
23+
24+
apps
25+
end
1126
end
1227
end
1328

@@ -90,6 +105,10 @@ def event_display_tuples
90105
out
91106
end
92107

108+
def _hydrate_redis_hash(hash)
109+
@raw_redis_hash = hash
110+
end
111+
93112
protected
94113

95114
def self.normalize(val)
@@ -114,16 +133,24 @@ def subscriptions
114133

115134
def read_redis_hash
116135
out = {}
117-
::QueueBus.redis do |redis|
118-
redis.hgetall(redis_key).each do |key, val|
119-
begin
120-
out[key] = ::QueueBus::Util.decode(val)
121-
rescue ::QueueBus::Util::DecodeException
122-
out[key] = val
123-
end
136+
raw_redis_hash.each do |key, val|
137+
begin
138+
out[key] = ::QueueBus::Util.decode(val)
139+
rescue ::QueueBus::Util::DecodeException
140+
out[key] = val
124141
end
125142
end
126143
out
127144
end
145+
146+
private
147+
148+
def raw_redis_hash
149+
return @raw_redis_hash if @raw_redis_hash
150+
151+
::QueueBus.redis do |redis|
152+
redis.hgetall(redis_key)
153+
end
154+
end
128155
end
129156
end

0 commit comments

Comments
 (0)