Class: Thrift::NonblockingServer

Inherits:
BaseServer show all
Defined in:
lib/thrift/server/nonblocking_server.rb

Overview

this class expects to always use a FramedTransport for reading messages

Defined Under Namespace

Classes: IOManager

Instance Method Summary collapse

Methods inherited from BaseServer

#to_s

Constructor Details

#initialize(processor, server_transport, transport_factory = nil, protocol_factory = nil, num = 20, logger = nil) ⇒ NonblockingServer

Returns a new instance of NonblockingServer.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/thrift/server/nonblocking_server.rb', line 26

def initialize(processor, server_transport, transport_factory=nil, protocol_factory=nil, num=20, logger=nil)
  super(processor, server_transport, transport_factory, protocol_factory)
  @num_threads = num
  if logger.nil?
    @logger = Logger.new(STDERR)
    @logger.level = Logger::WARN
  else
    @logger = logger
  end
  @shutdown_semaphore = Mutex.new
  @transport_semaphore = Mutex.new
end

Instance Method Details

#serveObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/thrift/server/nonblocking_server.rb', line 39

def serve
  @logger.info "Starting #{self}"
  @server_transport.listen
  @io_manager = start_io_manager

  begin
    loop do
      break if @server_transport.closed?
      begin
        rd, = select([@server_transport], nil, nil, 0.1)
      rescue Errno::EBADF => e
        # In Ruby 1.9, calling @server_transport.close in shutdown paths causes the select() to raise an
        # Errno::EBADF. If this happens, ignore it and retry the loop.
        break
      end
      next if rd.nil?
      socket = @server_transport.accept
      @logger.debug "Accepted socket: #{socket.inspect}"
      @io_manager.add_connection socket
    end
  rescue IOError => e
  end
  # we must be shutting down
  @logger.info "#{self} is shutting down, goodbye"
ensure
  @transport_semaphore.synchronize do
    @server_transport.close
  end
  @io_manager.ensure_closed unless @io_manager.nil?
end

#shutdown(timeout = 0, block = true) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/thrift/server/nonblocking_server.rb', line 70

def shutdown(timeout = 0, block = true)
  @shutdown_semaphore.synchronize do
    return if @is_shutdown
    @is_shutdown = true
  end
  # nonblocking is intended for calling from within a Handler
  # but we can't change the order of operations here, so lets thread
  shutdown_proc = lambda do
    @io_manager.shutdown(timeout)
    @transport_semaphore.synchronize do
      @server_transport.close # this will break the accept loop
    end
  end
  if block
    shutdown_proc.call
  else
    Thread.new &shutdown_proc
  end
end