mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Refactor connection factory (#181)
* Start moving out URI from ConnectionContext Create connections with an initial context. Database will set itself as context after connection has been created * Migrate to simpler/decoupled factory in driver This allows more freedom on how the connection is created. It will no longer need to have an explicit reference to the connection URI * Introduce DB::Connection::Options Move prepared_statements out from ConnectionContext * Delegate options parsing to driver DRY parsing connection options for database * Introduce DB::Pool::Options * Rename Driver#connection_pool_options to pool_options * Drop driver getter from database * Drop uri getter from database * Add public Database#initialize method * Drop :nodoc: Database#initialize * Pass spec helper explicitly (to access methods within each spec) * Update docs * Update src/db/pool.cr Co-authored-by: Beta Ziliani <beta@manas.tech> * Use ConnectionBuilder instead of procs * Fix inferred type when there is a single concrete connection type * Update src/db/driver.cr Co-authored-by: Beta Ziliani <beta@manas.tech> --------- Co-authored-by: Beta Ziliani <beta@manas.tech>
This commit is contained in:
parent
65b926c926
commit
f13846b133
13 changed files with 178 additions and 86 deletions
|
@ -36,6 +36,15 @@ class FooValue
|
|||
end
|
||||
|
||||
class FooDriver < DB::Driver
|
||||
class FooConnectionBuilder < DB::ConnectionBuilder
|
||||
def initialize(@options : DB::Connection::Options)
|
||||
end
|
||||
|
||||
def build : DB::Connection
|
||||
FooConnection.new(@options)
|
||||
end
|
||||
end
|
||||
|
||||
alias Any = DB::Any | FooValue
|
||||
@@row = [] of Any
|
||||
|
||||
|
@ -47,8 +56,9 @@ class FooDriver < DB::Driver
|
|||
@@row
|
||||
end
|
||||
|
||||
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
||||
FooConnection.new(context)
|
||||
def connection_builder(uri : URI) : DB::ConnectionBuilder
|
||||
params = HTTP::Params.parse(uri.query || "")
|
||||
FooConnectionBuilder.new(connection_options(params))
|
||||
end
|
||||
|
||||
class FooConnection < DB::Connection
|
||||
|
@ -99,6 +109,15 @@ class BarValue
|
|||
end
|
||||
|
||||
class BarDriver < DB::Driver
|
||||
class BarConnectionBuilder < DB::ConnectionBuilder
|
||||
def initialize(@options : DB::Connection::Options)
|
||||
end
|
||||
|
||||
def build : DB::Connection
|
||||
BarConnection.new(@options)
|
||||
end
|
||||
end
|
||||
|
||||
alias Any = DB::Any | BarValue
|
||||
@@row = [] of Any
|
||||
|
||||
|
@ -110,8 +129,9 @@ class BarDriver < DB::Driver
|
|||
@@row
|
||||
end
|
||||
|
||||
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
||||
BarConnection.new(context)
|
||||
def connection_builder(uri : URI) : DB::ConnectionBuilder
|
||||
params = HTTP::Params.parse(uri.query || "")
|
||||
BarConnectionBuilder.new(connection_options(params))
|
||||
end
|
||||
|
||||
class BarConnection < DB::Connection
|
||||
|
@ -156,8 +176,8 @@ DB.register_driver "bar", BarDriver
|
|||
|
||||
describe DB do
|
||||
it "should be able to register multiple drivers" do
|
||||
DB.open("foo://host").driver.should be_a(FooDriver)
|
||||
DB.open("bar://host").driver.should be_a(BarDriver)
|
||||
DB.open("foo://host").checkout.should be_a(FooDriver::FooConnection)
|
||||
DB.open("bar://host").checkout.should be_a(BarDriver::BarConnection)
|
||||
end
|
||||
|
||||
it "Foo and Bar drivers should return fake_row" do
|
||||
|
|
|
@ -9,12 +9,9 @@ describe DB do
|
|||
DB.driver_class("dummy").should eq(DummyDriver)
|
||||
end
|
||||
|
||||
it "should instantiate driver with connection uri" do
|
||||
it "should create dummy connection" do
|
||||
db = DB.open "dummy://localhost:1027"
|
||||
db.driver.should be_a(DummyDriver)
|
||||
db.uri.scheme.should eq("dummy")
|
||||
db.uri.host.should eq("localhost")
|
||||
db.uri.port.should eq(1027)
|
||||
db.checkout.should be_a(DummyDriver::DummyConnection)
|
||||
end
|
||||
|
||||
it "should create a connection and close it" do
|
||||
|
|
|
@ -2,13 +2,23 @@ require "spec"
|
|||
require "../src/db"
|
||||
|
||||
class DummyDriver < DB::Driver
|
||||
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
||||
DummyConnection.new(context)
|
||||
class DummyConnectionBuilder < DB::ConnectionBuilder
|
||||
def initialize(@options : DB::Connection::Options)
|
||||
end
|
||||
|
||||
def build : DB::Connection
|
||||
DummyConnection.new(@options)
|
||||
end
|
||||
end
|
||||
|
||||
def connection_builder(uri : URI) : DB::ConnectionBuilder
|
||||
params = HTTP::Params.parse(uri.query || "")
|
||||
DummyConnectionBuilder.new(connection_options(params))
|
||||
end
|
||||
|
||||
class DummyConnection < DB::Connection
|
||||
def initialize(context)
|
||||
super(context)
|
||||
def initialize(options : DB::Connection::Options)
|
||||
super(options)
|
||||
@connected = true
|
||||
@@connections ||= [] of DummyConnection
|
||||
@@connections.not_nil! << self
|
||||
|
|
|
@ -22,10 +22,10 @@ describe DB::Pool do
|
|||
expected_per_connection = 5
|
||||
requests = fixed_pool_size * expected_per_connection
|
||||
|
||||
pool = DB::Pool.new(
|
||||
pool = DB::Pool.new(DB::Pool::Options.new(
|
||||
initial_pool_size: fixed_pool_size,
|
||||
max_pool_size: fixed_pool_size,
|
||||
max_idle_pool_size: fixed_pool_size) {
|
||||
max_idle_pool_size: fixed_pool_size)) {
|
||||
HTTP::Client.new(URI.parse("http://127.0.0.1:#{address.port}/"))
|
||||
}
|
||||
|
||||
|
|
|
@ -57,15 +57,19 @@ class Closable
|
|||
end
|
||||
end
|
||||
|
||||
private def create_pool(**options, &factory : -> T) forall T
|
||||
DB::Pool.new(DB::Pool::Options.new(**options), &factory)
|
||||
end
|
||||
|
||||
describe DB::Pool do
|
||||
it "should use proc to create objects" do
|
||||
block_called = 0
|
||||
pool = DB::Pool.new(initial_pool_size: 3) { block_called += 1; Closable.new }
|
||||
pool = create_pool(initial_pool_size: 3) { block_called += 1; Closable.new }
|
||||
block_called.should eq(3)
|
||||
end
|
||||
|
||||
it "should get resource" do
|
||||
pool = DB::Pool.new { Closable.new }
|
||||
pool = create_pool { Closable.new }
|
||||
resource = pool.checkout
|
||||
resource.should be_a Closable
|
||||
resource.before_checkout_called.should be_true
|
||||
|
@ -73,18 +77,18 @@ describe DB::Pool do
|
|||
|
||||
it "should be available if not checkedout" do
|
||||
resource = uninitialized Closable
|
||||
pool = DB::Pool.new(initial_pool_size: 1) { resource = Closable.new }
|
||||
pool = create_pool(initial_pool_size: 1) { resource = Closable.new }
|
||||
pool.is_available?(resource).should be_true
|
||||
end
|
||||
|
||||
it "should not be available if checkedout" do
|
||||
pool = DB::Pool.new { Closable.new }
|
||||
pool = create_pool { Closable.new }
|
||||
resource = pool.checkout
|
||||
pool.is_available?(resource).should be_false
|
||||
end
|
||||
|
||||
it "should be available if returned" do
|
||||
pool = DB::Pool.new { Closable.new }
|
||||
pool = create_pool { Closable.new }
|
||||
resource = pool.checkout
|
||||
resource.after_release_called.should be_false
|
||||
pool.release resource
|
||||
|
@ -93,7 +97,7 @@ describe DB::Pool do
|
|||
end
|
||||
|
||||
it "should wait for available resource" do
|
||||
pool = DB::Pool.new(max_pool_size: 1, initial_pool_size: 1) { Closable.new }
|
||||
pool = create_pool(max_pool_size: 1, initial_pool_size: 1) { Closable.new }
|
||||
|
||||
b_cnn_request = ShouldSleepingOp.new
|
||||
wait_a = WaitFor.new
|
||||
|
@ -121,7 +125,7 @@ describe DB::Pool do
|
|||
|
||||
it "should create new if max was not reached" do
|
||||
block_called = 0
|
||||
pool = DB::Pool.new(max_pool_size: 2, initial_pool_size: 1) { block_called += 1; Closable.new }
|
||||
pool = create_pool(max_pool_size: 2, initial_pool_size: 1) { block_called += 1; Closable.new }
|
||||
block_called.should eq 1
|
||||
pool.checkout
|
||||
block_called.should eq 1
|
||||
|
@ -131,7 +135,7 @@ describe DB::Pool do
|
|||
|
||||
it "should reuse returned resources" do
|
||||
all = [] of Closable
|
||||
pool = DB::Pool.new(max_pool_size: 2, initial_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool = create_pool(max_pool_size: 2, initial_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool.checkout
|
||||
b1 = pool.checkout
|
||||
pool.release b1
|
||||
|
@ -143,7 +147,7 @@ describe DB::Pool do
|
|||
|
||||
it "should close available and total" do
|
||||
all = [] of Closable
|
||||
pool = DB::Pool.new(max_pool_size: 2, initial_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool = create_pool(max_pool_size: 2, initial_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
a = pool.checkout
|
||||
b = pool.checkout
|
||||
pool.release b
|
||||
|
@ -157,7 +161,7 @@ describe DB::Pool do
|
|||
end
|
||||
|
||||
it "should timeout" do
|
||||
pool = DB::Pool.new(max_pool_size: 1, checkout_timeout: 0.1) { Closable.new }
|
||||
pool = create_pool(max_pool_size: 1, checkout_timeout: 0.1) { Closable.new }
|
||||
pool.checkout
|
||||
expect_raises DB::PoolTimeout do
|
||||
pool.checkout
|
||||
|
@ -165,7 +169,7 @@ describe DB::Pool do
|
|||
end
|
||||
|
||||
it "should be able to release after a timeout" do
|
||||
pool = DB::Pool.new(max_pool_size: 1, checkout_timeout: 0.1) { Closable.new }
|
||||
pool = create_pool(max_pool_size: 1, checkout_timeout: 0.1) { Closable.new }
|
||||
a = pool.checkout
|
||||
pool.checkout rescue nil
|
||||
pool.release a
|
||||
|
@ -173,7 +177,7 @@ describe DB::Pool do
|
|||
|
||||
it "should close if max idle amount is reached" do
|
||||
all = [] of Closable
|
||||
pool = DB::Pool.new(max_pool_size: 3, max_idle_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool = create_pool(max_pool_size: 3, max_idle_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool.checkout
|
||||
pool.checkout
|
||||
pool.checkout
|
||||
|
@ -191,7 +195,7 @@ describe DB::Pool do
|
|||
end
|
||||
|
||||
it "should not return closed resources to the pool" do
|
||||
pool = DB::Pool.new(max_pool_size: 1, max_idle_pool_size: 1) { Closable.new }
|
||||
pool = create_pool(max_pool_size: 1, max_idle_pool_size: 1) { Closable.new }
|
||||
|
||||
# pool size 1 should be reusing the one resource
|
||||
resource1 = pool.checkout
|
||||
|
@ -209,7 +213,7 @@ describe DB::Pool do
|
|||
|
||||
it "should create resource after max_pool was reached if idle forced some close up" do
|
||||
all = [] of Closable
|
||||
pool = DB::Pool.new(max_pool_size: 3, max_idle_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool = create_pool(max_pool_size: 3, max_idle_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool.checkout
|
||||
pool.checkout
|
||||
pool.checkout
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue