mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Fix mt
This commit is contained in:
parent
87dc8aafaf
commit
76eba76dec
3 changed files with 37 additions and 19 deletions
|
@ -216,8 +216,10 @@ module DB
|
||||||
|
|
||||||
private def build_resource : T
|
private def build_resource : T
|
||||||
resource = @factory.call
|
resource = @factory.call
|
||||||
@total << resource
|
sync do
|
||||||
@idle << resource
|
@total << resource
|
||||||
|
@idle << resource
|
||||||
|
end
|
||||||
resource
|
resource
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ module DB
|
||||||
class PoolPreparedStatement < PoolStatement
|
class PoolPreparedStatement < PoolStatement
|
||||||
# connections where the statement was prepared
|
# connections where the statement was prepared
|
||||||
@connections = Set(WeakRef(Connection)).new
|
@connections = Set(WeakRef(Connection)).new
|
||||||
|
@mutex = Mutex.new
|
||||||
|
|
||||||
def initialize(db : Database, query : String)
|
def initialize(db : Database, query : String)
|
||||||
super
|
super
|
||||||
|
@ -20,12 +21,14 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
protected def do_close
|
protected def do_close
|
||||||
# TODO close all statements on all connections.
|
@mutex.synchronize do
|
||||||
# currently statements are closed when the connection is closed.
|
# 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
|
# WHAT-IF the connection is busy? Should each statement be able to
|
||||||
# deallocate itself when the connection is free.
|
# deallocate itself when the connection is free.
|
||||||
@connections.clear
|
@connections.clear
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# builds a statement over a real connection
|
# builds a statement over a real connection
|
||||||
|
@ -39,16 +42,22 @@ module DB
|
||||||
conn.release
|
conn.release
|
||||||
raise ex
|
raise ex
|
||||||
end
|
end
|
||||||
@connections << WeakRef.new(conn) unless existing
|
unless existing
|
||||||
|
@mutex.synchronize do
|
||||||
|
@connections << WeakRef.new(conn)
|
||||||
|
end
|
||||||
|
end
|
||||||
stmt
|
stmt
|
||||||
end
|
end
|
||||||
|
|
||||||
private def clean_connections
|
private def clean_connections
|
||||||
# remove disposed or closed connections
|
@mutex.synchronize do
|
||||||
@connections.each do |ref|
|
# remove disposed or closed connections
|
||||||
conn = ref.value
|
@connections.each do |ref|
|
||||||
if !conn || conn.closed?
|
conn = ref.value
|
||||||
@connections.delete ref
|
if !conn || conn.closed?
|
||||||
|
@connections.delete ref
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,21 +1,28 @@
|
||||||
module DB
|
module DB
|
||||||
class StringKeyCache(T)
|
class StringKeyCache(T)
|
||||||
@cache = {} of String => T
|
@cache = {} of String => T
|
||||||
|
@mutex = Mutex.new
|
||||||
|
|
||||||
def fetch(key : String) : T
|
def fetch(key : String) : T
|
||||||
value = @cache.fetch(key, nil)
|
@mutex.synchronize do
|
||||||
value = @cache[key] = yield unless value
|
value = @cache.fetch(key, nil)
|
||||||
value
|
value = @cache[key] = yield unless value
|
||||||
|
value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def each_value
|
def each_value
|
||||||
@cache.each do |_, value|
|
@mutex.synchronize do
|
||||||
yield value
|
@cache.each do |_, value|
|
||||||
|
yield value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear
|
def clear
|
||||||
@cache.clear
|
@mutex.synchronize do
|
||||||
|
@cache.clear
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue