Class: Moneta::Server

Inherits:
Object
  • Object
show all
Includes:
Config
Defined in:
lib/moneta/server.rb

Overview

Moneta server to be used together with Moneta::Adapters::Client

Defined Under Namespace

Classes: Connection

Instance Method Summary collapse

Methods included from Config

#config, included

Constructor Details

#initialize(store, options = {}) ⇒ Server

Returns a new instance of Server.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :port (Integer) — default: 9000

    TCP port

  • :socket (String)

    Alternative Unix socket file name

  • :timeout (Integer) — default: 1

    Number of seconds to timeout on IO.select

  • :max_size (Integer) — default: 0x100000

    Maximum number of bytes allowed to be sent by clients in requests



162
163
164
165
166
167
168
169
170
171
# File 'lib/moneta/server.rb', line 162

def initialize(store, options = {})
  options = configure(**options)
  @store = store
  @server = start(**options)
  @ios = [@server]
  @reads = @ios.dup
  @writes = []
  @connections = {}
  @running = false
end

Instance Method Details

#runObject

Note:

This method blocks!

Run the server



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/moneta/server.rb', line 183

def run
  raise 'Already running' if running?
  @stop = false
  @running = true
  begin
    mainloop until @stop
  ensure
    @running = false
    @server.close unless @server.closed?
    @ios
      .reject { |io| io == @server }
      .each { |io| close_connection(io) }
    File.unlink(config.socket) if config.socket rescue nil
  end
end

#running?Boolean

Is the server running

Returns:

  • (Boolean)

    true if the server is running



176
177
178
# File 'lib/moneta/server.rb', line 176

def running?
  @running
end

#stopObject

Stop the server



200
201
202
203
204
205
# File 'lib/moneta/server.rb', line 200

def stop
  raise 'Not running' unless running?
  @stop = true
  @server.close
  nil
end