/usr/lib/ruby/vendor_ruby/moneta/pool.rb is in ruby-moneta 0.7.20-2.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 | require 'thread'
module Moneta
# Creates a pool of stores.
# Each thread gets its own store.
#
# @example Add `Moneta::Pool` to proxy stack
# Moneta.build do
# use(:Pool) do
# # Every thread gets its own instance
# adapter :MemcachedNative
# end
# end
#
# @api public
class Pool < Wrapper
# @param [Moneta store] adapter The underlying store
# @param [Hash] options
# @option options [String] :mutex (::Mutex.new) Mutex object
def initialize(options = {}, &block)
super(nil)
@mutex = options[:mutex] || ::Mutex.new
@id = "Moneta::Pool(#{object_id})"
@builder = Builder.new(&block)
@pool, @all = [], []
end
def close
@mutex.synchronize do
raise '#close can only be called when no thread is using the pool' if @all.size != @pool.size
@all.each(&:close)
@all = @pool = nil
end
end
protected
def adapter
Thread.current[@id]
end
def wrap(*args)
store = Thread.current[@id] = pop
yield
ensure
Thread.current[@id] = nil
@mutex.synchronize { @pool << store }
end
def pop
if @mutex.synchronize { @pool.empty? }
store = @builder.build.last
@mutex.synchronize { @all << store }
store
else
@mutex.synchronize { @pool.pop }
end
end
end
end
|