Skip to content

Commit

Permalink
Merge pull request #2 from mrnugget/connection_pool
Browse files Browse the repository at this point in the history
Make pling thread safe by using a connection pool and thread local storage
  • Loading branch information
fabrik42 committed Jul 2, 2014
2 parents 8474922 + aad18bc commit 9ee6bf6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Expand Up @@ -5,11 +5,9 @@ language: ruby
rvm:
- ruby-head
- 1.9.3
- 1.8.7
- ree
- jruby
- jruby-19mode
- rbx-19mode
- rbx-2
matrix:
allow_failures:
- rvm: ruby-head
- rvm: ruby-head
24 changes: 20 additions & 4 deletions lib/pling/apn/gateway.rb
@@ -1,6 +1,7 @@
require 'socket'
require 'openssl'
require 'json'
require 'connection_pool'

module Pling
module APN
Expand Down Expand Up @@ -33,10 +34,10 @@ def initialize(configuration)
setup!
end

##
##
# Establishes a new connection if connection is not available or closed
def setup!
connection.reopen if connection.closed?
setup_connection_pool
end

##
Expand Down Expand Up @@ -75,18 +76,33 @@ def default_configuration
super.merge(
:host => 'gateway.push.apple.com',
:port => 2195,
:payload => false
:payload => false,
:pool_size => 3,
:timeout => 5
)
end

def connection
@connection ||= Connection.new(
@connection
end

def setup_connection_pool
@connection ||= connection_pool
end

def new_connection
Connection.new(
:host => configuration[:host],
:port => configuration[:port],
:certificate => configuration[:certificate]
)
end

def connection_pool
ConnectionPool::Wrapper.new(size: configuration[:pool_size], timeout: configuration[:timeout]) do
new_connection
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pling/c2dm/gateway.rb
Expand Up @@ -104,7 +104,7 @@ def default_configuration
end

def connection
@connection ||= Faraday.new(configuration[:connection]) do |builder|
Faraday.new(configuration[:connection]) do |builder|
builder.use Faraday::Request::UrlEncoded
builder.use Faraday::Response::Logger if configuration[:debug]
builder.adapter(configuration[:adapter])
Expand Down
2 changes: 1 addition & 1 deletion lib/pling/gcm/gateway.rb
Expand Up @@ -75,7 +75,7 @@ def default_configuration
end

def connection
@connection ||= Faraday.new(configuration[:connection]) do |builder|
Faraday.new(configuration[:connection]) do |builder|
builder.use FaradayMiddleware::EncodeJson
builder.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
builder.use Faraday::Response::Logger if configuration[:debug]
Expand Down
1 change: 1 addition & 0 deletions pling.gemspec
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "faraday", "~> 0.7"
s.add_runtime_dependency "faraday_middleware", "~> 0.8"
s.add_runtime_dependency "json", "~> 1.4"
s.add_runtime_dependency "connection_pool", "~> 2.0.0"
s.add_runtime_dependency("jruby-openssl") if RUBY_PLATFORM == 'java'

s.add_development_dependency "rspec", "~> 2.7"
Expand Down
25 changes: 24 additions & 1 deletion spec/pling/apn/gateway_spec.rb
Expand Up @@ -18,6 +18,21 @@
Pling::APN::Gateway.handled_types.should =~ [:apple, :apn, :ios, :ipad, :iphone, :ipod]
end

describe '#setup!' do
before do
valid_configuration.merge!(pool_size: 99, timeout: 55)
end

subject { Pling::APN::Gateway.new(valid_configuration) }

let(:connection_pool) { double(:connection_pool_double) }

it 'initializes a new connection pool for the APN connection' do
ConnectionPool::Wrapper.should_receive(:new).with(size: 99, timeout: 55).and_return(connection_pool)
subject.setup!
end
end

describe '#deliver' do
subject { Pling::APN::Gateway.new(valid_configuration) }

Expand All @@ -29,6 +44,14 @@
expect { subject.deliver(message, nil) }.to raise_error
end

it 'should initialize a new APN connection if none is established' do
Pling::APN::Connection.should_receive(:new).and_return(connection)
subject.deliver(message, device)

Pling::APN::Connection.should_not_receive(:new)
subject.deliver(message, device)
end

it 'should call #to_pling_message on the given message' do
message.should_receive(:to_pling_message).and_return(message)
subject.deliver(message, device)
Expand Down Expand Up @@ -138,4 +161,4 @@
end
end
end
end
end

0 comments on commit 9ee6bf6

Please sign in to comment.