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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a41652a44eeac100da5b263e45cacf1d4d18bac
4
- data.tar.gz: 42e617e90df278bb931646a89281fde4e155be51
3
+ metadata.gz: 8aee28c6b8c64e053dfd864eb43e05f4d350606c
4
+ data.tar.gz: 3cf659c4cb47be3b564fe992da89603c6c3649cf
5
5
  SHA512:
6
- metadata.gz: 006202ca3ddeb5670e824319dad63e1e980bc733a8a4a667f8a5faa1544a5f0af6c828515bc8ceb3f38fdf9f8f332fd3c09e4d6a8feb07522bf0dc256bd12253
7
- data.tar.gz: 7f0d305730a3a7824302c87bfc95845f75e68f646b6554d73edcc1484752a9431f4095be73b54292c85a7c06f2e266c77e743830e9877ba8aa89e3de30886244
6
+ metadata.gz: fcc93283eb0ff7d457ab33f4f4c87d1c6969a60b3fc7bda09b21cd4fa88fa198089d1b4317bab95bfb2b88a7ab6df2abb29af1bc42116954fa9bfa23027f84db
7
+ data.tar.gz: 46079754f78b019f506445d46040ff846e02568acd497a053dd0c1067a44a4329b18d5c3beefa8abfab7b0d55dcde9effc5e616112a15f54497a46818ee3313e
data/freddy.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  else
9
9
  spec.name = "freddy"
10
10
  end
11
- spec.version = '0.4.8'
11
+ spec.version = '0.4.9'
12
12
  spec.authors = ["Urmas Talimaa"]
13
13
  spec.email = ["urmas.talimaa@gmail.com"]
14
14
  spec.description = %q{Messaging API}
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 channel, logger, @consume_thread_pool, @producer
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
@@ -7,10 +7,11 @@ require_relative 'consumers/response_consumer'
7
7
 
8
8
  class Freddy
9
9
  class Consumer
10
- def initialize(channel, logger, consume_thread_pool, producer)
11
- @channel, @logger = channel, logger
12
- @tap_into_consumer = Consumers::TapIntoConsumer.new(consume_thread_pool, channel)
13
- @respond_to_consumer = Consumers::RespondToConsumer.new(consume_thread_pool, channel, producer, @logger)
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, channel, producer, logger)
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
- ensure_response_queue_exists
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
- @channel.queue(destination).subscribe do |delivery|
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, channel)
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
- consumer = @mutex.synchronize do
15
- create_consumer(pattern, &block)
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
- def create_consumer(pattern, &block)
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(@topic_exchange, routing_key: pattern)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freddy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Urmas Talimaa