Skip to content

Commit 67163f5

Browse files
authored
Fix linting (#343)
1 parent 876bcea commit 67163f5

File tree

1 file changed

+63
-77
lines changed

1 file changed

+63
-77
lines changed

lib/message_bus/backends/redis.rb

Lines changed: 63 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

3-
require 'redis'
4-
require 'digest'
3+
require "redis"
4+
require "digest"
55

66
module MessageBus
77
module Backends
@@ -49,9 +49,7 @@ def initialize(redis_config = {}, max_backlog_size = 1000)
4949
@redis_config = redis_config.dup
5050
@clear_every = redis_config.delete(:clear_every) || 1
5151
@logger = @redis_config[:logger]
52-
unless @redis_config[:enable_redis_logger]
53-
@redis_config[:logger] = nil
54-
end
52+
@redis_config[:logger] = nil unless @redis_config[:enable_redis_logger]
5553
@max_backlog_size = max_backlog_size
5654
@max_global_backlog_size = 2000
5755
@max_in_memory_publish_backlog = 1000
@@ -61,7 +59,7 @@ def initialize(redis_config = {}, max_backlog_size = 1000)
6159
@pub_redis = nil
6260
@subscribed = false
6361
# after 7 days inactive backlogs will be removed
64-
@max_backlog_age = 604800
62+
@max_backlog_age = 604_800
6563
end
6664

6765
# Reconnects to Redis; used after a process fork, typically triggered by a forking webserver
@@ -72,9 +70,7 @@ def after_fork
7270

7371
# (see Base#reset!)
7472
def reset!
75-
pub_redis.keys("__mb_*").each do |k|
76-
pub_redis.del k
77-
end
73+
pub_redis.keys("__mb_*").each { |k| pub_redis.del k }
7874
end
7975

8076
# (see Base#destroy)
@@ -85,9 +81,7 @@ def destroy
8581
# Deletes all backlogs and their data. Does not delete ID pointers, so new publications will get IDs that continue from the last publication before the expiry. Use with extreme caution.
8682
# @see Base#expire_all_backlogs!
8783
def expire_all_backlogs!
88-
pub_redis.keys("__mb_*backlog_n").each do |k|
89-
pub_redis.del k
90-
end
84+
pub_redis.keys("__mb_*backlog_n").each { |k| pub_redis.del k }
9185
end
9286

9387
# Note, the script takes care of all expiry of keys, however
@@ -157,23 +151,25 @@ def publish(channel, data, opts = nil)
157151
max_backlog_size,
158152
max_global_backlog_size,
159153
channel,
160-
clear_every
154+
clear_every,
161155
],
162156
keys: [
163157
global_id_key,
164158
backlog_id_key,
165159
backlog_key,
166160
global_backlog_key,
167-
redis_channel_name
168-
]
161+
redis_channel_name,
162+
],
169163
)
170164
rescue ::Redis::CommandError => e
171165
if queue_in_memory && e.message =~ /READONLY/
172166
@lock.synchronize do
173167
@in_memory_backlog << [channel, data]
174168
if @in_memory_backlog.length > @max_in_memory_publish_backlog
175169
@in_memory_backlog.delete_at(0)
176-
@logger.warn("Dropping old message cause max_in_memory_publish_backlog is full: #{e.message}\n#{e.backtrace.join('\n')}")
170+
@logger.warn(
171+
"Dropping old message cause max_in_memory_publish_backlog is full: #{e.message}\n#{e.backtrace.join('\n')}",
172+
)
177173
end
178174
end
179175

@@ -209,9 +205,7 @@ def backlog(channel, last_id = 0)
209205
backlog_key = backlog_key(channel)
210206
items = redis.zrangebyscore backlog_key, last_id.to_i + 1, "+inf"
211207

212-
items.map do |i|
213-
MessageBus::Message.decode(i)
214-
end
208+
items.map { |i| MessageBus::Message.decode(i) }
215209
end
216210

217211
# (see Base#global_backlog)
@@ -254,13 +248,9 @@ def subscribe(channel, last_id = nil)
254248
# we are subscribing on global and global is always going to be bigger than local
255249
# so worst case is a replay of a few messages
256250
message = get_message(channel, last_id)
257-
if message
258-
last_id = message.global_id
259-
end
260-
end
261-
global_subscribe(last_id) do |m|
262-
yield m if m.channel == channel
251+
last_id = message.global_id if message
263252
end
253+
global_subscribe(last_id) { |m| yield m if m.channel == channel }
264254
end
265255

266256
# (see Base#global_unsubscribe)
@@ -280,36 +270,31 @@ def global_subscribe(last_id = nil, &blk)
280270

281271
highest_id = last_id
282272

283-
clear_backlog = lambda do
284-
retries = 4
285-
begin
286-
highest_id = process_global_backlog(highest_id, retries > 0, &blk)
287-
rescue BackLogOutOfOrder => e
288-
highest_id = e.highest_id
289-
retries -= 1
290-
sleep(rand(50) / 1000.0)
291-
retry
273+
clear_backlog =
274+
lambda do
275+
retries = 4
276+
begin
277+
highest_id = process_global_backlog(highest_id, retries > 0, &blk)
278+
rescue BackLogOutOfOrder => e
279+
highest_id = e.highest_id
280+
retries -= 1
281+
sleep(rand(50) / 1000.0)
282+
retry
283+
end
292284
end
293-
end
294285

295286
begin
296287
global_redis = new_redis_connection
297288

298-
if highest_id
299-
clear_backlog.call(&blk)
300-
end
289+
clear_backlog.call(&blk) if highest_id
301290

302291
global_redis.subscribe(redis_channel_name) do |on|
303292
on.subscribe do
304-
if highest_id
305-
clear_backlog.call(&blk)
306-
end
293+
clear_backlog.call(&blk) if highest_id
307294
@subscribed = true
308295
end
309296

310-
on.unsubscribe do
311-
@subscribed = false
312-
end
297+
on.unsubscribe { @subscribed = false }
313298

314299
on.message do |_c, m|
315300
if m == UNSUB_MESSAGE
@@ -346,29 +331,30 @@ def global_subscribe(last_id = nil, &blk)
346331
private
347332

348333
def new_redis_connection
349-
config = @redis_config.filter do |k, v|
350-
# This is not ideal, required for Redis gem version 5
351-
# redis-client no longer accepts arbitrary params
352-
# anything unknown will error out.
353-
# https://github.com/redis-rb/redis-client/blob/4c8e05acfb3477c1651138a4924616e79e6116f2/lib/redis_client/config.rb#L21-L39
354-
#
355-
#
356-
# We should be doing the opposite and allowlisting params
357-
# or splitting the object up. Starting with the smallest change that is backwards compatible
358-
![
359-
:backend,
360-
:logger,
361-
:long_polling_enabled,
362-
:long_polling_interval,
363-
:backend_options,
364-
:base_route,
365-
:client_message_filters,
366-
:site_id_lookup,
367-
:group_ids_lookup,
368-
:user_id_lookup,
369-
:transport_codec
370-
].include?(k)
371-
end
334+
config =
335+
@redis_config.filter do |k, v|
336+
# This is not ideal, required for Redis gem version 5
337+
# redis-client no longer accepts arbitrary params
338+
# anything unknown will error out.
339+
# https://github.com/redis-rb/redis-client/blob/4c8e05acfb3477c1651138a4924616e79e6116f2/lib/redis_client/config.rb#L21-L39
340+
#
341+
#
342+
# We should be doing the opposite and allowlisting params
343+
# or splitting the object up. Starting with the smallest change that is backwards compatible
344+
!%i[
345+
backend
346+
logger
347+
long_polling_enabled
348+
long_polling_interval
349+
backend_options
350+
base_route
351+
client_message_filters
352+
site_id_lookup
353+
group_ids_lookup
354+
user_id_lookup
355+
transport_codec
356+
].include?(k)
357+
end
372358
::Redis.new(config)
373359
end
374360

@@ -399,9 +385,7 @@ def global_backlog_key
399385
end
400386

401387
def process_global_backlog(highest_id, raise_error)
402-
if highest_id > pub_redis.get(global_id_key).to_i
403-
highest_id = 0
404-
end
388+
highest_id = 0 if highest_id > pub_redis.get(global_id_key).to_i
405389

406390
global_backlog(highest_id).each do |old|
407391
if highest_id + 1 == old.global_id
@@ -444,19 +428,21 @@ def ensure_backlog_flushed
444428
if e.message =~ /^READONLY/
445429
try_again = true
446430
else
447-
@logger.warn("Dropping undeliverable message: #{e.message}\n#{e.backtrace.join('\n')}")
431+
@logger.warn(
432+
"Dropping undeliverable message: #{e.message}\n#{e.backtrace.join('\n')}",
433+
)
448434
end
449435
rescue => e
450-
@logger.warn("Dropping undeliverable message: #{e.message}\n#{e.backtrace.join('\n')}")
436+
@logger.warn(
437+
"Dropping undeliverable message: #{e.message}\n#{e.backtrace.join('\n')}",
438+
)
451439
end
452440

453441
@in_memory_backlog.delete_at(0) unless try_again
454442
end
455443
end
456444
ensure
457-
@lock.synchronize do
458-
@flush_backlog_thread = nil
459-
end
445+
@lock.synchronize { @flush_backlog_thread = nil }
460446
end
461447

462448
def cached_eval(redis, script, script_sha1, params)
@@ -479,10 +465,10 @@ def is_readonly?
479465
# in case we are not connected to the correct server
480466
# which can happen when sharing ips
481467
pub_redis.disconnect!
482-
pub_redis.set(key, '1')
468+
pub_redis.set(key, "1")
483469
false
484470
rescue ::Redis::CommandError => e
485-
return true if e.message =~ /^READONLY/
471+
true if e.message =~ /^READONLY/
486472
end
487473
end
488474

0 commit comments

Comments
 (0)