message_bus 3.3.7 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +1 -8
  3. data/.github/workflows/ci.yml +50 -20
  4. data/.prettierrc +1 -0
  5. data/CHANGELOG +99 -46
  6. data/README.md +31 -53
  7. data/Rakefile +22 -22
  8. data/docker-compose.yml +1 -1
  9. data/lib/message_bus/backends/base.rb +14 -0
  10. data/lib/message_bus/backends/memory.rb +13 -0
  11. data/lib/message_bus/backends/postgres.rb +55 -22
  12. data/lib/message_bus/backends/redis.rb +17 -1
  13. data/lib/message_bus/client.rb +26 -22
  14. data/lib/message_bus/distributed_cache.rb +1 -0
  15. data/lib/message_bus/rack/middleware.rb +0 -6
  16. data/lib/message_bus/rack/thin_ext.rb +1 -0
  17. data/lib/message_bus/version.rb +1 -1
  18. data/lib/message_bus.rb +53 -71
  19. data/message_bus.gemspec +4 -3
  20. data/package.json +2 -5
  21. data/spec/helpers.rb +6 -1
  22. data/spec/lib/fake_async_middleware.rb +1 -0
  23. data/spec/lib/message_bus/backend_spec.rb +20 -3
  24. data/spec/lib/message_bus/client_spec.rb +1 -0
  25. data/spec/lib/message_bus/connection_manager_spec.rb +4 -0
  26. data/spec/lib/message_bus/multi_process_spec.rb +21 -10
  27. data/spec/lib/message_bus/rack/middleware_spec.rb +2 -49
  28. data/spec/lib/message_bus/timer_thread_spec.rb +1 -5
  29. data/spec/lib/message_bus_spec.rb +12 -3
  30. data/spec/performance/backlog.rb +80 -0
  31. data/spec/performance/publish.rb +4 -4
  32. data/spec/spec_helper.rb +1 -1
  33. data/vendor/assets/javascripts/message-bus-ajax.js +38 -0
  34. data/vendor/assets/javascripts/message-bus.js +549 -0
  35. metadata +8 -31
  36. data/assets/application.jsx +0 -121
  37. data/assets/babel.min.js +0 -25
  38. data/assets/react-dom.js +0 -19851
  39. data/assets/react.js +0 -3029
  40. data/examples/diagnostics/Gemfile +0 -6
  41. data/examples/diagnostics/config.ru +0 -22
  42. data/lib/message_bus/diagnostics.rb +0 -62
  43. data/lib/message_bus/rack/diagnostics.rb +0 -120
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16601ac02268a477a94bf0347922162f66ce17dfdba1c4c655d576b46e78a79c
4
- data.tar.gz: 746e2295f873d9cfe75c28b079b406bb42329a992146d414c63292c2148a8f48
3
+ metadata.gz: 11a14ba0ec9ad142d3335fb7eb764be8e9b975a8dc8367fd4bf359e128a56095
4
+ data.tar.gz: bf41cede85c8e75e56829447cd3cf9ed6cd531ba27fcddfbccb6043f3a82da34
5
5
  SHA512:
6
- metadata.gz: 3d048dde14c63d5abdda283571cb9c2be1b7fc809a94c7f0b4ec2eaa4785da03bc47535264741f65cfa1d66c75e1b0169ea0de39449456b9c791ba91518d62e9
7
- data.tar.gz: 0cfbed1cab954e33805c94c7662c9094276615d5f338168da48d26f033da0f42685024b87aaee64d221e192d0592cd655fba7473da8d973616b218ce2858b045
6
+ metadata.gz: 23c1d181028194c16e98c5a3df42e445458aef4779c97ae2d6d5100adf5053680f62ea4c914bea7c84c124bd453127e141c68ffeb1c742cc208e1d040bc8558f
7
+ data.tar.gz: 7efffe711fac2520356b115820acba2d4206d53f35017ba64c86e3c571a85fb423e943cb9301a03d3b47d6f442f1626dea0495c0242220283685eaa944c890b6
data/.eslintrc.js CHANGED
@@ -10,12 +10,5 @@ module.exports = {
10
10
  sourceType: 'module',
11
11
  },
12
12
  rules: {},
13
- ignorePatterns: [
14
- '/vendor',
15
- '/doc',
16
- '/assets/babel.min.js',
17
- '/assets/jquery-1.8.2.js',
18
- '/assets/react-dom.js',
19
- '/assets/react.js',
20
- ],
13
+ ignorePatterns: ['/vendor', '/doc', '/assets/jquery-1.8.2.js'],
21
14
  };
@@ -1,16 +1,10 @@
1
- name: Message Bus Tests
1
+ name: CI
2
2
 
3
3
  on:
4
- pull_request:
5
4
  push:
6
5
  branches:
7
6
  - main
8
-
9
- env:
10
- PGHOST: localhost
11
- PGPORT: 5432
12
- PGPASSWORD: postgres
13
- PGUSER: postgres
7
+ pull_request:
14
8
 
15
9
  jobs:
16
10
  build:
@@ -18,10 +12,22 @@ jobs:
18
12
  name: Ruby ${{ matrix.ruby }} (redis ${{ matrix.redis }})
19
13
  timeout-minutes: 10
20
14
 
15
+ env:
16
+ PGHOST: localhost
17
+ PGPASSWORD: postgres
18
+ PGUSER: postgres
19
+
20
+ strategy:
21
+ fail-fast: false
22
+ matrix:
23
+ ruby: [2.6, 2.7, '3.0', 3.1]
24
+ redis: [5, 6]
25
+
21
26
  services:
22
27
  postgres:
23
- image: postgres:14.0
28
+ image: postgres:14
24
29
  env:
30
+ POSTGRES_DB: message_bus_test
25
31
  POSTGRES_PASSWORD: postgres
26
32
  ports:
27
33
  - 5432:5432
@@ -36,19 +42,13 @@ jobs:
36
42
  --health-timeout 5s
37
43
  --health-retries 5
38
44
 
39
- strategy:
40
- fail-fast: false
41
- matrix:
42
- ruby: ["3.0", "2.7", "2.6"]
43
- redis: ["5", "6"]
44
-
45
45
  steps:
46
46
  - uses: actions/checkout@v2
47
47
 
48
48
  - uses: ruby/setup-ruby@v1
49
49
  with:
50
50
  ruby-version: ${{ matrix.ruby }}
51
- bundler-cache: true # 'bundle install' and cache
51
+ bundler-cache: true
52
52
 
53
53
  - name: Set up Node.js
54
54
  uses: actions/setup-node@v2
@@ -59,13 +59,43 @@ jobs:
59
59
  - name: Setup npm
60
60
  run: npm install
61
61
 
62
- - name: Create Database
63
- run: |
64
- createdb message_bus_test
65
-
66
62
  - name: Tests
63
+ env:
64
+ TESTOPTS: --verbose
67
65
  run: bundle exec rake
68
66
  timeout-minutes: 3
69
67
 
70
68
  - name: Linting
71
69
  run: npx eslint .
70
+
71
+ publish:
72
+ if: github.event_name == 'push' && github.ref == 'refs/heads/main'
73
+ needs: build
74
+ runs-on: ubuntu-latest
75
+
76
+ steps:
77
+ - uses: actions/checkout@v2
78
+
79
+ - name: Release gem
80
+ uses: discourse/publish-rubygems-action@v2
81
+ id: publish-gem
82
+ env:
83
+ RUBYGEMS_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}
84
+ GIT_EMAIL: team@discourse.org
85
+ GIT_NAME: discoursebot
86
+
87
+ - name: Update package version
88
+ if: steps.publish-gem.outputs.new_version == 'true'
89
+ run: |
90
+ VERSION=$(ruby -r './lib/message_bus/version' -e 'puts MessageBus::VERSION')
91
+ sed -i "s/0.0.0-version-placeholder/$VERSION/" package.json
92
+ git config --global user.email "ci@ci.invalid"
93
+ git config --global user.name "Discourse CI"
94
+ git add package.json
95
+ git commit -m 'bump'
96
+
97
+ - name: Publish package
98
+ uses: JS-DevTools/npm-publish@v1
99
+ if: steps.publish-gem.outputs.new_version == 'true'
100
+ with:
101
+ token: ${{ secrets.NPM_TOKEN }}
data/.prettierrc ADDED
@@ -0,0 +1 @@
1
+ {}
data/CHANGELOG CHANGED
@@ -1,3 +1,34 @@
1
+ 16-02-2022
2
+
3
+ - Version 4.1.0
4
+
5
+ - PERF: Optimize Client#backlog for up-to-date clients
6
+
7
+ Also introduces a new MessageBus#last_ids(*channels) api for fetching the last_ids of
8
+ multiple channels simultaneously
9
+
10
+ 11-01-2022
11
+
12
+ - Version 4.0.0
13
+
14
+ - DEV: Remove backend diagnostics
15
+ - DEV: Rename reliable_pub_sub to backend_instance
16
+ - FIX: `destroy` following `after_fork` could thread lock
17
+
18
+ 31-12-2021
19
+
20
+ - Version 3.4.0
21
+
22
+ - FEATURE: Remove process auto-termination on missed keepalives
23
+
24
+ 20-12-2021
25
+
26
+ - Version 3.3.8
27
+
28
+ - FIX: Restore dist js files to fix a regression in 3.3.7
29
+ - FIX: Stop various thread/pg connection leaks
30
+ - DEV: Fix ruby warnings
31
+
1
32
  15-12-2021
2
33
 
3
34
  - Version 3.3.7
@@ -63,14 +94,14 @@
63
94
 
64
95
  - Version 3.3.0
65
96
 
66
- - FEATURE: `MessageBus.base_route=` to alter the route that message bus will listen on.
97
+ - FEATURE: `MessageBus.base_route=` to alter the route that message bus will listen on.
67
98
 
68
99
  07-05-2020
69
100
 
70
101
  - Version 3.2.0
71
102
 
72
- - FIX: compatibility with Rails 6.0.3, note: apps without ActionDispatch::Flash may stop working after this upgrade
73
- to correct this disable middleware injection with `config.skip_message_bus_middleware = true` and configure middleware by hand with `app.middleware.use(MessageBus::Rack::Middleware)`
103
+ - FIX: compatibility with Rails 6.0.3, note: apps without ActionDispatch::Flash may stop working after this upgrade
104
+ to correct this disable middleware injection with `config.skip_message_bus_middleware = true` and configure middleware by hand with `app.middleware.use(MessageBus::Rack::Middleware)`
74
105
 
75
106
  28-04-2020
76
107
 
@@ -105,7 +136,6 @@
105
136
 
106
137
  - FIX: In the redis backend make the `is_readonly?` method compatible with the redis gem both pre and post v4.0 when the `client` attribute was removed
107
138
 
108
-
109
139
  30-04-2019
110
140
 
111
141
  - Version 2.2.1
@@ -136,7 +166,7 @@
136
166
  - Version 2.2.0.pre
137
167
 
138
168
  - FIX: In redis backend we now expire the key used to track channel id this can cause a redis key leak
139
- with large amounts of subscriptions that go away
169
+ with large amounts of subscriptions that go away
140
170
  - FEATURE: Much extra implementation documentation, and some improvements to usage documentation.
141
171
  - FEATURE: Improvements to development workflow:
142
172
  - Fully docker-based development and testing, with no other dependencies.
@@ -153,44 +183,47 @@
153
183
  - Supports setting backlog size on publication for memory/postgres
154
184
  - FEATURE: `MessageBus.off` now prevents the server subscription from starting up.
155
185
  - FEATURE: Trims unused parts of the public API:
186
+
156
187
  - Methods removed:
157
- * ConnectionManager#stats (never used and the ConnectionManager is not exposed to application code)
158
- * Client#cancel (effectively duplicate of Client#close and the Client is only available via the ConnectionManager, thus not available to application code)
188
+
189
+ - ConnectionManager#stats (never used and the ConnectionManager is not exposed to application code)
190
+ - Client#cancel (effectively duplicate of Client#close and the Client is only available via the ConnectionManager, thus not available to application code)
159
191
 
160
192
  - Methods made private:
161
- * MessageBus::Implementation#encode_channel_name
162
- * MessageBus::Implementation#decode_channel_name
163
- * Client#in_async?
164
- * Client#ensure_closed!
165
- * ConnectionManager#subscribe_client
166
- * Diagnostics.full_process_path
167
- * Diagnostics.hostname
168
- * MessageBus::Rack::Diagnostics#js_asset
169
- * MessageBus::Rack::Diagnostics#generate_script_tag
170
- * MessageBus::Rack::Diagnostics#file_hash
171
- * MessageBus::Rack::Diagnostics#asset_contents
172
- * MessageBus::Rack::Diagnostics#asset_path
173
- * MessageBus::Rack::Diagnostics#index
174
- * MessageBus::Rack::Diagnostics#translate_handlebars
175
- * MessageBus::Rack::Diagnostics#indent
176
- * MessageBus::Rack::Middleware#start_listener
177
- * MessageBus::Rack::Middleware#close_db_connection!
178
- * MessageBus::Rack::Middleware#add_client_with_timeout
193
+
194
+ - MessageBus::Implementation#encode_channel_name
195
+ - MessageBus::Implementation#decode_channel_name
196
+ - Client#in_async?
197
+ - Client#ensure_closed!
198
+ - ConnectionManager#subscribe_client
199
+ - Diagnostics.full_process_path
200
+ - Diagnostics.hostname
201
+ - MessageBus::Rack::Diagnostics#js_asset
202
+ - MessageBus::Rack::Diagnostics#generate_script_tag
203
+ - MessageBus::Rack::Diagnostics#file_hash
204
+ - MessageBus::Rack::Diagnostics#asset_contents
205
+ - MessageBus::Rack::Diagnostics#asset_path
206
+ - MessageBus::Rack::Diagnostics#index
207
+ - MessageBus::Rack::Diagnostics#translate_handlebars
208
+ - MessageBus::Rack::Diagnostics#indent
209
+ - MessageBus::Rack::Middleware#start_listener
210
+ - MessageBus::Rack::Middleware#close_db_connection!
211
+ - MessageBus::Rack::Middleware#add_client_with_timeout
179
212
 
180
213
  - Methods switched from protected to private:
181
- * MessageBus::Implementation#global?
182
- * MessageBus::Implementation#decode_message!
183
- * MessageBus::Implementation#replay_backlog
184
- * MessageBus::Implementation#subscribe_impl
185
- * MessageBus::Implementation#unsubscribe_impl
186
- * MessageBus::Implementation#ensure_subscriber_thread
187
- * MessageBus::Implementation#new_subscriber_thread
188
- * MessageBus::Implementation#global_subscribe_thread
189
- * MessageBus::Implementation#multi_each
190
- * Client#write_headers
191
- * Client#write_chunk
192
- * Client#write_and_close
193
- * Client#messages_to_json
214
+ - MessageBus::Implementation#global?
215
+ - MessageBus::Implementation#decode_message!
216
+ - MessageBus::Implementation#replay_backlog
217
+ - MessageBus::Implementation#subscribe_impl
218
+ - MessageBus::Implementation#unsubscribe_impl
219
+ - MessageBus::Implementation#ensure_subscriber_thread
220
+ - MessageBus::Implementation#new_subscriber_thread
221
+ - MessageBus::Implementation#global_subscribe_thread
222
+ - MessageBus::Implementation#multi_each
223
+ - Client#write_headers
224
+ - Client#write_chunk
225
+ - Client#write_and_close
226
+ - Client#messages_to_json
194
227
 
195
228
  15-10-2018
196
229
 
@@ -219,7 +252,7 @@
219
252
  - Version 2.1.2
220
253
 
221
254
  - FEATURE: minHiddenPollInterval set to 1500ms out of the box, ensures we never do hidden tab
222
- polls at a high rate if tons of tabs are open
255
+ polls at a high rate if tons of tabs are open
223
256
  - FEATURE: added random 500ms to delayed polls to increase tab entropy
224
257
 
225
258
  18-12-2017
@@ -233,7 +266,7 @@
233
266
  - Version 2.1.0
234
267
 
235
268
  - FEATURE: you can now lookup last N messages on channel on subscribe from JavaScript
236
- Subscribe at position (-1 - numberOfMessages) from the client
269
+ Subscribe at position (-1 - numberOfMessages) from the client
237
270
 
238
271
  24-11-2017
239
272
 
@@ -253,8 +286,7 @@
253
286
  - Version 2.0.7
254
287
 
255
288
  - Fix/Feature: use LUA script for publishing to bus, this eliminates a race condition
256
- and ensures that we are never stuck in a multi transaction by mistake
257
-
289
+ and ensures that we are never stuck in a multi transaction by mistake
258
290
 
259
291
  29-09-2017
260
292
 
@@ -282,12 +314,10 @@
282
314
  - Version 2.0.2
283
315
  - Feature: Add on_middleware_error callback for remapping middleware errors to HTTP results
284
316
 
285
-
286
317
  25-07-2016
287
318
 
288
319
  - Feature: Add JavaScript MessageBus.status() function
289
320
 
290
-
291
321
  21-06-2016
292
322
 
293
323
  - Version 2.0.1
@@ -368,8 +398,8 @@
368
398
 
369
399
  - Version 1.1.1
370
400
  - Fix: In multisite config there was no way to specify site for last_id or backlog
371
- to resolve overrides were added to #last_id and #backlog, MessageBus::Client now
372
- uses the new overrides
401
+ to resolve overrides were added to #last_id and #backlog, MessageBus::Client now
402
+ uses the new overrides
373
403
 
374
404
  07-12-2015
375
405
 
@@ -379,86 +409,105 @@
379
409
  - Feature: remove most jQuery dependency from message-bus.js
380
410
 
381
411
  09-07-2015
412
+
382
413
  - Version 1.0.16
383
414
  - Fix: correct edge cases around keepalive checks on bus
384
415
 
385
416
  09-07-2015
417
+
386
418
  - Version 1.0.15
387
419
  - Feature: MessageBus.reliable_pub_sub.max_backlog_age (in secs) configurable (default to 7 days)
388
420
  - Fix: API for MessageBus.backlog("/bla") was returning global backlog by mistake
389
421
  - Change: Max global backlog size reduced to 2000 elements
390
422
 
391
423
  08-06-2015
424
+
392
425
  - Version 1.0.14
393
426
  - Fix: we can not use Thread#kill best keepalive can do is terminate process cleanly
394
427
  - Feature: you can opt-out of keepalive with MessageBus.keepalive_timeout = 0
395
428
 
396
429
  08-06-2015
430
+
397
431
  - Version 1.0.13
398
432
  - Fix: on global subscribe reconnect replay missed messages
399
433
  - Feature: keepalive tests for global subscribe, catches hung redis connections
400
434
 
401
435
  28-05-2015
436
+
402
437
  - Version 1.0.12
403
438
  - Feature: Support client_id targeted message
404
439
 
405
440
  06-05-2015
441
+
406
442
  - Version 1.0.11
407
443
  - Fix: race condition in TimerThread
408
444
 
409
445
  01-05-2015
446
+
410
447
  - Version: 1.0.10
411
448
  - Feature: no longer depends on EventMachine (only used for Thin backend)
412
449
  - Feature: reliable pub sub will queue messages in memory if redis is readonly, configurable
413
450
  - Fix: if redis is flushed we will continue to deliver messages
414
451
 
415
452
  23-03-2015
453
+
416
454
  - Version 1.0.9
417
455
  - Fix: inherit off StandardError not Exception for all exceptions raised
418
456
 
419
457
  20-03-2015
458
+
420
459
  - Version 1.0.8
421
460
  - Fix: aggressive short polling in background
422
461
 
423
462
  16-03-2015
463
+
424
464
  - Version 1.0.7
425
465
  - Feature: added pause and resume methods
426
466
 
427
467
  03-02-2015
468
+
428
469
  - Version 1.0.6
429
470
  - Fix: global backlog not truncating correctly
430
471
 
431
472
  23-09-2014
473
+
432
474
  - Version 1.0.5
433
475
  - Fix: missing custom headers from long polls
434
476
 
435
477
  23-09-2014
478
+
436
479
  - Version 1.0.4
437
480
  - Change: MessageBus.access_control_allow_origin_lookup to extra_response_headers_lookup
438
481
 
439
482
  23-09-2014
483
+
440
484
  - Version 1.0.3
441
485
  - Change: MessageBus.access_control_allow_origin to MessageBus.access_control_allow_origin_lookup
442
486
 
443
487
  23-09-2014
488
+
444
489
  - Version 1.0.2
445
490
  - Feature: MessageBus.access_control_allow_origin to control origin header
446
491
 
447
492
  23-09-2014
493
+
448
494
  - Version 1.0.1
449
495
  - Feature: $.ajax dependency can be passed in.
450
496
  - Feature: unsubscribe accepts a second param for the function to unsubscribe.
451
497
 
452
498
  22-09-2014
499
+
453
500
  - Version 1.0.0
454
501
  - Feature: add backgroundCallbackInterval - interval to send polls when page is in the background
455
502
  - Feature: issue a long poll as soon as page moves into the foreground
456
503
 
457
504
  11-08-2014
505
+
458
506
  - Version 0.9.5
459
507
  - Fix: release db connection a lot earlier for long polling (rails defer closes)
460
508
 
461
509
  13-01-2014
510
+
462
511
  - Version 0.9.4
463
512
  - Added support for /global/ channel to publish messages across a multisite
464
513
  - Cleaned up test harness so it uses local bus as opposed to global
@@ -467,19 +516,23 @@
467
516
  - ensure_reactor could say the reactor is running, but it was not, on first call
468
517
 
469
518
  06-12-2013
519
+
470
520
  - Version 0.9.3.2
471
521
  - Fix permissions in gem
472
522
 
473
523
  05-12-2013
524
+
474
525
  - Version 0.9.3.1
475
526
  - Add MessageBus.diagnostics() for diagnosing bus issues client side
476
527
  - Add more robustness to JavaScript, if callbacks used to fail they would halt the chain
477
528
 
478
529
  03-12-2013
530
+
479
531
  - Version 0.9.3
480
532
  - Remove thin dependency
481
533
  - Improve robustness under failure conditions
482
534
 
483
535
  30-09-2013
536
+
484
537
  - Fix failures in Ruby 1.9
485
538
  - Set up rack hijack by default in light of passengers new setting
data/README.md CHANGED
@@ -26,15 +26,21 @@ MessageBus only support officially supported versions of Ruby; as of [2021-03-31
26
26
 
27
27
  Add this line to your application's Gemfile:
28
28
 
29
- gem 'message_bus'
29
+ ```ruby
30
+ gem 'message_bus'
31
+ ```
30
32
 
31
33
  And then execute:
32
34
 
33
- $ bundle
35
+ ```shell
36
+ $ bundle
37
+ ```
34
38
 
35
39
  Or install it yourself as:
36
40
 
37
- $ gem install message_bus
41
+ ```shell
42
+ $ gem install message_bus
43
+ ```
38
44
 
39
45
  ## Usage
40
46
 
@@ -109,7 +115,7 @@ MessageBus.user_id_lookup do |env|
109
115
  end
110
116
  ```
111
117
 
112
- If both `user_ids` and `group_ids` options are supplied when publishing a message, the message will be targeted at clients with lookup return values that matches on either the `user_ids` **or** the `group_ids` options.
118
+ If both `user_ids` and `group_ids` options are supplied when publishing a message, the message will be targeted at clients with lookup return values that matches on either the `user_ids` **or** the `group_ids` options.
113
119
 
114
120
  ```ruby
115
121
  MessageBus.publish "/channel", "hello", user_ids: [1, 2, 3], group_ids: [1, 2, 3]
@@ -129,7 +135,7 @@ Custom client message filters can be registered via `MessageBus#register_client_
129
135
 
130
136
  For example, ensuring that only messages seen by the server in the last 20 seconds are published to the client:
131
137
 
132
- ```
138
+ ```ruby
133
139
  MessageBus.register_client_message_filter('/test') do |message|
134
140
  (Time.now.to_i - message.data[:published_at]) <= 20
135
141
  end
@@ -163,32 +169,6 @@ curl -H "Content-Type: application/x-www-form-urlencoded" -X POST --data "/messa
163
169
 
164
170
  You should see a reply with the messages of that channel you requested (in this case `/message`) starting at the message ID you requested (`0`). The URL parameter `dlp=t` disables long-polling: we do not want this request to stay open.
165
171
 
166
- ### Diagnostics
167
-
168
- MessageBus comes with a diagnostics interface, which you can access at `/message-bus/_diagnostics`. This interface allows you visibility into the runtime behaviour of message_bus.
169
-
170
- In order to use the diagnostics UI in your application, it is necessary to:
171
-
172
- * Enable it
173
- * Define a user ID for requests
174
- * Define a check for admin role
175
-
176
- as an example, you can do something like this:
177
-
178
- ```ruby
179
- MessageBus.enable_diagnostics # Must be called after `MessageBus.after_fork` if using a forking webserver
180
-
181
- MessageBus.user_id_lookup do |_env|
182
- 1
183
- end
184
-
185
- MessageBus.is_admin_lookup do |_env|
186
- true
187
- end
188
- ```
189
-
190
- Of course, in your real-world application, you would define these values according to your authentication/authorization logic.
191
-
192
172
  ### Transport
193
173
 
194
174
  MessageBus ships with 3 transport mechanisms.
@@ -280,7 +260,7 @@ MessageBus.start(); // call once at startup
280
260
  MessageBus.callbackInterval = 500;
281
261
 
282
262
  // you will get all new messages sent to channel
283
- MessageBus.subscribe("/channel", function(data){
263
+ MessageBus.subscribe("/channel", function (data) {
284
264
  // data shipped from server
285
265
  });
286
266
 
@@ -386,6 +366,16 @@ headers|{}|Extra headers to be include with requests. Properties and values of o
386
366
 
387
367
  message_bus can be configured to use one of several available storage backends, and each has its own configuration options.
388
368
 
369
+ ### Keepalive
370
+
371
+ To ensure correct operation of message_bus, every 60 seconds a message is broadcast to itself. If for any reason the message is not consumed by the same process within 3 keepalive intervals a warning log message is raised.
372
+
373
+ To control keepalive interval use
374
+
375
+ ```ruby
376
+ MessageBus.configure(keepalive_interval: 60)
377
+ ```
378
+
389
379
  ### Redis
390
380
 
391
381
  message_bus supports using Redis as a storage backend, and you can configure message_bus to use redis in `config/initializers/message_bus.rb`, like so:
@@ -400,17 +390,17 @@ The redis client message_bus uses is [redis-rb](https://github.com/redis/redis-r
400
390
 
401
391
  Out of the box Redis keeps track of 2000 messages in the global backlog and 1000 messages in a per-channel backlog. Per-channel backlogs get cleared automatically after 7 days of inactivity.
402
392
 
403
- This is configurable via accessors on the ReliablePubSub instance.
393
+ This is configurable via accessors on the Backend instance.
404
394
 
405
395
  ```ruby
406
396
  # only store 100 messages per channel
407
- MessageBus.reliable_pub_sub.max_backlog_size = 100
397
+ MessageBus.backend_instance.max_backlog_size = 100
408
398
 
409
399
  # only store 100 global messages
410
- MessageBus.reliable_pub_sub.max_global_backlog_size = 100
400
+ MessageBus.backend_instance.max_global_backlog_size = 100
411
401
 
412
402
  # flush per-channel backlog after 100 seconds of inactivity
413
- MessageBus.reliable_pub_sub.max_backlog_age = 100
403
+ MessageBus.backend_instance.max_backlog_age = 100
414
404
  ```
415
405
 
416
406
  ### PostgreSQL
@@ -435,7 +425,6 @@ MessageBus.configure(backend: :memory)
435
425
 
436
426
  The `:clear_every` option supported by the PostgreSQL backend is also supported by the in-memory backend.
437
427
 
438
-
439
428
  ### Transport codecs
440
429
 
441
430
  By default MessageBus serializes messages to the backend using JSON. Under most situation this performs extremely well.
@@ -456,7 +445,7 @@ Keep in mind, much of MessageBus internals and supporting tools expect data to b
456
445
 
457
446
  Another example may be very large and complicated messages where Oj in compatibility mode outperforms JSON. To opt for the Oj codec use:
458
447
 
459
- ```
448
+ ```ruby
460
449
  MessageBus.configure(transport_codec: MessageBus::Codec::Oj.new)
461
450
  ```
462
451
 
@@ -503,7 +492,6 @@ For more information see the [Passenger documentation](https://www.phusionpassen
503
492
 
504
493
  ```ruby
505
494
  # path/to/your/config/puma.rb
506
- require 'message_bus' # omit this line for Rails 5
507
495
  on_worker_boot do
508
496
  MessageBus.after_fork
509
497
  end
@@ -513,7 +501,6 @@ end
513
501
 
514
502
  ```ruby
515
503
  # path/to/your/config/unicorn.rb
516
- require 'message_bus'
517
504
  after_fork do |server, worker|
518
505
  MessageBus.after_fork
519
506
  end
@@ -554,26 +541,21 @@ MessageBus ships with an optional `DistributedCache` API which provides a simple
554
541
  require 'message_bus/distributed_cache'
555
542
 
556
543
  # process 1
557
-
558
544
  cache = MessageBus::DistributedCache.new("animals")
559
545
 
560
546
  # process 2
561
-
562
547
  cache = MessageBus::DistributedCache.new("animals")
563
548
 
564
549
  # process 1
565
-
566
550
  cache["frogs"] = 5
567
551
 
568
552
  # process 2
569
-
570
553
  puts cache["frogs"]
571
554
  # => 5
572
555
 
573
556
  cache["frogs"] = nil
574
557
 
575
558
  # process 1
576
-
577
559
  puts cache["frogs"]
578
560
  # => nil
579
561
  ```
@@ -631,7 +613,7 @@ In e.g. `config/initializers/message_bus.rb`:
631
613
  ```ruby
632
614
  MessageBus.extra_response_headers_lookup do |env|
633
615
  [
634
- ["Access-Control-Allow-Origin", "http://example.com:3000"],
616
+ ["Access-Control-Allow-Origin", "http://example.com:3000"],
635
617
  ]
636
618
  end
637
619
  ```
@@ -692,8 +674,8 @@ In certain conditions, a status message will be delivered and look like this:
692
674
  "message_id": -1,
693
675
  "channel": "/__status",
694
676
  "data": {
695
- "/some/channel":5,
696
- "/other/channel":9
677
+ "/some/channel": 5,
678
+ "/other/channel": 9
697
679
  }
698
680
  }
699
681
  ```
@@ -727,7 +709,7 @@ When submitting a PR, please be sure to include notes on it in the `Unreleased`
727
709
 
728
710
  To run tests you need both Postgres and Redis installed. By default on Redis the tests connect to `localhost:6379` and on Postgres connect the database `localhost:5432/message_bus_test` with the system username; if you wish to override this, you can set alternative values:
729
711
 
730
- ```
712
+ ```shell
731
713
  PGUSER=some_user PGDATABASE=some_db bundle exec rake
732
714
  ```
733
715
 
@@ -742,7 +724,3 @@ While working on documentation, it is useful to automatically re-build it as you
742
724
  ### Benchmarks
743
725
 
744
726
  Some simple benchmarks are implemented in `spec/performance` and can be executed using `rake performance` (or `docker-compose run tests rake performance`). You should run these before and after your changes to avoid introducing performance regressions.
745
-
746
- ### Diagnostics Interface
747
-
748
- It is possible to manually test the diagnostics interface by executing `docker-compose up example` and then `open http://localhost:9292`.