mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
291b65b853
* Allow DB::Pool to be a generic connection pool * Use fully qualified class name for consistency Co-authored-by: Brian J. Cardiff <bcardiff@gmail.com> * Wrap only the necessary code in an `ensure` * Add spec for http client pool * Fix ICE in crystal-sqlite3 Co-authored-by: Brian J. Cardiff <bcardiff@gmail.com>
48 lines
1 KiB
Crystal
48 lines
1 KiB
Crystal
require "http"
|
|
require "./fibers"
|
|
|
|
def wait_for(timeout = 5.seconds)
|
|
now = Time.monotonic
|
|
|
|
until yield
|
|
Fiber.yield
|
|
|
|
if (Time.monotonic - now) > timeout
|
|
raise "block failed to evaluate to true within #{timeout}"
|
|
end
|
|
end
|
|
end
|
|
|
|
# Helper method which runs *server*
|
|
# 1. Spawns `server.listen` in a new fiber.
|
|
# 2. Waits until `server.listening?`.
|
|
# 3. Yields to the given block.
|
|
# 4. Ensures the server is closed.
|
|
# 5. After returning from the block, it waits for the server to gracefully
|
|
# shut down before continuing execution in the current fiber.
|
|
# 6. If the listening fiber raises an exception, it is rescued and re-raised
|
|
# in the current fiber.
|
|
def run_server(server)
|
|
server_done = Channel(Exception?).new
|
|
|
|
f = spawn do
|
|
server.listen
|
|
rescue exc
|
|
server_done.send exc
|
|
else
|
|
server_done.send nil
|
|
end
|
|
|
|
begin
|
|
wait_for { server.listening? }
|
|
wait_until_blocked f
|
|
|
|
yield server_done
|
|
ensure
|
|
server.close unless server.closed?
|
|
|
|
if exc = server_done.receive
|
|
raise exc
|
|
end
|
|
end
|
|
end
|