Guest User

Untitled

a guest
Aug 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
  2. index f721e91203..746e0cf8c1 100644
  3. --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
  4. +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
  5. @@ -2,6 +2,7 @@
  6.  
  7. require "thread"
  8. require "concurrent/map"
  9. +require "concurrent/executor/timer_set"
  10. require "monitor"
  11.  
  12. module ActiveRecord
  13. @@ -285,30 +286,41 @@ def internal_poll(timeout)
  14. # Configure the frequency by setting +reaping_frequency+ in your database
  15. # yaml file (default 60 seconds).
  16. class Reaper
  17. - attr_reader :pool, :frequency
  18. -
  19. - def initialize(pool, frequency)
  20. - @pool = pool
  21. - @frequency = frequency
  22. + def initialize
  23. + @timer_set = Concurrent::TimerSet.new(
  24. + executor: Concurrent::FixedThreadPool.new(1)
  25. + )
  26. end
  27.  
  28. def run
  29. - return unless frequency && frequency > 0
  30. - Thread.new(frequency, pool) { |t, p|
  31. - loop do
  32. - sleep t
  33. - p.reap
  34. - p.flush
  35. - end
  36. - }
  37. + Thread.new do
  38. + klass = ActiveRecord::ConnectionAdapters::ConnectionPool
  39. + ObjectSpace.each_object(klass) { |pool| queue_reaping(pool) }
  40. + end
  41. end
  42. +
  43. + private
  44. +
  45. + def queue_reaping(pool)
  46. + @timer_set.post(pool.reaping_frequency) do
  47. + if !pool.connections.empty?
  48. + pool.reap
  49. + pool.flush
  50. + end
  51. +
  52. + queue_reaping(pool)
  53. + end
  54. + end
  55. end
  56.  
  57. include MonitorMixin
  58. include QueryCache::ConnectionPoolConfiguration
  59.  
  60. attr_accessor :automatic_reconnect, :checkout_timeout, :schema_cache
  61. - attr_reader :spec, :connections, :size, :reaper
  62. + attr_reader :spec, :connections, :size, :reaping_frequency
  63. +
  64. + @@pools_to_reap = []
  65. + @@reaper = nil
  66.  
  67. # Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification
  68. # object which describes database connection information (e.g. adapter,
  69. @@ -321,14 +333,12 @@ def initialize(spec)
  70.  
  71. @spec = spec
  72.  
  73. - @checkout_timeout = (spec.config[:checkout_timeout] && spec.config[:checkout_timeout].to_f) || 5
  74. - if @idle_timeout = spec.config.fetch(:idle_timeout, 300)
  75. - @idle_timeout = @idle_timeout.to_f
  76. - @idle_timeout = nil if @idle_timeout <= 0
  77. - end
  78. + @checkout_timeout = spec.config.fetch(:checkout_timeout, 5).to_f
  79. + @idle_timeout = spec.config.fetch(:idle_timeout, 300).to_f
  80. + @idle_timeout = nil if @idle_timeout <= 0
  81.  
  82. # default max pool size to 5
  83. - @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
  84. + @size = spec.config.fetch(:pool, 5).to_i
  85.  
  86. # This variable tracks the cache of threads mapped to reserved connections, with the
  87. # sole purpose of speeding up the +connection+ method. It is not the authoritative
  88. @@ -358,9 +368,9 @@ def initialize(spec)
  89.  
  90. # +reaping_frequency+ is configurable mostly for historical reasons, but it could
  91. # also be useful if someone wants a very low +idle_timeout+.
  92. - reaping_frequency = spec.config.fetch(:reaping_frequency, 60)
  93. - @reaper = Reaper.new(self, reaping_frequency && reaping_frequency.to_f)
  94. - @reaper.run
  95. + @reaping_frequency = spec.config.fetch(:reaping_frequency, 1).to_f
  96. + @@pools_to_reap << self if reaping_frequency > 0
  97. + @@reaper ||= Reaper.new.run
  98. end
  99.  
  100. def lock_thread=(lock_thread)
Add Comment
Please, Sign In to add comment