-
-
Save rociiu/305308 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query_factory = MemoizingQueryFactory.new(TimingOutQueryFactory.new(Query, 1.second)) | |
QueryEvaluator.new(ConnectionPool.new(20), query_factory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MemoizingQueryFactory | |
def initialize(query_factory) | |
@memo = Hash.new do |h, k| | |
h = query_factory.new(*k) | |
end | |
end | |
def new(connection, query_string, *args) | |
@memo[[connection, query_string, args]] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class QueryEvaluator | |
def initialize(connection_pool, query_factory = Query) | |
@query_factory = query_factory | |
@connection_pool = connection_pool | |
end | |
def select(query_string, *args) | |
@connection_pool.with_connection do |connection| | |
@query_factory.new(connection, query_string, *args).select | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TimingOutQueryProxyFactory | |
def initialize(query_factory, timeout) | |
@query_factory = query_factory | |
@timeout = timeout | |
end | |
def new(connection, query_string, *args) | |
@query_factory.new(connection, query_string, *args) | |
end | |
end | |
class TimingOutQueryProxy | |
def initialize(query, timeout) | |
@query = query | |
@timeout = timeout | |
end | |
def select | |
Timeout.timeout(@timeout) do | |
@query.select | |
end | |
end | |
# ... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment