, yea in that case not sure what your options would be. Kinda just spit-balling, but wonder if the pool could be made to accept some sort of arguments when creating a resource… Something like this:
diff --git a/src/db/pool.cr b/src/db/pool.cr
index 378e303..8edc8dc 100644
--- a/src/db/pool.cr
+++ b/src/db/pool.cr
@@ -3,7 +3,7 @@ require "weak_ref"
require "./error"
module DB
- class Pool(T)
+ class Pool(T, *Args)
record Options,
# initial number of connections in the pool
initial_pool_size : Int32 = 1,
@@ -62,16 +62,17 @@ module DB
@[Deprecated("Use `#new` with DB::Pool::Options instead")]
def initialize(initial_pool_size = 1, max_pool_size = 0, max_idle_pool_size = 1, checkout_timeout = 5.0,
- retry_attempts = 1, retry_delay = 0.2, &factory : -> T)
+ retry_attempts = 1, retry_delay = 0.2, *args : *Args, &factory : Proc(*Args, T))
initialize(
Options.new(
initial_pool_size: initial_pool_size, max_pool_size: max_pool_size,
max_idle_pool_size: max_idle_pool_size, checkout_timeout: checkout_timeout,
retry_attempts: retry_attempts, retry_delay: retry_delay),
+ *args,
&factory)
end
- def initialize(pool_options : Options = Options.new, &@factory : -> T)
+ def initialize(pool_options : Options = Options.new, *args : *Args, &@factory : Proc(*Args, T))
@initial_pool_size = pool_options.initial_pool_size
@max_pool_size = pool_options.max_pool_size
@max_idle_pool_size = pool_options.max_idle_pool_size
@@ -83,7 +84,7 @@ module DB
@inflight = 0
@mutex = Mutex.new
- @initial_pool_size.times { build_resource }
+ @initial_pool_size.times { build_resource *args }
end
# close all resources in the pool
@@ -109,7 +110,7 @@ module DB
)
end
- def checkout : T
+ def checkout(*args : *Args) : T
res = sync do
resource = nil
@@ -118,7 +119,7 @@ module DB
if can_increase_pool?
@inflight += 1
begin
- r = unsync { build_resource }
+ r = unsync { build_resource *args }
ensure
@inflight -= 1
end
@@ -148,8 +149,8 @@ module DB
res
end
- def checkout(&block : T ->)
- connection = checkout
+ def checkout(*args : *Args, &block : T ->)
+ connection = checkout *args
begin
yield connection
@@ -236,8 +237,8 @@ module DB
@idle.delete(resource)
end
- private def build_resource : T
- resource = @factory.call
+ private def build_resource(*args : *Args) : T
+ resource = @factory.call *args
sync do
@total << resource
@idle << resource
From here you can do something like:
@pool = DB::Pool(HTTP::Client, String).new(DB::Pool::Options.new(initial_pool_size: 0), @token_generator.call) do |token|
http = HTTP::Client.new(base_uri)
http.before_request do |request|
request.headers["Authorization"] = "Bearer #{token}"
request.headers["Accept"] = "application/json"
request.headers["Connection"] = "keep-alive"
end
http
end
Which says the pool accepts a String
block argument, passing the token to .new
so its available if you wanted some clients created immediately. Otherwise you then do like:
@pool.checkout @token_generator.call do |client|
# Do something ...
end
Wouldn’t really be a way to define defaults or anything. Nor would it help if the clients inside the pool aren’t built fresh all the time but
. So yea, prob easiest to just do option 1 and call it a day
.