Class: Rubcask::Server::Async

Inherits:
AbstractServer show all
Defined in:
lib/rubcask/server/async.rb

Overview

Async-based server supporting Rubcask protocol It requires “async-io” gem.

Constant Summary

Constants inherited from AbstractServer

Rubcask::Server::AbstractServer::BLOCK_SIZE, Rubcask::Server::AbstractServer::MAX_READ_SIZE

Constants included from Protocol

Protocol::ERROR, Protocol::NIL, Protocol::OK, Protocol::PING, Protocol::PONG, Protocol::SEPARATOR

Instance Attribute Summary

Attributes inherited from AbstractServer

#dir

Instance Method Summary collapse

Methods included from Protocol

create_call_message, encode_message, error_message, nil_message, ok_message, ping_message, pong_message

Constructor Details

#initialize(dir, config: Server::Config.new) ⇒ Async



14
15
16
17
18
19
20
21
# File 'lib/rubcask/server/async.rb', line 14

def initialize(dir, config: Server::Config.new)
  @dir = dir
  @config = config
  @hostname = config.hostname
  @port = config.port
  @logger = Logger.new($stdout)
  @endpoint = ::Async::IO::Endpoint.tcp(@hostname, @port)
end

Instance Method Details

#shutdownObject

Note:

You might want to use it inside signal trap

Shuts down the server



25
26
27
28
29
30
31
# File 'lib/rubcask/server/async.rb', line 25

def shutdown
  return unless @task
  Sync do
    @shutdown_condition.signal
    @task.wait
  end
end

#start(on_start_condition = nil) ⇒ Object

Starts the server



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubcask/server/async.rb', line 35

def start(on_start_condition = nil)
  Async do
    @shutdown_condition = ::Async::Condition.new

    _, @task = @endpoint.bind do |server, task|
      if @config.keepalive
        server.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
      end

      define_close_routine(server, task)

      Console.logger.info(server) { "Accepting connections on #{server.local_address.inspect}" }

      server.listen(Socket::SOMAXCONN)
      on_start_condition&.signal

      server.accept_each do |conn|
        conn.binmode
        client_loop(::Async::IO::Stream.new(conn))
      end
    end
  end
end