freddy 0.4.8 → 0.4.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/freddy.gemspec +1 -1
- data/lib/freddy.rb +1 -1
- data/lib/freddy/consumer.rb +7 -6
- data/lib/freddy/consumers/respond_to_consumer.rb +5 -17
- data/lib/freddy/consumers/tap_into_consumer.rb +10 -18
- data/spec/integration/concurrency_spec.rb +11 -0
- 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: 8aee28c6b8c64e053dfd864eb43e05f4d350606c
|
4
|
+
data.tar.gz: 3cf659c4cb47be3b564fe992da89603c6c3649cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcc93283eb0ff7d457ab33f4f4c87d1c6969a60b3fc7bda09b21cd4fa88fa198089d1b4317bab95bfb2b88a7ab6df2abb29af1bc42116954fa9bfa23027f84db
|
7
|
+
data.tar.gz: 46079754f78b019f506445d46040ff846e02568acd497a053dd0c1067a44a4329b18d5c3beefa8abfab7b0d55dcde9effc5e616112a15f54497a46818ee3313e
|
data/freddy.gemspec
CHANGED
data/lib/freddy.rb
CHANGED
@@ -42,7 +42,7 @@ class Freddy
|
|
42
42
|
@channel = connection.create_channel
|
43
43
|
@consume_thread_pool = Thread.pool(max_concurrency)
|
44
44
|
@producer = Producer.new channel, logger
|
45
|
-
@consumer = Consumer.new
|
45
|
+
@consumer = Consumer.new logger, @consume_thread_pool, @producer, @connection
|
46
46
|
@request = Request.new channel, logger, @producer, @consumer
|
47
47
|
end
|
48
48
|
private :initialize
|
data/lib/freddy/consumer.rb
CHANGED
@@ -7,10 +7,11 @@ require_relative 'consumers/response_consumer'
|
|
7
7
|
|
8
8
|
class Freddy
|
9
9
|
class Consumer
|
10
|
-
def initialize(
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
10
|
+
def initialize(logger, consume_thread_pool, producer, connection)
|
11
|
+
@logger = logger
|
12
|
+
@connection = connection
|
13
|
+
@tap_into_consumer = Consumers::TapIntoConsumer.new(consume_thread_pool)
|
14
|
+
@respond_to_consumer = Consumers::RespondToConsumer.new(consume_thread_pool, producer, @logger)
|
14
15
|
@response_consumer = Consumers::ResponseConsumer.new(@logger)
|
15
16
|
end
|
16
17
|
|
@@ -21,12 +22,12 @@ class Freddy
|
|
21
22
|
|
22
23
|
def tap_into(pattern, &block)
|
23
24
|
@logger.debug "Tapping into messages that match #{pattern}"
|
24
|
-
@tap_into_consumer.consume(pattern, &block)
|
25
|
+
@tap_into_consumer.consume(pattern, @connection.create_channel, &block)
|
25
26
|
end
|
26
27
|
|
27
28
|
def respond_to(destination, &block)
|
28
29
|
@logger.info "Listening for requests on #{destination}"
|
29
|
-
@respond_to_consumer.consume(destination, &block)
|
30
|
+
@respond_to_consumer.consume(destination, @connection.create_channel, &block)
|
30
31
|
end
|
31
32
|
end
|
32
33
|
end
|
@@ -1,18 +1,14 @@
|
|
1
1
|
class Freddy
|
2
2
|
module Consumers
|
3
3
|
class RespondToConsumer
|
4
|
-
def initialize(consume_thread_pool,
|
4
|
+
def initialize(consume_thread_pool, producer, logger)
|
5
5
|
@consume_thread_pool = consume_thread_pool
|
6
|
-
@channel = channel
|
7
6
|
@producer = producer
|
8
7
|
@logger = logger
|
9
|
-
@response_queue_lock = Mutex.new
|
10
8
|
end
|
11
9
|
|
12
|
-
def consume(destination, &block)
|
13
|
-
|
14
|
-
|
15
|
-
consumer = consume_from_destination(destination) do |delivery|
|
10
|
+
def consume(destination, channel, &block)
|
11
|
+
consumer = consume_from_destination(destination, channel) do |delivery|
|
16
12
|
log_receive_event(destination, delivery)
|
17
13
|
|
18
14
|
handler_class = MessageHandlers.for_type(delivery.type)
|
@@ -27,8 +23,8 @@ class Freddy
|
|
27
23
|
|
28
24
|
private
|
29
25
|
|
30
|
-
def consume_from_destination(destination, &block)
|
31
|
-
|
26
|
+
def consume_from_destination(destination, channel, &block)
|
27
|
+
channel.queue(destination).subscribe do |delivery|
|
32
28
|
process_message(delivery, &block)
|
33
29
|
end
|
34
30
|
end
|
@@ -46,14 +42,6 @@ class Freddy
|
|
46
42
|
@logger.debug "Received message on #{destination} with payload #{delivery.payload} with correlation_id #{delivery.correlation_id}"
|
47
43
|
end
|
48
44
|
end
|
49
|
-
|
50
|
-
def ensure_response_queue_exists
|
51
|
-
return @response_queue if defined?(@response_queue)
|
52
|
-
|
53
|
-
@response_queue_lock.synchronize do
|
54
|
-
@response_queue = @channel.queue('', exclusive: true)
|
55
|
-
end
|
56
|
-
end
|
57
45
|
end
|
58
46
|
end
|
59
47
|
end
|
@@ -1,18 +1,15 @@
|
|
1
|
-
require 'thread'
|
2
|
-
|
3
1
|
class Freddy
|
4
2
|
module Consumers
|
5
3
|
class TapIntoConsumer
|
6
|
-
def initialize(consume_thread_pool
|
4
|
+
def initialize(consume_thread_pool)
|
7
5
|
@consume_thread_pool = consume_thread_pool
|
8
|
-
@channel = channel
|
9
|
-
@topic_exchange = @channel.topic(Freddy::FREDDY_TOPIC_EXCHANGE_NAME)
|
10
|
-
@mutex = Mutex.new
|
11
6
|
end
|
12
7
|
|
13
|
-
def consume(pattern, &block)
|
14
|
-
|
15
|
-
|
8
|
+
def consume(pattern, channel, &block)
|
9
|
+
queue = create_queue(pattern, channel)
|
10
|
+
|
11
|
+
consumer = queue.subscribe do |delivery|
|
12
|
+
process_message(delivery, &block)
|
16
13
|
end
|
17
14
|
|
18
15
|
ResponderHandler.new(consumer, @consume_thread_pool)
|
@@ -20,17 +17,12 @@ class Freddy
|
|
20
17
|
|
21
18
|
private
|
22
19
|
|
20
|
+
def create_queue(pattern, channel)
|
21
|
+
topic_exchange = channel.topic(Freddy::FREDDY_TOPIC_EXCHANGE_NAME)
|
23
22
|
|
24
|
-
|
25
|
-
create_queue(pattern).subscribe do |delivery|
|
26
|
-
process_message(delivery, &block)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def create_queue(pattern)
|
31
|
-
@channel
|
23
|
+
channel
|
32
24
|
.queue('', exclusive: true)
|
33
|
-
.bind(
|
25
|
+
.bind(topic_exchange, routing_key: pattern)
|
34
26
|
end
|
35
27
|
|
36
28
|
def process_message(delivery, &block)
|
@@ -73,4 +73,15 @@ describe 'Concurrency' do
|
|
73
73
|
|
74
74
|
expect(results.count).to eq(10)
|
75
75
|
end
|
76
|
+
|
77
|
+
it 'supports adding multiple #respond_to listeners' do
|
78
|
+
results = 10.times.map do |id|
|
79
|
+
Thread.new do
|
80
|
+
freddy1.respond_to "respond_to.listener.#{id}" do
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end.map(&:join)
|
84
|
+
|
85
|
+
expect(results.count).to eq(10)
|
86
|
+
end
|
76
87
|
end
|