Class: Thin::Backends::AttachSocket

Inherits:
Base
  • Object
show all
Defined in:
lib/thin/attach_socket.rb

Overview

Backend to act as a TCP server using an already opened file descriptor. Currently requires a patched EventMachine like github.com/ibc/EventMachine-LE

If you’re running thin inside a reactor with other servers, then pass preserve_reactor: true. This ensures that when you stop the Thin server, it will not stop your reactor.

If you’re doing that you probably also want to pass signals: false so that you can control when thin shuts down:

Examples:


Thin::Server.new(MyApp,
                 backend: Thin::Backends::AttachSocket,
                 socket: IO.for_fd(6))

Thin::Server.new(MyApp,
                 backend: Thin::Backends::AttachSocket,
                 socket: IO.for_fd(6),
                 signals: false,
                 preserve_reactor: true)

Instance Method Summary collapse

Constructor Details

#initialize(host, port, options) ⇒ AttachSocket

Returns a new instance of AttachSocket.



39
40
41
42
43
# File 'lib/thin/attach_socket.rb', line 39

def initialize(host, port, options)
  @socket = options[:socket]
  @preserve_reactor = !!options[:preserve_reactor]
  super()
end

Instance Method Details

#connectObject



45
46
47
# File 'lib/thin/attach_socket.rb', line 45

def connect
  @signature = EventMachine.attach_server(@socket, Connection, &method(:initialize_connection))
end

#disconnectObject



49
50
51
# File 'lib/thin/attach_socket.rb', line 49

def disconnect
  EventMachine.stop_server(@signature)
end

#stop!Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/thin/attach_socket.rb', line 57

def stop!
  if @preserve_reactor
    # This is the code for super, with the EM::stop call removed.
    @running  = false
    @stopping = false

    @connections.each { |connection| connection.close_connection_after_writing }
    close
  else
    super
  end
end

#to_sObject



53
54
55
# File 'lib/thin/attach_socket.rb', line 53

def to_s
  "socket:#{@socket.inspect}"
end