跳至内容 跳至搜索

Active Record 连接池的清理器

清理器是一个在进程后台运行的单例,负责维护所有连接池的常规事务。

它会回收借给已死线程的连接,确保一个糟糕的线程不会永远占用池中的一个槽位。根据定义,这涉及到访问当前已借出的连接,但这是安全的,因为已知拥有线程已死。

除此之外,它还管理可用/未借出连接的健康状况。

* retiring connections that have been idle[1] for too long
* creating occasional activity on inactive[1] connections
* keeping the pool prepopulated up to its minimum size
* proactively connecting to the target database from any pooled
  connections that had lazily deferred that step
* resetting or replacing connections that are known to be broken

[1]:“空闲”和“非活动”在此区分了长时间未被应用程序请求的连接(空闲)以及长时间未与其远程服务器通信的连接(非活动)。前者是减少连接数的理想机会(`idle_timeout`);后者是服务器或防火墙可能会丢弃我们仍预期使用的连接的风险(通过 `keepalive` 避免)。

方法
N
R

Attributes

[R] 频率
[R] pool

类公共方法

new(pool, frequency)

# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb, line 36
def initialize(pool, frequency)
  @pool      = pool
  @frequency = frequency
end

实例公共方法

run()

# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb, line 110
def run
  return unless frequency && frequency > 0
  self.class.register_pool(pool, frequency)
end