rapns 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,7 +47,7 @@ module Rapns
47
47
  rescue StandardError => e
48
48
  Rapns::Daemon.logger.error(e)
49
49
  ensure
50
- Rapns::Daemon.delivery_queue.signal_waiters_if_empty
50
+ Rapns::Daemon.delivery_queue.handler_available
51
51
  end
52
52
  end
53
53
  end
@@ -1,9 +1,10 @@
1
1
  module Rapns
2
2
  module Daemon
3
3
  class DeliveryQueue
4
- def initialize
4
+ def initialize(num_handlers)
5
+ @num_handlers = num_handlers
5
6
  @queue = Queue.new
6
- @waiting_threads = []
7
+ @feeder_threads = []
7
8
  @mutex = Mutex.new
8
9
  end
9
10
 
@@ -15,23 +16,29 @@ module Rapns
15
16
  @queue.pop
16
17
  end
17
18
 
18
- def signal_waiters_if_empty
19
+ def handler_available
19
20
  @mutex.synchronize do
20
- begin
21
- if @queue.size == 0
22
- t = @waiting_threads.shift
23
- t.wakeup if t
24
- end
25
- rescue ThreadError
26
- retry
27
- end
21
+ signal_feeder if handler_available?
22
+ end
23
+ end
24
+
25
+ def handler_available?
26
+ @queue.size < @num_handlers
27
+ end
28
+
29
+ def signal_feeder
30
+ begin
31
+ t = @feeder_threads.shift
32
+ t.wakeup if t
33
+ rescue ThreadError
34
+ retry
28
35
  end
29
36
  end
30
37
 
31
- def wait_until_empty
38
+ def wait_for_available_handler
32
39
  Thread.exclusive do
33
- if @queue.size > 0
34
- @waiting_threads << Thread.current
40
+ if @queue.size >= @num_handlers
41
+ @feeder_threads << Thread.current
35
42
  Thread.stop
36
43
  end
37
44
  end
@@ -20,7 +20,7 @@ module Rapns
20
20
  Rapns::Daemon.delivery_queue.push(notification)
21
21
  end
22
22
 
23
- Rapns::Daemon.delivery_queue.wait_until_empty
23
+ Rapns::Daemon.delivery_queue.wait_for_available_handler
24
24
  rescue ActiveRecord::StatementInvalid, *ADAPTER_ERRORS => e
25
25
  Rapns::Daemon.logger.error(e)
26
26
  reconnect
data/lib/rapns/daemon.rb CHANGED
@@ -34,7 +34,7 @@ module Rapns
34
34
  self.certificate = Certificate.new(configuration.certificate)
35
35
  certificate.load
36
36
 
37
- self.delivery_queue = DeliveryQueue.new
37
+ self.delivery_queue = DeliveryQueue.new(configuration.connections)
38
38
 
39
39
  unless foreground?
40
40
  daemonize
data/lib/rapns/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rapns
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  describe Rapns::Daemon::DeliveryHandler do
4
4
  before do
5
5
  @notification = Rapns::Notification.create!(:device_token => "a" * 64)
6
- Rapns::Daemon.stub(:delivery_queue).and_return(Rapns::Daemon::DeliveryQueue.new)
6
+ Rapns::Daemon.stub(:delivery_queue).and_return(Rapns::Daemon::DeliveryQueue.new(1))
7
7
  Rapns::Daemon.delivery_queue.push(@notification)
8
8
  @connection = mock("Connection", :connect => nil, :write => nil)
9
9
  Rapns::Daemon::Connection.stub(:new).and_return(@connection)
@@ -59,8 +59,8 @@ describe Rapns::Daemon::DeliveryHandler do
59
59
  @delivery_handler.send(:handle_next_notification)
60
60
  end
61
61
 
62
- it "should signal the feeder if the delivery queue is empty" do
63
- Rapns::Daemon.delivery_queue.should_receive(:signal_waiters_if_empty)
62
+ it "should notify the delivery queue a handler is now available" do
63
+ Rapns::Daemon.delivery_queue.should_receive(:handler_available)
64
64
  @delivery_handler.send(:handle_next_notification)
65
65
  end
66
66
 
@@ -6,7 +6,7 @@ describe Rapns::Daemon::Feeder do
6
6
  @notification = Rapns::Notification.create!(:device_token => "a" * 64)
7
7
  @logger = mock("Logger", :info => nil, :error => nil, :warn => nil)
8
8
  Rapns::Daemon.stub(:logger).and_return(@logger)
9
- @queue = mock(:push => nil, :wait_until_empty => nil)
9
+ @queue = mock(:push => nil, :wait_for_available_handler => nil)
10
10
  Rapns::Daemon.stub(:delivery_queue).and_return(@queue)
11
11
  Rapns::Daemon.stub(:configuration => mock("Configuration", :poll => 2))
12
12
  end
@@ -52,8 +52,8 @@ describe Rapns::Daemon::Feeder do
52
52
  Rapns::Daemon::Feeder.enqueue_notifications
53
53
  end
54
54
 
55
- it "should wait for the delivery queue to be emptied" do
56
- Rapns::Daemon.delivery_queue.should_receive(:wait_until_empty)
55
+ it "should wait for a delivery handler to become available" do
56
+ Rapns::Daemon.delivery_queue.should_receive(:wait_for_available_handler)
57
57
  Rapns::Daemon::Feeder.enqueue_notifications
58
58
  end
59
59
 
@@ -60,6 +60,11 @@ describe Rapns::Daemon, "when starting" do
60
60
  Rapns::Daemon.start("development", {})
61
61
  end
62
62
 
63
+ it "should initialize the delivery queue with the number of connection as its signal point" do
64
+ Rapns::Daemon::DeliveryQueue.should_receive(:new).with(3)
65
+ Rapns::Daemon.start("development", {})
66
+ end
67
+
63
68
  it "should make the connection pool accessible" do
64
69
  Rapns::Daemon.start("development", {})
65
70
  Rapns::Daemon.connection_pool.should == @connection_pool
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-09 00:00:00.000000000Z
12
+ date: 2011-10-14 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Easy to use library for Apple's Push Notification Service with Rails
15
15
  3