Class: FiberConnectionPool
- Inherits:
-
Sequel::ConnectionPool
- Object
- Sequel::ConnectionPool
- FiberConnectionPool
- Defined in:
- lib/stack-service-base/fiber_pool.rb
Constant Summary collapse
- VALIDATION_TIMEOUT =
20- POOL_SIZE =
ENV.fetch('DB_CONNECTION_POOL_SIZE', '10').to_i
Instance Method Summary collapse
-
#disconnect(symbol) ⇒ Object
def preconnect(_concurrent = false) = :unimplemented.
- #hold(_server = nil) ⇒ Object
-
#initialize(db, opts = OPTS) ⇒ FiberConnectionPool
constructor
A new instance of FiberConnectionPool.
- #is_valid_connection?(conn) ⇒ Boolean
- #max_size ⇒ Object
-
#pool_type ⇒ Object
def servers = [].
- #size ⇒ Object
-
#sync ⇒ Object
:threaded.
Constructor Details
#initialize(db, opts = OPTS) ⇒ FiberConnectionPool
Returns a new instance of FiberConnectionPool.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/stack-service-base/fiber_pool.rb', line 25 def initialize(db, opts = OPTS) otl_span "FiberConnectionPool.initialize" do |span| super @allocator = ->() { make_new(:default).tap { |conn| $local_log["new connection (fiber pool) #{conn.__id__}"] } } @stock = [] @acquired = {} @sp = Async::Semaphore.new opts[:max_connections] || POOL_SIZE end end |
Instance Method Details
#disconnect(symbol) ⇒ Object
def preconnect(_concurrent = false) = :unimplemented
85 86 87 88 89 90 |
# File 'lib/stack-service-base/fiber_pool.rb', line 85 def disconnect(symbol) until @stock.empty? $local_log['disconnect connection (fiber pool)'] @stock.shift.close end end |
#hold(_server = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/stack-service-base/fiber_pool.rb', line 48 def hold(_server = nil) return yield @acquired[Fiber.current] if @acquired[Fiber.current] # protect from recursion $local_log["hold in (fiber pool: #{__id__}) #{@stock.map{_1.__id__}}"] fiber = Fiber.current try_count = 2 @sp.acquire do until @acquired[fiber] && ( @acquired[fiber].instance_eval { @last_use_.nil? || (Time.now - @last_use_).to_i < VALIDATION_TIMEOUT } || is_valid_connection?(@acquired[fiber]) ) @acquired[fiber] = @stock.shift || @allocator.call end @acquired[fiber].instance_eval { @last_use_ = Time.now } yield @acquired[fiber] rescue Sequel::DatabaseDisconnectError => e $local_log["remove connection (fiber pool) retry(#{try_count})"] @acquired.delete(fiber) (try_count -=1) < 0 ? raise : retry rescue =>e $stdout.puts e. $stdout.puts e.backtrace[0..10].join "\n" $local_log['remove connection (fiber pool) give up'] @acquired.delete(fiber) raise ensure @stock.push @acquired.delete(fiber) if @acquired[fiber] $local_log["hold out (fiber pool: #{__id__}) #{@stock.map{_1.__id__}}"] end end |
#is_valid_connection?(conn) ⇒ Boolean
39 40 41 42 43 44 45 46 |
# File 'lib/stack-service-base/fiber_pool.rb', line 39 def is_valid_connection?(conn) sql = valid_connection_sql log_connection_execute(conn, sql) true rescue =>e_ conn.close rescue nil false end |
#max_size ⇒ Object
83 84 |
# File 'lib/stack-service-base/fiber_pool.rb', line 83 def max_size = @sp.limit # def preconnect(_concurrent = false) = :unimplemented |
#pool_type ⇒ Object
def servers = []
92 |
# File 'lib/stack-service-base/fiber_pool.rb', line 92 def pool_type = :fiber # :threaded |
#size ⇒ Object
82 |
# File 'lib/stack-service-base/fiber_pool.rb', line 82 def size = @acquired.size |
#sync ⇒ Object
:threaded
93 |
# File 'lib/stack-service-base/fiber_pool.rb', line 93 def sync = yield |