rasti-web-broadcaster 1.1.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e5290c1fe92680bd88bd05f57f91b1d49a6848516865a6da8cfdb8d47cf6f44
4
- data.tar.gz: eef7f1069f999eddd07fe89a7d6059e95bd669fbae2030f75d2fef0b180bf2b5
3
+ metadata.gz: bee6440950bad244f76deef7e4685575da162bc540699b69b638e78c3eefd131
4
+ data.tar.gz: e84320050ffb17592a5b58f2957a1f1e75727a0200916a8beadd5795a0840ebe
5
5
  SHA512:
6
- metadata.gz: db5ca61f44dcb3ebaab5557cca6c245a8e7bccca22833b2aaa0aa38428a0baa5e0c9b0f7c8133ac7fb7e132a2ee173d39e4135a486b747209ed90cadd2d71b60
7
- data.tar.gz: 06bebe40478194b3dceea7fd7a2b670cd75997630170d7ab54b72f7142c57bcdaf3b94a2d6969bf26de1f3c3eeddd581a2bbe338fe205cf251fe14c6cc6db313
6
+ metadata.gz: 4f4b557db3d69f416a6e89a6ba665a6c18c1b9ba1da9332861fac73eb14cc2acfde9336a4d4013a6a0208bb4688dbc3b9f5c59fbad0d6510dd16848df68b7d0f
7
+ data.tar.gz: d4e9d49f0e703e1087bb5fe3d2d9cfc4369ca8ca123af6f0e4b48e6d02d851c8782b5871bbcdd4ed5b37cb1edd3b998e19bb765a9a31d85527bbfa3194fe25b1
data/.travis.yml CHANGED
@@ -1,7 +1,6 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - 2.0
5
4
  - 2.1
6
5
  - 2.2
7
6
  - 2.3
@@ -9,6 +8,7 @@ rvm:
9
8
  - 2.5
10
9
  - 2.6
11
10
  - 2.7
11
+ - 3.0
12
12
  - jruby-9.2.9.0
13
13
  - ruby-head
14
14
  - jruby-head
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Rasti::Web::Broadcaster
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/rasti-web-broadcaster.svg)](https://rubygems.org/gems/rasti-web-broadcaster)
4
- [![Build Status](https://travis-ci.org/gabynaiman/rasti-web-broadcaster.svg?branch=master)](https://travis-ci.org/gabynaiman/rasti-web-broadcaster)
4
+ [![Build Status](https://app.travis-ci.com/gabynaiman/rasti-web-broadcaster.svg?branch=master)](https://app.travis-ci.com/gabynaiman/rasti-web-broadcaster)
5
5
  [![Coverage Status](https://coveralls.io/repos/gabynaiman/rasti-web-broadcaster/badge.svg?branch=master)](https://coveralls.io/r/gabynaiman/rasti-web-broadcaster?branch=master)
6
6
  [![Code Climate](https://codeclimate.com/github/gabynaiman/rasti-web-broadcaster.svg)](https://codeclimate.com/github/gabynaiman/rasti-web-broadcaster)
7
7
 
@@ -30,6 +30,7 @@ Or install it yourself as:
30
30
  Rasti::Web::Broadcaster.configure do |config|
31
31
  config.id = 'AppName'
32
32
  config.redis_settings = "redis://#{ENV['REDIS_HOST']}:#{ENV['REDIS_PORT']}"
33
+ config.keep_alive_interval = 30
33
34
  config.logger = Logger.new "/log/#{ENV['RACK_ENV']}.log"
34
35
  end
35
36
  ```
@@ -0,0 +1,37 @@
1
+ module Rasti
2
+ module Web
3
+ class Broadcaster
4
+ class Timer
5
+ class << self
6
+
7
+ def every(interval, &block)
8
+ Thread.new do
9
+ loop do
10
+ execute_using_time_slot(interval, &block)
11
+ end
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def execute_using_time_slot(interval)
18
+ started_at = Time.now
19
+ yield
20
+
21
+ rescue => ex
22
+ Broadcaster.logger.error(self) { ex }
23
+
24
+ ensure
25
+ elapsed_time = Time.now - started_at
26
+ if elapsed_time > interval
27
+ Broadcaster.logger.warn(self) { "Elapsed time #{elapsed_time}s for interval of #{interval}s" }
28
+ else
29
+ sleep interval - elapsed_time
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,7 +1,7 @@
1
1
  module Rasti
2
2
  module Web
3
3
  class Broadcaster
4
- VERSION = '1.1.2'
4
+ VERSION = '2.0.0'
5
5
  end
6
6
  end
7
7
  end
@@ -4,18 +4,22 @@ require 'class_config'
4
4
 
5
5
  require_relative 'broadcaster/safe_event_machine'
6
6
  require_relative 'broadcaster/safe_rack_lint'
7
+ require_relative 'broadcaster/timer'
7
8
  require_relative 'broadcaster/version'
8
9
 
9
10
  module Rasti
10
11
  module Web
11
12
  class Broadcaster
12
13
 
14
+ KEEP_ALIVE_EVENT = 'keepAlive'
15
+
13
16
  extend ClassConfig
14
17
 
15
- attr_config :id, 'rasti.web.broadcaster'
16
- attr_config :redis_client, Redic
17
- attr_config :redis_settings, 'redis://localhost:6379'
18
- attr_config :logger, Logger.new(STDOUT)
18
+ attr_config :id, 'rasti.web.broadcaster'
19
+ attr_config :redis_client, Redic
20
+ attr_config :redis_settings, 'redis://localhost:6379'
21
+ attr_config :logger, Logger.new(STDOUT)
22
+ attr_config :keep_alive_interval
19
23
 
20
24
  @mutex = Mutex.new
21
25
 
@@ -23,8 +27,8 @@ module Rasti
23
27
 
24
28
  extend Forwardable
25
29
 
26
- def_delegators :broadcaster, :subscribe,
27
- :unsubscribe,
30
+ def_delegators :broadcaster, :subscribe,
31
+ :unsubscribe,
28
32
  :publish
29
33
 
30
34
  private
@@ -40,20 +44,20 @@ module Rasti
40
44
  def initialize(app, headers={})
41
45
  @app = app
42
46
  @headers = headers
47
+ @mutex = Mutex.new
48
+ @subscriptions = {}
49
+
50
+ start_sending_keep_alive_messages
43
51
  end
44
52
 
45
53
  def call(env)
46
54
  if Faye::EventSource.eventsource? env
47
- event_source = Faye::EventSource.new env, headers: @headers
48
- channel = env['PATH_INFO'][1..-1]
55
+ event_source = Faye::EventSource.new env, headers: headers
49
56
 
50
- subscription_id = self.class.subscribe channel do |message|
51
- event_source.send message[:data], event: message[:event],
52
- id: message[:id]
53
- end
57
+ subscription_id = subscribe channel_from(env), event_source
54
58
 
55
59
  event_source.on :close do
56
- self.class.unsubscribe subscription_id
60
+ unsubscribe subscription_id
57
61
  event_source = nil
58
62
  end
59
63
 
@@ -65,7 +69,41 @@ module Rasti
65
69
 
66
70
  private
67
71
 
68
- attr_reader :app
72
+ attr_reader :app, :headers, :mutex, :subscriptions
73
+
74
+ def subscribe(channel, event_source)
75
+ subscription_id = self.class.subscribe channel do |message|
76
+ send_message(event_source, **message)
77
+ end
78
+
79
+ mutex.synchronize { subscriptions[subscription_id] = event_source }
80
+
81
+ subscription_id
82
+ end
83
+
84
+ def unsubscribe(subscription_id)
85
+ self.class.unsubscribe subscription_id
86
+ mutex.synchronize { subscriptions.delete subscription_id }
87
+ end
88
+
89
+ def send_message(event_source, data:, event: nil, id: nil)
90
+ event_source.send data, event: event, id: id
91
+ end
92
+
93
+ def start_sending_keep_alive_messages
94
+ if self.class.keep_alive_interval
95
+ Timer.every self.class.keep_alive_interval do
96
+ subscriptions.each do |subscription_id, event_source|
97
+ self.class.logger.debug(self.class) { "Sending keep alive to #{subscription_id}" }
98
+ send_message event_source, data: '', event: KEEP_ALIVE_EVENT
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ def channel_from(env)
105
+ env['PATH_INFO'][1..-1]
106
+ end
69
107
 
70
108
  end
71
109
  end
@@ -47,7 +47,7 @@ describe Rasti::Web::Broadcaster do
47
47
  event_source = nil
48
48
  events = []
49
49
 
50
- env['async.callback'] = proc do |(status, headers, body)|
50
+ env['async.callback'] = proc do |(_status, _headers, body)|
51
51
  event_source = body.instance_variable_get(:@socket_object)
52
52
  body.each { |e| events << e }
53
53
  end
@@ -60,14 +60,15 @@ describe Rasti::Web::Broadcaster do
60
60
  Rasti::Web::Broadcaster.publish 'channel_2', data: 'message 2'
61
61
  Rasti::Web::Broadcaster.publish 'channel_1', data: 'message 3'
62
62
 
63
- wait_for { events.count == 3 }
63
+ wait_for { events.count == 4 }
64
64
 
65
65
  event_source.close
66
66
 
67
67
  events.must_equal [
68
68
  event_headers,
69
69
  event_for(data: 'message 1', event: 'event_1', id: 1),
70
- event_for(data: 'message 3')
70
+ event_for(data: 'message 3'),
71
+ event_for(event: Rasti::Web::Broadcaster::KEEP_ALIVE_EVENT)
71
72
  ]
72
73
  end
73
74
 
@@ -5,4 +5,7 @@ require 'pry-nav'
5
5
  require 'rasti-web-broadcaster'
6
6
  require 'rack/test'
7
7
 
8
- Rasti::Web::Broadcaster.logger.level = Logger::ERROR
8
+ Rasti::Web::Broadcaster.configure do |config|
9
+ config.logger = Logger.new '/dev/null'
10
+ config.keep_alive_interval = 0.1
11
+ end
@@ -0,0 +1,48 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Web::Broadcaster::Timer do
4
+
5
+ let(:timer) { Rasti::Web::Broadcaster::Timer }
6
+
7
+ it 'Tick' do
8
+ count = 0
9
+
10
+ thread = timer.every(0.02) do
11
+ count += 1
12
+ end
13
+
14
+ sleep 0.07
15
+ thread.exit
16
+
17
+ count.must_equal 4
18
+ end
19
+
20
+ it 'Interval exceded' do
21
+ count = 0
22
+
23
+ thread = timer.every(0.02) do
24
+ sleep 0.04 if count == 1
25
+ count += 1
26
+ end
27
+
28
+ sleep 0.07
29
+ thread.exit
30
+
31
+ count.must_equal 3
32
+ end
33
+
34
+ it 'Error safe' do
35
+ count = 0
36
+
37
+ thread = timer.every(0.02) do
38
+ count += 1
39
+ raise 'Unexpected error' if count == 2
40
+ end
41
+
42
+ sleep 0.05
43
+ thread.exit
44
+
45
+ count.must_equal 3
46
+ end
47
+
48
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-web-broadcaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-26 00:00:00.000000000 Z
11
+ date: 2022-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faye-websocket
@@ -197,11 +197,13 @@ files:
197
197
  - lib/rasti/web/broadcaster.rb
198
198
  - lib/rasti/web/broadcaster/safe_event_machine.rb
199
199
  - lib/rasti/web/broadcaster/safe_rack_lint.rb
200
+ - lib/rasti/web/broadcaster/timer.rb
200
201
  - lib/rasti/web/broadcaster/version.rb
201
202
  - rasti-web-broadcaster.gemspec
202
203
  - spec/coverage_helper.rb
203
204
  - spec/middleware_spec.rb
204
205
  - spec/minitest_helper.rb
206
+ - spec/timer_spec.rb
205
207
  homepage: https://github.com/gabynaiman/rasti-web-broadcaster
206
208
  licenses:
207
209
  - MIT
@@ -221,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
221
223
  - !ruby/object:Gem::Version
222
224
  version: '0'
223
225
  requirements: []
224
- rubygems_version: 3.0.6
226
+ rubygems_version: 3.2.3
225
227
  signing_key:
226
228
  specification_version: 4
227
229
  summary: Rack middleware for server sent events
@@ -229,3 +231,4 @@ test_files:
229
231
  - spec/coverage_helper.rb
230
232
  - spec/middleware_spec.rb
231
233
  - spec/minitest_helper.rb
234
+ - spec/timer_spec.rb