message_bus 4.3.7 → 4.3.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +35 -21
- data/.rubocop.yml +6 -2
- data/CHANGELOG +15 -2
- data/Rakefile +2 -4
- data/lib/message_bus/backends/base.rb +0 -1
- data/lib/message_bus/backends/postgres.rb +1 -1
- data/lib/message_bus/backends/redis.rb +64 -78
- data/lib/message_bus/rack/middleware.rb +7 -1
- data/lib/message_bus/version.rb +1 -1
- data/lib/message_bus.rb +1 -2
- data/message_bus.gemspec +4 -2
- data/package-lock.json +392 -192
- data/spec/lib/message_bus/multi_process_spec.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- metadata +7 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 258a314e3749265bd7fbaa9fadcaba10a3e4fdb46d204787ccc96effecdcbc7c
|
4
|
+
data.tar.gz: b14add0ef9b398c2adf721adb725d7dbeb2719066f694e753fde7073a06da502
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebc4a907f665282f748fef892398b203b71888c98800350d7061ee870d7848a8a3f1034d501ccb162c5d099dee9edbc411a950921ce063ccac7a7ac58e60e995
|
7
|
+
data.tar.gz: 9b454a1f78c88351865bb277c2831b4f9772113ad865793f7d05ef531816600f828a4cc18f461a969db002038a30b5ae2d472bdd4128b7a0ccf8d2bfab6e6067
|
data/.github/workflows/ci.yml
CHANGED
@@ -7,7 +7,34 @@ on:
|
|
7
7
|
pull_request:
|
8
8
|
|
9
9
|
jobs:
|
10
|
-
|
10
|
+
lint:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
timeout-minutes: 5
|
13
|
+
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v4
|
16
|
+
|
17
|
+
- uses: ruby/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: "3.3"
|
20
|
+
bundler-cache: true
|
21
|
+
|
22
|
+
- name: Set up Node.js
|
23
|
+
uses: actions/setup-node@v4
|
24
|
+
with:
|
25
|
+
node-version: 20
|
26
|
+
cache: npm
|
27
|
+
|
28
|
+
- name: Setup npm
|
29
|
+
run: npm install
|
30
|
+
|
31
|
+
- name: Rubocop
|
32
|
+
run: bundle exec rubocop
|
33
|
+
|
34
|
+
- name: ESLint
|
35
|
+
run: npx eslint .
|
36
|
+
|
37
|
+
test:
|
11
38
|
runs-on: ubuntu-latest
|
12
39
|
name: Ruby ${{ matrix.ruby }} (redis ${{ matrix.redis }})
|
13
40
|
timeout-minutes: 10
|
@@ -20,12 +47,12 @@ jobs:
|
|
20
47
|
strategy:
|
21
48
|
fail-fast: false
|
22
49
|
matrix:
|
23
|
-
ruby: [2.6, 2.7,
|
50
|
+
ruby: ["2.6", "2.7", "3.0", "3.1"]
|
24
51
|
redis: [5, 6]
|
25
52
|
|
26
53
|
services:
|
27
54
|
postgres:
|
28
|
-
image: postgres:
|
55
|
+
image: postgres:16
|
29
56
|
env:
|
30
57
|
POSTGRES_DB: message_bus_test
|
31
58
|
POSTGRES_PASSWORD: postgres
|
@@ -43,41 +70,28 @@ jobs:
|
|
43
70
|
--health-retries 5
|
44
71
|
|
45
72
|
steps:
|
46
|
-
- uses: actions/checkout@
|
73
|
+
- uses: actions/checkout@v4
|
47
74
|
|
48
75
|
- uses: ruby/setup-ruby@v1
|
49
76
|
with:
|
50
77
|
ruby-version: ${{ matrix.ruby }}
|
51
78
|
bundler-cache: true
|
52
79
|
|
53
|
-
- name: Set up Node.js
|
54
|
-
uses: actions/setup-node@v3
|
55
|
-
with:
|
56
|
-
node-version: 18
|
57
|
-
cache: npm
|
58
|
-
|
59
|
-
- name: Setup npm
|
60
|
-
run: npm install
|
61
|
-
|
62
80
|
- name: Tests
|
63
81
|
env:
|
64
82
|
TESTOPTS: --verbose
|
65
83
|
run: bundle exec rake
|
66
|
-
timeout-minutes: 3
|
67
|
-
|
68
|
-
- name: Linting
|
69
|
-
run: npx eslint .
|
70
84
|
|
71
85
|
publish:
|
72
86
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
73
|
-
needs:
|
87
|
+
needs: [lint, test]
|
74
88
|
runs-on: ubuntu-latest
|
75
89
|
|
76
90
|
steps:
|
77
|
-
- uses: actions/checkout@
|
91
|
+
- uses: actions/checkout@v4
|
78
92
|
|
79
93
|
- name: Release gem
|
80
|
-
uses: discourse/publish-rubygems-action@
|
94
|
+
uses: discourse/publish-rubygems-action@v3
|
81
95
|
id: publish-gem
|
82
96
|
env:
|
83
97
|
RUBYGEMS_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}
|
@@ -95,7 +109,7 @@ jobs:
|
|
95
109
|
git commit -m 'bump'
|
96
110
|
|
97
111
|
- name: Publish package
|
98
|
-
uses: JS-DevTools/npm-publish@
|
112
|
+
uses: JS-DevTools/npm-publish@v3
|
99
113
|
if: steps.publish-gem.outputs.new_version == 'true'
|
100
114
|
with:
|
101
115
|
token: ${{ secrets.NPM_TOKEN }}
|
data/.rubocop.yml
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
inherit_gem:
|
2
|
-
rubocop-discourse: .
|
2
|
+
rubocop-discourse: stree-compat.yml
|
3
3
|
inherit_mode:
|
4
4
|
merge:
|
5
5
|
- Exclude
|
6
|
+
|
6
7
|
AllCops:
|
7
8
|
Exclude:
|
8
|
-
-
|
9
|
+
- "examples/**/*"
|
10
|
+
|
11
|
+
Discourse/Plugins:
|
12
|
+
Enabled: false
|
9
13
|
|
10
14
|
RSpec:
|
11
15
|
Enabled: false
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
18-02-2025
|
2
|
+
|
3
|
+
- Version 4.3.9
|
4
|
+
|
5
|
+
- Prevent deprecation warning when using Rails 7.1
|
6
|
+
|
7
|
+
14-09-2023
|
8
|
+
|
9
|
+
- Version 4.3.8
|
10
|
+
|
11
|
+
- FIX: Typo in `group_ids_lookup` option name in Redis params exclusion list that was preventing the use of the redis backend along with this option.
|
12
|
+
- FIX: Removes close_db_connection! deprecation warning.
|
13
|
+
|
1
14
|
28-06-2023
|
2
15
|
|
3
16
|
- Version 4.3.7
|
@@ -35,13 +48,13 @@
|
|
35
48
|
|
36
49
|
06-01-2023
|
37
50
|
|
38
|
-
- Version 4.
|
51
|
+
- Version 4.3.1
|
39
52
|
|
40
53
|
- FIX: Ensure non-long-polling requests are always spaced out
|
41
54
|
|
42
55
|
04-11-2022
|
43
56
|
|
44
|
-
- Version 4.
|
57
|
+
- Version 4.3.0
|
45
58
|
|
46
59
|
- FIX: Add redis gem version 5 support
|
47
60
|
- FEATURE: Allow disabling subscriptions without disabling publication
|
data/Rakefile
CHANGED
@@ -4,12 +4,10 @@ require 'rake/testtask'
|
|
4
4
|
require 'bundler'
|
5
5
|
require 'bundler/gem_tasks'
|
6
6
|
require 'bundler/setup'
|
7
|
-
require 'rubocop/rake_task'
|
8
7
|
require 'yard'
|
9
8
|
|
10
9
|
Bundler.require(:default, :test)
|
11
10
|
|
12
|
-
RuboCop::RakeTask.new
|
13
11
|
YARD::Rake::YardocTask.new
|
14
12
|
|
15
13
|
BACKENDS = Dir["lib/message_bus/backends/*.rb"].map { |file| file.match(%r{backends/(?<backend>.*).rb})[:backend] } - ["base"]
|
@@ -99,5 +97,5 @@ task :performance do
|
|
99
97
|
end
|
100
98
|
end
|
101
99
|
|
102
|
-
desc "Run all tests
|
103
|
-
task default: [:spec, :
|
100
|
+
desc "Run all tests and confirm the documentation compiles without error"
|
101
|
+
task default: [:spec, :test_doc]
|
@@ -48,7 +48,6 @@ module MessageBus
|
|
48
48
|
#
|
49
49
|
# @abstract
|
50
50
|
class Base
|
51
|
-
# rubocop:disable Lint/UnusedMethodArgument
|
52
51
|
|
53
52
|
# Raised to indicate that the concrete backend implementation does not implement part of the API
|
54
53
|
ConcreteClassMustImplementError = Class.new(StandardError)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "redis"
|
4
|
+
require "digest"
|
5
5
|
|
6
6
|
module MessageBus
|
7
7
|
module Backends
|
@@ -49,9 +49,7 @@ module MessageBus
|
|
49
49
|
@redis_config = redis_config.dup
|
50
50
|
@clear_every = redis_config.delete(:clear_every) || 1
|
51
51
|
@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]
|
55
53
|
@max_backlog_size = max_backlog_size
|
56
54
|
@max_global_backlog_size = 2000
|
57
55
|
@max_in_memory_publish_backlog = 1000
|
@@ -61,7 +59,7 @@ module MessageBus
|
|
61
59
|
@pub_redis = nil
|
62
60
|
@subscribed = false
|
63
61
|
# after 7 days inactive backlogs will be removed
|
64
|
-
@max_backlog_age =
|
62
|
+
@max_backlog_age = 604_800
|
65
63
|
end
|
66
64
|
|
67
65
|
# Reconnects to Redis; used after a process fork, typically triggered by a forking webserver
|
@@ -72,9 +70,7 @@ module MessageBus
|
|
72
70
|
|
73
71
|
# (see Base#reset!)
|
74
72
|
def reset!
|
75
|
-
pub_redis.keys("__mb_*").each
|
76
|
-
pub_redis.del k
|
77
|
-
end
|
73
|
+
pub_redis.keys("__mb_*").each { |k| pub_redis.del k }
|
78
74
|
end
|
79
75
|
|
80
76
|
# (see Base#destroy)
|
@@ -85,9 +81,7 @@ module MessageBus
|
|
85
81
|
# 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.
|
86
82
|
# @see Base#expire_all_backlogs!
|
87
83
|
def expire_all_backlogs!
|
88
|
-
pub_redis.keys("__mb_*backlog_n").each
|
89
|
-
pub_redis.del k
|
90
|
-
end
|
84
|
+
pub_redis.keys("__mb_*backlog_n").each { |k| pub_redis.del k }
|
91
85
|
end
|
92
86
|
|
93
87
|
# Note, the script takes care of all expiry of keys, however
|
@@ -157,15 +151,15 @@ LUA
|
|
157
151
|
max_backlog_size,
|
158
152
|
max_global_backlog_size,
|
159
153
|
channel,
|
160
|
-
clear_every
|
154
|
+
clear_every,
|
161
155
|
],
|
162
156
|
keys: [
|
163
157
|
global_id_key,
|
164
158
|
backlog_id_key,
|
165
159
|
backlog_key,
|
166
160
|
global_backlog_key,
|
167
|
-
redis_channel_name
|
168
|
-
]
|
161
|
+
redis_channel_name,
|
162
|
+
],
|
169
163
|
)
|
170
164
|
rescue ::Redis::CommandError => e
|
171
165
|
if queue_in_memory && e.message =~ /READONLY/
|
@@ -173,7 +167,9 @@ LUA
|
|
173
167
|
@in_memory_backlog << [channel, data]
|
174
168
|
if @in_memory_backlog.length > @max_in_memory_publish_backlog
|
175
169
|
@in_memory_backlog.delete_at(0)
|
176
|
-
@logger.warn(
|
170
|
+
@logger.warn(
|
171
|
+
"Dropping old message cause max_in_memory_publish_backlog is full: #{e.message}\n#{e.backtrace.join('\n')}",
|
172
|
+
)
|
177
173
|
end
|
178
174
|
end
|
179
175
|
|
@@ -209,9 +205,7 @@ LUA
|
|
209
205
|
backlog_key = backlog_key(channel)
|
210
206
|
items = redis.zrangebyscore backlog_key, last_id.to_i + 1, "+inf"
|
211
207
|
|
212
|
-
items.map
|
213
|
-
MessageBus::Message.decode(i)
|
214
|
-
end
|
208
|
+
items.map { |i| MessageBus::Message.decode(i) }
|
215
209
|
end
|
216
210
|
|
217
211
|
# (see Base#global_backlog)
|
@@ -254,13 +248,9 @@ LUA
|
|
254
248
|
# we are subscribing on global and global is always going to be bigger than local
|
255
249
|
# so worst case is a replay of a few messages
|
256
250
|
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
|
263
252
|
end
|
253
|
+
global_subscribe(last_id) { |m| yield m if m.channel == channel }
|
264
254
|
end
|
265
255
|
|
266
256
|
# (see Base#global_unsubscribe)
|
@@ -280,42 +270,37 @@ LUA
|
|
280
270
|
|
281
271
|
highest_id = last_id
|
282
272
|
|
283
|
-
clear_backlog =
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
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
|
292
284
|
end
|
293
|
-
end
|
294
285
|
|
295
286
|
begin
|
296
287
|
global_redis = new_redis_connection
|
297
288
|
|
298
|
-
if highest_id
|
299
|
-
clear_backlog.call(&blk)
|
300
|
-
end
|
289
|
+
clear_backlog.call(&blk) if highest_id
|
301
290
|
|
302
291
|
global_redis.subscribe(redis_channel_name) do |on|
|
303
292
|
on.subscribe do
|
304
|
-
if highest_id
|
305
|
-
clear_backlog.call(&blk)
|
306
|
-
end
|
293
|
+
clear_backlog.call(&blk) if highest_id
|
307
294
|
@subscribed = true
|
308
295
|
end
|
309
296
|
|
310
|
-
on.unsubscribe
|
311
|
-
@subscribed = false
|
312
|
-
end
|
297
|
+
on.unsubscribe { @subscribed = false }
|
313
298
|
|
314
299
|
on.message do |_c, m|
|
315
300
|
if m == UNSUB_MESSAGE
|
316
301
|
@subscribed = false
|
317
302
|
global_redis.unsubscribe
|
318
|
-
return
|
303
|
+
return # rubocop:disable Lint/NonLocalExitFromIterator
|
319
304
|
end
|
320
305
|
m = MessageBus::Message.decode m
|
321
306
|
|
@@ -346,29 +331,30 @@ LUA
|
|
346
331
|
private
|
347
332
|
|
348
333
|
def new_redis_connection
|
349
|
-
config =
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
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
|
372
358
|
::Redis.new(config)
|
373
359
|
end
|
374
360
|
|
@@ -399,9 +385,7 @@ LUA
|
|
399
385
|
end
|
400
386
|
|
401
387
|
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
|
405
389
|
|
406
390
|
global_backlog(highest_id).each do |old|
|
407
391
|
if highest_id + 1 == old.global_id
|
@@ -444,19 +428,21 @@ LUA
|
|
444
428
|
if e.message =~ /^READONLY/
|
445
429
|
try_again = true
|
446
430
|
else
|
447
|
-
@logger.warn(
|
431
|
+
@logger.warn(
|
432
|
+
"Dropping undeliverable message: #{e.message}\n#{e.backtrace.join('\n')}",
|
433
|
+
)
|
448
434
|
end
|
449
435
|
rescue => e
|
450
|
-
@logger.warn(
|
436
|
+
@logger.warn(
|
437
|
+
"Dropping undeliverable message: #{e.message}\n#{e.backtrace.join('\n')}",
|
438
|
+
)
|
451
439
|
end
|
452
440
|
|
453
441
|
@in_memory_backlog.delete_at(0) unless try_again
|
454
442
|
end
|
455
443
|
end
|
456
444
|
ensure
|
457
|
-
@lock.synchronize
|
458
|
-
@flush_backlog_thread = nil
|
459
|
-
end
|
445
|
+
@lock.synchronize { @flush_backlog_thread = nil }
|
460
446
|
end
|
461
447
|
|
462
448
|
def cached_eval(redis, script, script_sha1, params)
|
@@ -479,10 +465,10 @@ LUA
|
|
479
465
|
# in case we are not connected to the correct server
|
480
466
|
# which can happen when sharing ips
|
481
467
|
pub_redis.disconnect!
|
482
|
-
pub_redis.set(key,
|
468
|
+
pub_redis.set(key, "1")
|
483
469
|
false
|
484
470
|
rescue ::Redis::CommandError => e
|
485
|
-
|
471
|
+
true if e.message =~ /^READONLY/
|
486
472
|
end
|
487
473
|
end
|
488
474
|
|
@@ -199,7 +199,13 @@ class MessageBus::Rack::Middleware
|
|
199
199
|
# ConnectionManagement in Rails puts a BodyProxy around stuff
|
200
200
|
# this means connections are not returned until rack.async is
|
201
201
|
# closed
|
202
|
-
if defined? ActiveRecord::Base.
|
202
|
+
if defined? ActiveRecord::Base.connection_handler
|
203
|
+
if Gem::Version.new(Rails.version) >= "7.1"
|
204
|
+
ActiveRecord::Base.connection_handler.clear_active_connections!(:all)
|
205
|
+
else
|
206
|
+
ActiveRecord::Base.connection_handler.clear_active_connections!
|
207
|
+
end
|
208
|
+
elsif defined? ActiveRecord::Base.clear_active_connections!
|
203
209
|
ActiveRecord::Base.clear_active_connections!
|
204
210
|
end
|
205
211
|
end
|
data/lib/message_bus/version.rb
CHANGED
data/lib/message_bus.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "monitor"
|
4
|
-
require "set"
|
5
4
|
|
6
5
|
require_relative "message_bus/version"
|
7
6
|
require_relative "message_bus/message"
|
@@ -772,7 +771,7 @@ module MessageBus::Implementation
|
|
772
771
|
globals, locals, local_globals, global_globals = nil
|
773
772
|
|
774
773
|
@mutex.synchronize do
|
775
|
-
return if @destroyed
|
774
|
+
return if @destroyed # rubocop:disable Lint/NonLocalExitFromIterator
|
776
775
|
next unless @subscriptions
|
777
776
|
|
778
777
|
globals = @subscriptions[nil]
|
data/message_bus.gemspec
CHANGED
@@ -35,6 +35,8 @@ Gem::Specification.new do |gem|
|
|
35
35
|
gem.add_development_dependency 'byebug'
|
36
36
|
gem.add_development_dependency 'oj'
|
37
37
|
gem.add_development_dependency 'yard'
|
38
|
-
|
39
|
-
|
38
|
+
|
39
|
+
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
|
40
|
+
gem.add_development_dependency 'rubocop-discourse', '3.8.1'
|
41
|
+
end
|
40
42
|
end
|