-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce SQS connection pooling #52
base: master
Are you sure you want to change the base?
Changes from all commits
c40ff4f
8d78130
4fe1f0b
b7795dd
088522a
42b087e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require 'connection_pool' | ||
|
||
# The enclosing module for the library | ||
module Sqewer | ||
# Eager-load everything except extensions. Sort to ensure | ||
|
@@ -24,6 +26,18 @@ def self.submit!(*jobs, **options) | |
Sqewer::Submitter.default.submit!(*jobs, **options) | ||
end | ||
|
||
def self.default_connection_pool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we create an accessor pair ( |
||
@default_connection_pool ||= ConnectionPool.new do | ||
Sqewer::Connection.default | ||
end | ||
end | ||
|
||
def self.reset_default_connection_pool! | ||
@default_connection_pool = ConnectionPool.new do | ||
Sqewer::Connection.default | ||
end | ||
end | ||
|
||
# If we are within Rails, load the railtie | ||
require_relative 'sqewer/extensions/railtie' if defined?(Rails) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,13 @@ | |
# (something that responds to `#send_message`) | ||
# and the serializer (something that responds to `#serialize`) to | ||
# convert the job into the string that will be put in the queue. | ||
class Sqewer::Submitter < Struct.new(:connection, :serializer) | ||
class Sqewer::Submitter < Struct.new(:connection_pool, :serializer) | ||
NotSqewerJob = Class.new(StandardError) | ||
|
||
# Returns a default Submitter, configured with the default connection | ||
# and the default serializer. | ||
def self.default | ||
new(Sqewer::Connection.default, Sqewer::Serializer.default) | ||
new(Sqewer.default_connection_pool, Sqewer::Serializer.default) | ||
end | ||
|
||
def submit!(job, **kwargs_for_send) | ||
|
@@ -21,7 +21,13 @@ def submit!(job, **kwargs_for_send) | |
else | ||
serializer.serialize(job) | ||
end | ||
connection.send_message(message_body, **kwargs_for_send) | ||
if connection_pool.is_a?(Sqewer::ConnectionMessagebox) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use a pool always, but with a stand-in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit trickier, because the message box also uses a connection (it acts as a composed object). Given the API around this, this is the best solution I found. If you have something better in mind, I'd be happy to make it work :) |
||
connection_pool.send_message(message_body, **kwargs_for_send) | ||
else | ||
connection_pool.with do |connection| | ||
connection.send_message(message_body, **kwargs_for_send) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
client.delete_queue(queue_url: ENV.fetch('SQS_QUEUE_URL')) rescue Aws::SQS::Errors::NonExistentQueue | ||
|
||
ENV.delete('SQS_QUEUE_URL') | ||
Sqewer.reset_default_connection_pool! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we have an accessor pair we could do an explicit assignment here instead? |
||
else | ||
example.run | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not need to require
connection_pool
or depend on it - we can provide aNullPool
instead (Prorate gem has a similar setup). Less dependencies that way :-) and if we document connection_pool compatibility people may install it themselves