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.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/stack-service-base/fiber_pool.rb', line 22 def initialize(db, opts = OPTS) otl_span "FiberConnectionPool.initialize" do |span| 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 end |
Instance Method Details
#disconnect(symbol) ⇒ Object
def preconnect(_concurrent = false) = :unimplemented
82 83 84 85 86 87 |
# File 'lib/stack-service-base/fiber_pool.rb', line 82 def disconnect(symbol) until @stock.empty? log 'disconnect connection (fiber pool)' @stock.shift.close end end |
#hold(_server = nil) ⇒ Object
45 46 47 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 |
# File 'lib/stack-service-base/fiber_pool.rb', line 45 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
36 37 38 39 40 41 42 43 |
# File 'lib/stack-service-base/fiber_pool.rb', line 36 def is_valid_connection?(conn) sql = valid_connection_sql log_connection_execute(conn, sql) true rescue =>e_ conn.close rescue nil false end |
#log(msg) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/stack-service-base/fiber_pool.rb', line 9 def log(msg) otl_current_span{ |span| span.add_event("FiberPool", attributes: { F: Fiber.current.__id__, T: Thread.current.__id__, A: self.__id__, message: msg }.transform_keys(&:to_s) ) } 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
80 81 |
# File 'lib/stack-service-base/fiber_pool.rb', line 80 def max_size = @sp.limit # def preconnect(_concurrent = false) = :unimplemented |
#pool_type ⇒ Object
def servers = []
89 |
# File 'lib/stack-service-base/fiber_pool.rb', line 89 def pool_type = :fiber # :threaded |
#size ⇒ Object
79 |
# File 'lib/stack-service-base/fiber_pool.rb', line 79 def size = @acquired.size |
#sync ⇒ Object
:threaded
90 |
# File 'lib/stack-service-base/fiber_pool.rb', line 90 def sync = yield |