Class: KStor::SocketServer

Inherits:
Object
  • Object
show all
Defined in:
lib/kstor/socket_server.rb

Overview

Serve clients on UNIX sockets.

Direct Known Subclasses

Server

Constant Summary collapse

GRACEFUL_TIMEOUT =

Wait this number of seconds for worker threads to terminate before killing them without mercy.

10

Instance Method Summary collapse

Constructor Details

#initialize(socket_path:, nworkers:) ⇒ SocketServer

Create a new server.

Parameters:

  • socket_path (String)

    path to listening socket

  • nworkers (Integer)

    number of worker threads



20
21
22
23
24
25
# File 'lib/kstor/socket_server.rb', line 20

def initialize(socket_path:, nworkers:)
  @path = socket_path
  @nworkers = nworkers
  @client_queue = Queue.new
  @workers = []
end

Instance Method Details

#startObject

Start serving clients.

Send interrupt signal to cleanly stop.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kstor/socket_server.rb', line 30

def start
  start_workers
  server = server_socket
  Systemd.service_ready
  Log.info('socket_server: started')
  loop do
    maintain_workers
    @client_queue.enq(server.accept.first)
  end
rescue Interrupt
  stop(server)
  Log.info('socket_server: stopped.')
end

#work(client) ⇒ Object

This method is abstract.

Do some work for a client and write a response.



46
47
48
49
# File 'lib/kstor/socket_server.rb', line 46

def work(client)
  # Abstract method.
  client.close
end