pubsubstub 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -2
- data/Gemfile +1 -2
- data/example/config.ru +0 -1
- data/gemfiles/{Gemfile.sinatra-1.x → Gemfile.rack-1.x} +1 -1
- data/gemfiles/{Gemfile.sinatra-1.x.lock → Gemfile.rack-1.x.lock} +3 -10
- data/gemfiles/{Gemfile.sinatra-2.x → Gemfile.rack-2.x} +1 -1
- data/gemfiles/{Gemfile.sinatra-2.x.lock → Gemfile.rack-2.x.lock} +3 -20
- data/lib/pubsubstub/application.rb +19 -3
- data/lib/pubsubstub/publish_action.rb +9 -5
- data/lib/pubsubstub/stream.rb +25 -0
- data/lib/pubsubstub/stream_action.rb +20 -29
- data/lib/pubsubstub/version.rb +1 -1
- data/lib/pubsubstub.rb +2 -2
- data/pubsubstub.gemspec +2 -2
- metadata +10 -16
- data/lib/pubsubstub/action.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dfd7d496f0e2ce3d0a48c9bc7a88a96b53d14150
|
4
|
+
data.tar.gz: c4849cbfb57dc5f9415edd74beb2737592d35921
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6a1b5013dbaef4a34fc7486f9f8095818d599a1083d0361c9e57a9b51698d92048eff0d524d924b6a9826e401583ba572a04c477ba2919c57812d34d02c1212
|
7
|
+
data.tar.gz: 818686cfa16835e0a430c830b4af65fc61820217bc08da80cd9b3128f69f2fa4d8e6828424dcc7b9aa2f2305f34b833744e83cf8e75e193a11587579ee1cecb4
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -3,11 +3,10 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in pubsubstub.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
-
gem 'sinatra', github: 'sinatra/sinatra', ref: 'a5da6fa82c46436f59ae482d07c1752ab908c852'
|
7
6
|
gem "rspec", "3.1.0"
|
8
7
|
gem "pry-byebug"
|
9
8
|
gem "puma", "~> 3.4"
|
10
9
|
gem "thin", "~> 1.6"
|
11
10
|
gem "rack-test"
|
12
11
|
gem "timecop"
|
13
|
-
gem "rake", "~> 10.0"
|
12
|
+
gem "rake", "~> 10.0"
|
data/example/config.ru
CHANGED
@@ -3,7 +3,7 @@ GEM
|
|
3
3
|
specs:
|
4
4
|
byebug (9.0.5)
|
5
5
|
coderay (1.1.1)
|
6
|
-
daemons (1.2.
|
6
|
+
daemons (1.2.4)
|
7
7
|
diff-lcs (1.2.5)
|
8
8
|
eventmachine (1.2.0.1)
|
9
9
|
method_source (0.8.2)
|
@@ -14,10 +14,8 @@ GEM
|
|
14
14
|
pry-byebug (3.4.0)
|
15
15
|
byebug (~> 9.0)
|
16
16
|
pry (~> 0.10)
|
17
|
-
puma (3.
|
17
|
+
puma (3.6.0)
|
18
18
|
rack (1.6.4)
|
19
|
-
rack-protection (1.5.3)
|
20
|
-
rack
|
21
19
|
rack-test (0.6.3)
|
22
20
|
rack (>= 1.0)
|
23
21
|
rake (10.5.0)
|
@@ -34,16 +32,11 @@ GEM
|
|
34
32
|
rspec-mocks (3.1.3)
|
35
33
|
rspec-support (~> 3.1.0)
|
36
34
|
rspec-support (3.1.2)
|
37
|
-
sinatra (1.4.7)
|
38
|
-
rack (~> 1.5)
|
39
|
-
rack-protection (~> 1.4)
|
40
|
-
tilt (>= 1.3, < 3)
|
41
35
|
slop (3.6.0)
|
42
36
|
thin (1.7.0)
|
43
37
|
daemons (~> 1.0, >= 1.0.9)
|
44
38
|
eventmachine (~> 1.0, >= 1.0.4)
|
45
39
|
rack (>= 1, < 3)
|
46
|
-
tilt (2.0.5)
|
47
40
|
timecop (0.8.1)
|
48
41
|
|
49
42
|
PLATFORMS
|
@@ -52,11 +45,11 @@ PLATFORMS
|
|
52
45
|
DEPENDENCIES
|
53
46
|
pry-byebug
|
54
47
|
puma (~> 3.4)
|
48
|
+
rack (< 2)
|
55
49
|
rack-test
|
56
50
|
rake (~> 10.0)
|
57
51
|
redis (~> 3.0)
|
58
52
|
rspec (= 3.1.0)
|
59
|
-
sinatra (< 2)
|
60
53
|
thin (~> 1.6)
|
61
54
|
timecop
|
62
55
|
|
@@ -1,25 +1,12 @@
|
|
1
|
-
GIT
|
2
|
-
remote: git://github.com/sinatra/sinatra.git
|
3
|
-
revision: a5da6fa82c46436f59ae482d07c1752ab908c852
|
4
|
-
ref: a5da6fa82c46436f59ae482d07c1752ab908c852
|
5
|
-
specs:
|
6
|
-
sinatra (2.0.0.pre.alpha)
|
7
|
-
mustermann (~> 0.4)
|
8
|
-
rack (~> 2.0)
|
9
|
-
rack-protection (~> 1.5)
|
10
|
-
tilt (~> 2.0)
|
11
|
-
|
12
1
|
GEM
|
13
2
|
remote: https://rubygems.org/
|
14
3
|
specs:
|
15
4
|
byebug (9.0.5)
|
16
5
|
coderay (1.1.1)
|
17
|
-
daemons (1.2.
|
6
|
+
daemons (1.2.4)
|
18
7
|
diff-lcs (1.2.5)
|
19
8
|
eventmachine (1.2.0.1)
|
20
9
|
method_source (0.8.2)
|
21
|
-
mustermann (0.4.0)
|
22
|
-
tool (~> 0.2)
|
23
10
|
pry (0.10.4)
|
24
11
|
coderay (~> 1.1.0)
|
25
12
|
method_source (~> 0.8.1)
|
@@ -27,10 +14,8 @@ GEM
|
|
27
14
|
pry-byebug (3.4.0)
|
28
15
|
byebug (~> 9.0)
|
29
16
|
pry (~> 0.10)
|
30
|
-
puma (3.
|
17
|
+
puma (3.6.0)
|
31
18
|
rack (2.0.1)
|
32
|
-
rack-protection (1.5.3)
|
33
|
-
rack
|
34
19
|
rack-test (0.6.3)
|
35
20
|
rack (>= 1.0)
|
36
21
|
rake (10.5.0)
|
@@ -52,9 +37,7 @@ GEM
|
|
52
37
|
daemons (~> 1.0, >= 1.0.9)
|
53
38
|
eventmachine (~> 1.0, >= 1.0.4)
|
54
39
|
rack (>= 1, < 3)
|
55
|
-
tilt (2.0.5)
|
56
40
|
timecop (0.8.1)
|
57
|
-
tool (0.2.3)
|
58
41
|
|
59
42
|
PLATFORMS
|
60
43
|
ruby
|
@@ -62,11 +45,11 @@ PLATFORMS
|
|
62
45
|
DEPENDENCIES
|
63
46
|
pry-byebug
|
64
47
|
puma (~> 3.4)
|
48
|
+
rack (~> 2.0)
|
65
49
|
rack-test
|
66
50
|
rake (~> 10.0)
|
67
51
|
redis (~> 3.0)
|
68
52
|
rspec (= 3.1.0)
|
69
|
-
sinatra!
|
70
53
|
thin (~> 1.6)
|
71
54
|
timecop
|
72
55
|
|
@@ -1,6 +1,22 @@
|
|
1
1
|
module Pubsubstub
|
2
|
-
class Application
|
3
|
-
|
4
|
-
|
2
|
+
class Application
|
3
|
+
def initialize(*)
|
4
|
+
@publish = PublishAction.new
|
5
|
+
@stream = StreamAction.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.call(env)
|
9
|
+
@instance ||= new
|
10
|
+
@instance.call(env)
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
request = Rack::Request.new(env)
|
15
|
+
if request.get?
|
16
|
+
@stream.call(env)
|
17
|
+
else
|
18
|
+
@publish.call(env)
|
19
|
+
end
|
20
|
+
end
|
5
21
|
end
|
6
22
|
end
|
@@ -1,10 +1,14 @@
|
|
1
1
|
module Pubsubstub
|
2
|
-
class PublishAction
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
class PublishAction
|
3
|
+
def initialize(*)
|
4
|
+
end
|
5
|
+
|
6
|
+
def call(env)
|
7
|
+
request = Rack::Request.new(env)
|
8
|
+
channels = (request.params['channels'] || [:default]).each do |channel_name|
|
9
|
+
Pubsubstub.publish(channel_name, request.params['data'], name: request.params['event'])
|
6
10
|
end
|
7
|
-
|
11
|
+
[200, {}, ['']]
|
8
12
|
end
|
9
13
|
end
|
10
14
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Stream
|
2
|
+
def initialize(&callback)
|
3
|
+
@callback = callback
|
4
|
+
@closed = false
|
5
|
+
end
|
6
|
+
|
7
|
+
def close
|
8
|
+
@closed = true
|
9
|
+
end
|
10
|
+
|
11
|
+
def each(&front)
|
12
|
+
@front = front
|
13
|
+
@callback.call(self)
|
14
|
+
close
|
15
|
+
end
|
16
|
+
|
17
|
+
def <<(data)
|
18
|
+
@front.call(data.to_s)
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def closed?
|
23
|
+
@closed
|
24
|
+
end
|
25
|
+
end
|
@@ -1,44 +1,39 @@
|
|
1
1
|
module Pubsubstub
|
2
|
-
class StreamAction
|
2
|
+
class StreamAction
|
3
|
+
HEADERS = {
|
4
|
+
'Content-Type' => 'text/event-stream',
|
5
|
+
'Cache-Control' => 'no-cache',
|
6
|
+
'X-Accel-Buffering' => 'no',
|
7
|
+
'Connection' => 'keep-alive',
|
8
|
+
}.freeze
|
3
9
|
include Logging
|
4
10
|
|
5
11
|
def initialize(*)
|
6
|
-
super
|
7
12
|
@subscriptions = Set.new
|
8
13
|
@mutex = Mutex.new
|
9
14
|
end
|
10
15
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
'Connection' => 'keep-alive',
|
17
|
-
})
|
16
|
+
def call(env)
|
17
|
+
spawn_helper_threads
|
18
|
+
last_event_id = env['HTTP_LAST_EVENT_ID']
|
19
|
+
request = Rack::Request.new(env)
|
20
|
+
channels = (request.params['channels'] || [:default]).map(&Channel.method(:new))
|
18
21
|
|
19
|
-
if event_machine?
|
20
|
-
send_scrollback
|
22
|
+
stream = if event_machine?
|
23
|
+
send_scrollback(channels, last_event_id)
|
21
24
|
else
|
22
|
-
subscribe_connection
|
25
|
+
subscribe_connection(channels, last_event_id)
|
23
26
|
end
|
24
|
-
|
25
|
-
|
26
|
-
def call(*)
|
27
|
-
spawn_helper_threads
|
28
|
-
super
|
27
|
+
[200, HEADERS, stream]
|
29
28
|
end
|
30
29
|
|
31
30
|
private
|
32
31
|
|
33
|
-
def last_event_id
|
34
|
-
request.env['HTTP_LAST_EVENT_ID']
|
35
|
-
end
|
36
|
-
|
37
|
-
def send_scrollback
|
32
|
+
def send_scrollback(channels, last_event_id)
|
38
33
|
scrollback_events = []
|
39
34
|
scrollback_events = channels.flat_map { |c| c.scrollback(since: last_event_id) } if last_event_id
|
40
35
|
scrollback_events = [Pubsubstub.heartbeat_event] if scrollback_events.empty?
|
41
|
-
|
36
|
+
Stream.new do |connection|
|
42
37
|
scrollback_events.each do |event|
|
43
38
|
connection << event.to_message
|
44
39
|
end
|
@@ -49,12 +44,8 @@ module Pubsubstub
|
|
49
44
|
defined?(EventMachine) && EventMachine.reactor_running?
|
50
45
|
end
|
51
46
|
|
52
|
-
def channels
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
def subscribe_connection
|
57
|
-
stream do |connection|
|
47
|
+
def subscribe_connection(channels, last_event_id)
|
48
|
+
Stream.new do |connection|
|
58
49
|
subscription = register(channels, connection)
|
59
50
|
begin
|
60
51
|
subscription.stream(last_event_id)
|
data/lib/pubsubstub/version.rb
CHANGED
data/lib/pubsubstub.rb
CHANGED
@@ -3,7 +3,7 @@ require "mutex_m"
|
|
3
3
|
require "json"
|
4
4
|
require "set"
|
5
5
|
|
6
|
-
require "
|
6
|
+
require "rack/request"
|
7
7
|
require "redis"
|
8
8
|
require "pubsubstub/version"
|
9
9
|
require "pubsubstub/logging"
|
@@ -11,7 +11,7 @@ require "pubsubstub/channel"
|
|
11
11
|
require "pubsubstub/subscriber"
|
12
12
|
require "pubsubstub/subscription"
|
13
13
|
require "pubsubstub/event"
|
14
|
-
require "pubsubstub/
|
14
|
+
require "pubsubstub/stream"
|
15
15
|
require "pubsubstub/stream_action"
|
16
16
|
require "pubsubstub/publish_action"
|
17
17
|
require "pubsubstub/application"
|
data/pubsubstub.gemspec
CHANGED
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency '
|
22
|
-
spec.add_dependency 'redis',
|
21
|
+
spec.add_dependency 'rack'
|
22
|
+
spec.add_dependency 'redis', "~> 3.0"
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.5"
|
25
25
|
end
|
metadata
CHANGED
@@ -1,35 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pubsubstub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Guillaume Malette
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: rack
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '3'
|
19
|
+
version: '0'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
24
|
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
30
|
-
- - "<"
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '3'
|
26
|
+
version: '0'
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: redis
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,17 +74,17 @@ files:
|
|
80
74
|
- example/Gemfile
|
81
75
|
- example/config.ru
|
82
76
|
- example/puma_config.rb
|
83
|
-
- gemfiles/Gemfile.
|
84
|
-
- gemfiles/Gemfile.
|
85
|
-
- gemfiles/Gemfile.
|
86
|
-
- gemfiles/Gemfile.
|
77
|
+
- gemfiles/Gemfile.rack-1.x
|
78
|
+
- gemfiles/Gemfile.rack-1.x.lock
|
79
|
+
- gemfiles/Gemfile.rack-2.x
|
80
|
+
- gemfiles/Gemfile.rack-2.x.lock
|
87
81
|
- lib/pubsubstub.rb
|
88
|
-
- lib/pubsubstub/action.rb
|
89
82
|
- lib/pubsubstub/application.rb
|
90
83
|
- lib/pubsubstub/channel.rb
|
91
84
|
- lib/pubsubstub/event.rb
|
92
85
|
- lib/pubsubstub/logging.rb
|
93
86
|
- lib/pubsubstub/publish_action.rb
|
87
|
+
- lib/pubsubstub/stream.rb
|
94
88
|
- lib/pubsubstub/stream_action.rb
|
95
89
|
- lib/pubsubstub/subscriber.rb
|
96
90
|
- lib/pubsubstub/subscription.rb
|
data/lib/pubsubstub/action.rb
DELETED