class Moneta::Shared

Shares a store between processes

@example Share a store

Moneta.build do
  use :Transformer, key: :marshal, value: :marshal
  use :Shared do
    adapter :GDBM, file: 'shared.db'
  end
end

@api public

Public Class Methods

new(options = {}, &block) click to toggle source

@param [Hash] options @option options [Integer] :port (9000) TCP port @option options [String] :host Server hostname @option options [String] :socket Unix socket file name

# File lib/moneta/shared.rb, line 18
def initialize(options = {}, &block)
  @options = options
  @builder = Builder.new(&block)
  @connect_lock = ::Mutex.new
end

Public Instance Methods

close() click to toggle source

(see Proxy#close)

# File lib/moneta/shared.rb, line 25
def close
  if server?
    @server.stop
    @thread.join
    @server = @thread = nil
  end
  if @adapter
    @adapter.close
    @adapter = nil
  end
end
server?() click to toggle source

Returns true if this wrapper is running as the server

@return [Boolean] wrapper is a server

# File lib/moneta/shared.rb, line 40
def server?
  @server != nil
end

Protected Instance Methods

connect() click to toggle source
# File lib/moneta/shared.rb, line 55
def connect
  return if @adapter
  @connect_lock.synchronize do
    @adapter ||= Adapters::Client.new(@options)
  end
rescue Errno::ECONNREFUSED, Errno::ENOENT, IOError => ex
  start_server
  tries ||= 0
  warn "Moneta::Shared - Failed to connect: #{ex.message}" if tries > 0
  (tries += 1) < 10 ? retry : raise
end
start_server() click to toggle source

TODO: Implement this using forking (MRI) and threading (JRuby) to get maximal performance

# File lib/moneta/shared.rb, line 69
def start_server
  @connect_lock.synchronize do
    return if server?
    begin
      raise "Adapter already set" if @adapter
      @adapter = Lock.new(@builder.build.last)
      raise "Server already set" if server?
      @server = Server.new(@adapter, @options)
      @thread = Thread.new { @server.run }
      sleep 0.1 until @server.running?
    rescue => ex
      @adapter.close if @adapter
      @adapter = nil
      @server = nil
      @thread = nil
      warn "Moneta::Shared - Failed to start server: #{ex.message}"
    end
  end
end
wrap(*args) { || ... } click to toggle source
# File lib/moneta/shared.rb, line 46
def wrap(*args)
  connect
  yield
rescue Errno::ECONNRESET, Errno::EPIPE, IOError, SystemCallError
  @connect_lock.synchronize { close unless server? }
  tries ||= 0
  (tries += 1) < 3 ? retry : raise
end