mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
allow releasing connections of the pool from pool_statements
remove old connections from PoolStatement::@connections either disposed or closed connections
This commit is contained in:
parent
6745ea956f
commit
ea2a107069
3 changed files with 22 additions and 9 deletions
|
@ -1,4 +1,5 @@
|
||||||
require "http/params"
|
require "http/params"
|
||||||
|
require "weak_ref"
|
||||||
|
|
||||||
module DB
|
module DB
|
||||||
# Acts as an entry point for database access.
|
# Acts as an entry point for database access.
|
||||||
|
@ -63,7 +64,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
def checkout_some(candidates : Enumerable(Connection)) : {Connection, Bool}
|
def checkout_some(candidates : Enumerable(WeakRef(Connection))) : {Connection, Bool}
|
||||||
@pool.checkout_some candidates
|
@pool.checkout_some candidates
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "weak_ref"
|
||||||
|
|
||||||
module DB
|
module DB
|
||||||
class Pool(T)
|
class Pool(T)
|
||||||
@initial_pool_size : Int32
|
@initial_pool_size : Int32
|
||||||
|
@ -47,18 +49,19 @@ module DB
|
||||||
# ```
|
# ```
|
||||||
# `selected` be a resource from the `candidates` list and `is_candidate` == `true`
|
# `selected` be a resource from the `candidates` list and `is_candidate` == `true`
|
||||||
# or `selected` will be a new resource adn `is_candidate` == `false`
|
# or `selected` will be a new resource adn `is_candidate` == `false`
|
||||||
def checkout_some(candidates : Enumerable(T)) : {T, Bool}
|
def checkout_some(candidates : Enumerable(WeakRef(T))) : {T, Bool}
|
||||||
# TODO honor candidates while waiting for availables
|
# TODO honor candidates while waiting for availables
|
||||||
# this will allow us to remove `candidates.includes?(resource)`
|
# this will allow us to remove `candidates.includes?(resource)`
|
||||||
candidates.each do |resource|
|
candidates.each do |ref|
|
||||||
if is_available?(resource)
|
resource = ref.target
|
||||||
|
if resource && is_available?(resource)
|
||||||
@available.delete resource
|
@available.delete resource
|
||||||
return {resource, true}
|
return {resource, true}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
resource = checkout
|
resource = checkout
|
||||||
{resource, candidates.includes?(resource)}
|
{resource, candidates.any? { |ref| ref.target == resource }}
|
||||||
end
|
end
|
||||||
|
|
||||||
def release(resource : T) : Nil
|
def release(resource : T) : Nil
|
||||||
|
|
|
@ -7,7 +7,7 @@ module DB
|
||||||
include StatementMethods
|
include StatementMethods
|
||||||
|
|
||||||
# connections where the statement was prepared
|
# connections where the statement was prepared
|
||||||
@connections = Set(Connection).new
|
@connections = Set(WeakRef(Connection)).new
|
||||||
|
|
||||||
def initialize(@db : Database, @query : String)
|
def initialize(@db : Database, @query : String)
|
||||||
# Prepares a statement on some connection
|
# Prepares a statement on some connection
|
||||||
|
@ -61,13 +61,22 @@ module DB
|
||||||
# builds a statement over a real connection
|
# builds a statement over a real connection
|
||||||
# the conneciton is registered in `@connections`
|
# the conneciton is registered in `@connections`
|
||||||
private def build_statement
|
private def build_statement
|
||||||
# TODO closed connections should be removed from @connections
|
clean_connections
|
||||||
# either by callbacks or by week references.
|
|
||||||
conn, existing = @db.checkout_some(@connections)
|
conn, existing = @db.checkout_some(@connections)
|
||||||
@connections << conn unless existing
|
@connections << WeakRef.new(conn) unless existing
|
||||||
conn.prepare(@query)
|
conn.prepare(@query)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def clean_connections
|
||||||
|
# remove disposed or closed connections
|
||||||
|
@connections.each do |ref|
|
||||||
|
conn = ref.target
|
||||||
|
if !conn || conn.closed?
|
||||||
|
@connections.delete ref
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private def statement_with_retry
|
private def statement_with_retry
|
||||||
@db.retry do
|
@db.retry do
|
||||||
return yield build_statement
|
return yield build_statement
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue