Class: FiberConnectionPool
- Inherits:
-
Sequel::ConnectionPool
- Object
- Sequel::ConnectionPool
- FiberConnectionPool
- Defined in:
- lib/stack-service-base/fiber_pool.rb
Overview
$stdout.sync = true
Constant Summary collapse
- VALIDATION_TIMEOUT =
20
- POOL_SIZE =
10
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
- #log(msg) ⇒ Object
- #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.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/stack-service-base/fiber_pool.rb', line 15 def initialize(db, opts = OPTS) super @allocator = ->() { make_new(:default).tap { |conn| log "new connection (fiber pool) #{conn}" } } @stock = [] @acquired = {} @sp = Async::Semaphore.new opts[:max_connections] || POOL_SIZE end |
Instance Method Details
#disconnect(symbol) ⇒ Object
def preconnect(_concurrent = false) = :unimplemented
73 74 75 76 77 78 |
# File 'lib/stack-service-base/fiber_pool.rb', line 73 def disconnect(symbol) until @stock.empty? log 'disconnect connection (fiber pool)' @stock.shift.close end end |
#hold(_server = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/stack-service-base/fiber_pool.rb', line 36 def hold(_server = nil) return yield @acquired[Fiber.current] if @acquired[Fiber.current] # protect from recursion 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 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" log 'remove connection (fiber pool) give up' @acquired.delete(fiber) raise ensure @stock.push @acquired.delete(fiber) if @acquired[fiber] log "hold out (fiber pool: #{__id__}) #{@stock.map{_1.__id__}}" end end |
#is_valid_connection?(conn) ⇒ Boolean
27 28 29 30 31 32 33 34 |
# File 'lib/stack-service-base/fiber_pool.rb', line 27 def is_valid_connection?(conn) sql = valid_connection_sql log_connection_execute(conn, sql) true rescue =>e_ conn.close rescue false end |
#log(msg) ⇒ Object
9 10 11 12 13 |
# File 'lib/stack-service-base/fiber_pool.rb', line 9 def log(msg) return if defined? PERFORMANCE $stdout.puts "F:#{Fiber.current.__id__} : T:#{Thread.current.__id__} : A:#{self.__id__} : #{msg}" # LOGGER.debug :fiber_pool, msg end |
#max_size ⇒ Object
71 72 |
# File 'lib/stack-service-base/fiber_pool.rb', line 71 def max_size = @sp.limit # def preconnect(_concurrent = false) = :unimplemented |
#pool_type ⇒ Object
def servers = []
80 |
# File 'lib/stack-service-base/fiber_pool.rb', line 80 def pool_type = :fiber # :threaded |
#size ⇒ Object
70 |
# File 'lib/stack-service-base/fiber_pool.rb', line 70 def size = @acquired.size |
#sync ⇒ Object
:threaded
81 |
# File 'lib/stack-service-base/fiber_pool.rb', line 81 def sync = yield |