muxer 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/muxer/multiplexer.rb +49 -26
- data/lib/muxer/version.rb +1 -1
- data/spec/muxer/multiplexer_spec.rb +21 -1
- data/spec/spec_helper.rb +4 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cbe8738783f06b32d10471723bb67a93db7d4b2
|
4
|
+
data.tar.gz: 1f84ad1ec573791317e96fdd2fc499a88774fe10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5c63ae1b19c7d41b2abdb00d12824c2980939c0fdbec3584b5a79cb7bac2e11dcda357a2751273ab2b6d007c2d942d7ecd2f612a1a45ccf3a7520c52876bf08
|
7
|
+
data.tar.gz: a8c372bea209d0524b67c01d0409089d85a90b4afac42057cdbd588f3cd935fac5b482d628a91a0a46b4721b2ba0729b204d5577d4ddf3147b6b2976a66534b4
|
data/lib/muxer/multiplexer.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Muxer
|
2
2
|
class Multiplexer
|
3
3
|
attr_reader :requests
|
4
|
+
attr_writer :timeout
|
4
5
|
def initialize
|
5
6
|
@requests = []
|
6
7
|
@timeout = nil
|
@@ -14,40 +15,62 @@ module Muxer
|
|
14
15
|
end
|
15
16
|
|
16
17
|
def execute
|
17
|
-
responses = {succeeded: [], failed: [], pending: []}
|
18
|
-
|
19
|
-
finish = Time.now + @timeout if @timeout
|
18
|
+
@responses = {succeeded: [], failed: [], pending: []}
|
19
|
+
@start = Time.now
|
20
20
|
EventMachine.run do
|
21
21
|
requests.each do |request|
|
22
|
-
responses[:pending] << request.process!
|
22
|
+
@responses[:pending] << request.process!
|
23
23
|
end
|
24
24
|
|
25
|
-
EM::PeriodicTimer.new(0.
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
responses[:succeeded] << pending
|
32
|
-
else
|
33
|
-
responses[:failed] << pending
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
if @timeout && Time.now >= finish
|
38
|
-
responses[:pending].each do |pending|
|
39
|
-
responses[:failed] << pending
|
40
|
-
end
|
41
|
-
responses[:pending] = []
|
42
|
-
EM.stop
|
43
|
-
end
|
25
|
+
EM::PeriodicTimer.new(0.001) do
|
26
|
+
process_requests
|
27
|
+
end
|
28
|
+
end
|
29
|
+
@responses
|
30
|
+
end
|
44
31
|
|
45
|
-
|
46
|
-
|
32
|
+
private
|
33
|
+
|
34
|
+
def process_requests
|
35
|
+
process_pending
|
36
|
+
|
37
|
+
process_timeouts
|
38
|
+
|
39
|
+
if @responses[:pending].empty?
|
40
|
+
EM.stop
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def process_pending
|
45
|
+
@responses[:pending].each do |pending|
|
46
|
+
if pending.completed?
|
47
|
+
@responses[:pending].delete(pending)
|
48
|
+
if pending.error.nil?
|
49
|
+
@responses[:succeeded] << pending
|
50
|
+
else
|
51
|
+
@responses[:failed] << pending
|
47
52
|
end
|
48
53
|
end
|
49
54
|
end
|
50
|
-
|
55
|
+
end
|
56
|
+
|
57
|
+
def process_timeouts
|
58
|
+
if @timeout && Time.now >= finish
|
59
|
+
finish_timeouts
|
60
|
+
return
|
61
|
+
end
|
62
|
+
highest_remaining_timeout = @responses[:pending].map(&:timeout).max
|
63
|
+
if highest_remaining_timeout && (@start + highest_remaining_timeout <= Time.now)
|
64
|
+
finish_timeouts
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def finish_timeouts
|
69
|
+
@responses[:pending].each do |pending|
|
70
|
+
@responses[:failed] << pending
|
71
|
+
end
|
72
|
+
@responses[:pending] = []
|
73
|
+
EM.stop
|
51
74
|
end
|
52
75
|
end
|
53
76
|
end
|
data/lib/muxer/version.rb
CHANGED
@@ -1,6 +1,26 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Muxer::Multiplexer do
|
4
|
-
let(:
|
4
|
+
let(:multiplexer) { Muxer::Multiplexer.new }
|
5
5
|
|
6
|
+
it 'kills requests with a timeout' do
|
7
|
+
VCR.use_cassette('muxer/multiplexer/with_a_timeout') do
|
8
|
+
multiplexer.add_url('https://github.com//', 0.0001)
|
9
|
+
response = multiplexer.execute
|
10
|
+
|
11
|
+
expect(response[:succeeded].count).to eq(0)
|
12
|
+
expect(response[:failed].count).to eq(1)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'lets a request wait on the longer one' do
|
17
|
+
VCR.use_cassette('muxer/multiplexer/with_one_timeout') do
|
18
|
+
multiplexer.add_url('https://github.com/', 0.0001)
|
19
|
+
multiplexer.add_url('https://github.com/', 2)
|
20
|
+
response = multiplexer.execute
|
21
|
+
|
22
|
+
expect(response[:succeeded].count).to eq(2)
|
23
|
+
expect(response[:failed].count).to eq(0)
|
24
|
+
end
|
25
|
+
end
|
6
26
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
+
require "codeclimate-test-reporter"
|
2
|
+
CodeClimate::TestReporter.start
|
3
|
+
|
1
4
|
require 'vcr'
|
2
5
|
require 'pry'
|
3
6
|
require File.join(__FILE__, "..","..","lib","muxer")
|
4
7
|
|
5
|
-
require "codeclimate-test-reporter"
|
6
|
-
CodeClimate::TestReporter.start
|
7
|
-
|
8
8
|
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
9
9
|
RSpec.configure do |config|
|
10
10
|
# rspec-expectations config goes here. You can use an alternate
|
@@ -34,6 +34,7 @@ RSpec.configure do |config|
|
|
34
34
|
VCR.configure do |c|
|
35
35
|
c.cassette_library_dir = 'spec/cassettes'
|
36
36
|
c.hook_into :webmock
|
37
|
+
c.ignore_hosts 'codeclimate.com', 'github.com'
|
37
38
|
end
|
38
39
|
# The settings below are suggested to provide a good initial experience
|
39
40
|
# with RSpec, but feel free to customize to your heart's content.
|