Class: FiberConnectionPool

Inherits:
Sequel::ConnectionPool
  • Object
show all
Defined in:
lib/stack-service-base/fiber_pool.rb

Constant Summary collapse

VALIDATION_TIMEOUT =
20
POOL_SIZE =
10

Instance Method Summary collapse

Constructor Details

#initialize(db, opts = OPTS) ⇒ FiberConnectionPool

Returns a new instance of FiberConnectionPool.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/stack-service-base/fiber_pool.rb', line 24

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



84
85
86
87
88
89
# File 'lib/stack-service-base/fiber_pool.rb', line 84

def disconnect(symbol)
  until @stock.empty?
    $local_log['disconnect connection (fiber pool)']
    @stock.shift.close
  end
end

#hold(_server = nil) ⇒ Object



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
78
79
# File 'lib/stack-service-base/fiber_pool.rb', line 47

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.message
    $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

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/stack-service-base/fiber_pool.rb', line 38

def is_valid_connection?(conn)
  sql = valid_connection_sql
  log_connection_execute(conn, sql)
  true
rescue =>e_
  conn.close rescue nil
  false
end

#max_sizeObject



82
83
# File 'lib/stack-service-base/fiber_pool.rb', line 82

def max_size = @sp.limit
# def preconnect(_concurrent = false) = :unimplemented

#pool_typeObject

def servers = []



91
# File 'lib/stack-service-base/fiber_pool.rb', line 91

def pool_type = :fiber # :threaded

#sizeObject



81
# File 'lib/stack-service-base/fiber_pool.rb', line 81

def size = @acquired.size

#syncObject

:threaded



92
# File 'lib/stack-service-base/fiber_pool.rb', line 92

def sync = yield