-
-
Notifications
You must be signed in to change notification settings - Fork 419
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
ConnectionPool adapter #835
Open
bkeepers
wants to merge
4
commits into
main
Choose a base branch
from
connection-pool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ gem 'mysql2' | |
gem 'pg' | ||
gem 'cuprite' | ||
gem 'puma' | ||
gem 'connection_pool' | ||
|
||
group(:guard) do | ||
gem 'guard' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# An adapter that uses ConnectionPool to manage connections. | ||
# | ||
# Usage: | ||
# | ||
# # Reuse an existing pool to create new adapters | ||
# pool = ConnectionPool.new(size: 5, timeout: 5) { Redis.new } | ||
# Flipper::Adapters::ConnectionPool.new(pool) do |client| | ||
# Flipper::Adapters::Redis.new(client) | ||
# end | ||
# | ||
# # Create a new pool that returns the adapter | ||
# Flipper::Adapters::ConnectionPool.new(size: 5, timeout: 5) do | ||
# Flipper::Adapters::Redis.new(Redis.new) | ||
# end | ||
class Flipper::Adapters::ConnectionPool | ||
include Flipper::Adapter | ||
|
||
# Use an existing ConnectionPool to initialize and cache new adapter instances. | ||
class Wrapper | ||
def initialize(pool, &initializer) | ||
@pool = pool | ||
@initializer = initializer | ||
@instances = {} | ||
end | ||
|
||
def with(&block) | ||
@pool.with do |resource| | ||
yield @instances[resource] ||= @initializer.call(resource) | ||
end | ||
end | ||
end | ||
|
||
def initialize(options = {}, &adapter_initializer) | ||
@pool = options.is_a?(ConnectionPool) ? Wrapper.new(options, &adapter_initializer) : ConnectionPool.new(options, &adapter_initializer) | ||
end | ||
|
||
OPERATIONS.each do |method| | ||
define_method(method) do |*args| | ||
@pool.with { |adapter| adapter.public_send(method, *args) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require "connection_pool" | ||
require "flipper/adapters/connection_pool" | ||
require "flipper-redis" | ||
|
||
RSpec.describe Flipper::Adapters::ConnectionPool do | ||
let(:redis_options) { {url: ENV['REDIS_URL']}.compact } | ||
|
||
before do | ||
skip_on_error(Redis::CannotConnectError, 'Redis not available') do | ||
Redis.new(redis_options).flushdb | ||
end | ||
end | ||
|
||
context "with an existing pool" do | ||
let(:pool) do | ||
ConnectionPool.new(size: 1, timeout: 5) { Redis.new(redis_options) } | ||
end | ||
|
||
subject do | ||
described_class.new(pool) { |client| Flipper::Adapters::Redis.new(client) } | ||
end | ||
|
||
it_should_behave_like 'a flipper adapter' | ||
end | ||
|
||
context "with a new pool" do | ||
subject do | ||
described_class.new(size: 2) { Flipper::Adapters::Redis.new(Redis.new(redis_options)) } | ||
end | ||
|
||
it_should_behave_like 'a flipper adapter' | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could this hang onto resources for too long in some cases, preventing them from being garbage collected?
ConnectionPool
has areload
capability that will release its hold on the existing resources, but this@instances
hash map will retain them and then grow larger as the pool offers up new resources.I was initially thinking that
ObjectSpace::WeakKeyMap
would solve the problem, but this is a funny situation in which the map keys and values both have references to the same resource.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.
Yes, good catch. I just pushed 18b2a1b which makes
ConnectionPool
observable. I might open an issue on the uptream repo and see if a change like this would be accepted.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.
That seems like a good solution. The only other thing that comes to mind is an LRU cache, but I don't know if there's a solid Ruby implementation.