mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Drop checkout_some, simpler pool_prepared statement
This commit is contained in:
parent
684b8ac032
commit
cd107ca635
2 changed files with 2 additions and 75 deletions
|
@ -158,27 +158,6 @@ module DB
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# ```
|
|
||||||
# selected, is_candidate = pool.checkout_some(candidates)
|
|
||||||
# ```
|
|
||||||
# `selected` be a resource from the `candidates` list and `is_candidate` == `true`
|
|
||||||
# or `selected` will be a new resource and `is_candidate` == `false`
|
|
||||||
def checkout_some(candidates : Enumerable(WeakRef(T))) : {T, Bool}
|
|
||||||
sync do
|
|
||||||
candidates.each do |ref|
|
|
||||||
resource = ref.value
|
|
||||||
if resource && is_available?(resource)
|
|
||||||
@idle.delete resource
|
|
||||||
resource.before_checkout
|
|
||||||
return {resource, true}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
resource = checkout
|
|
||||||
{resource, candidates.any? { |ref| ref.value == resource }}
|
|
||||||
end
|
|
||||||
|
|
||||||
def release(resource : T) : Nil
|
def release(resource : T) : Nil
|
||||||
idle_pushed = false
|
idle_pushed = false
|
||||||
|
|
||||||
|
|
|
@ -5,74 +5,22 @@ module DB
|
||||||
#
|
#
|
||||||
# See `PoolStatement`
|
# See `PoolStatement`
|
||||||
class PoolPreparedStatement < PoolStatement
|
class PoolPreparedStatement < PoolStatement
|
||||||
# connections where the statement was prepared
|
|
||||||
@connections = Set(WeakRef(Connection)).new
|
|
||||||
@mutex = Mutex.new
|
|
||||||
|
|
||||||
def initialize(db : Database, query : String)
|
def initialize(db : Database, query : String)
|
||||||
super
|
super
|
||||||
# Prepares a statement on some connection
|
|
||||||
# otherwise the preparation is delayed until the first execution.
|
|
||||||
# After the first initialization the connection must be released
|
|
||||||
# it will be checked out when executing it.
|
|
||||||
|
|
||||||
# This only happens if the db is configured to use prepared statements cache.
|
|
||||||
# Without that there is no reference to the already prepared statement we can
|
|
||||||
# take advantage of.
|
|
||||||
if db.prepared_statements_cache?
|
|
||||||
statement_with_retry &.release_connection
|
|
||||||
end
|
|
||||||
|
|
||||||
# TODO use a round-robin selection in the pool so multiple sequentially
|
|
||||||
# initialized statements are assigned to different connections.
|
|
||||||
end
|
end
|
||||||
|
|
||||||
protected def do_close
|
protected def do_close
|
||||||
@mutex.synchronize do
|
|
||||||
# TODO close all statements on all connections.
|
|
||||||
# currently statements are closed when the connection is closed.
|
|
||||||
|
|
||||||
# WHAT-IF the connection is busy? Should each statement be able to
|
|
||||||
# deallocate itself when the connection is free.
|
|
||||||
@connections.clear
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# builds a statement over a real connection
|
# builds a statement over a real connection
|
||||||
# the connection is registered in `@connections`
|
|
||||||
private def build_statement : Statement
|
private def build_statement : Statement
|
||||||
clean_connections
|
conn = @db.pool.checkout
|
||||||
|
|
||||||
conn, existing = @mutex.synchronize do
|
|
||||||
@db.checkout_some(@connections)
|
|
||||||
end
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
stmt = conn.prepared.build(@query)
|
conn.prepared.build(@query)
|
||||||
rescue ex
|
rescue ex
|
||||||
conn.release
|
conn.release
|
||||||
raise ex
|
raise ex
|
||||||
end
|
end
|
||||||
if !existing && @db.prepared_statements_cache?
|
|
||||||
@mutex.synchronize do
|
|
||||||
@connections << WeakRef.new(conn)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
stmt
|
|
||||||
end
|
|
||||||
|
|
||||||
private def clean_connections
|
|
||||||
return unless @db.prepared_statements_cache?
|
|
||||||
|
|
||||||
@mutex.synchronize do
|
|
||||||
# remove disposed or closed connections
|
|
||||||
@connections.each do |ref|
|
|
||||||
conn = ref.value
|
|
||||||
if !conn || conn.closed?
|
|
||||||
@connections.delete ref
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue