Skip to content

Instantly share code, notes, and snippets.

@moonpolysoft
Created March 3, 2009 18:11
Show Gist options
  • Save moonpolysoft/73430 to your computer and use it in GitHub Desktop.
Save moonpolysoft/73430 to your computer and use it in GitHub Desktop.
require 'thin'
module Thin
module Backends
class DualServer < Base
# UNIX domain socket on which the server is listening for connections.
attr_accessor :socket, :host, :port
def initialize(host, port, opts={})
puts "host #{host.inspect} port #{port.inspect} opts #{opts.inspect}"
raise PlatformNotSupported, 'UNIX domain sockets not available on Windows' if Thin.win?
@socket = opts[:socket]
@host = host
@port = port
super()
end
# Connect the server
def connect
at_exit { remove_socket_file } # In case it crashes
EventMachine.start_unix_domain_server(@socket, UnixConnection, &method(:initialize_connection))
# HACK EventMachine.start_unix_domain_server doesn't return the connection signature
# so we have to go in the internal stuff to find it.
@unix_signature = EventMachine.instance_eval{@acceptors.keys.first}
@tcp_signature = EventMachine.start_server(@host, @port, Thin::Connection, &method(:initialize_connection))
end
# Stops the server
def disconnect
EventMachine.stop_server(@unix_signature)
EventMachine.stop_server(@signature)
end
# Free up resources used by the backend.
def close
remove_socket_file
end
def to_s
@socket
end
protected
def remove_socket_file
File.delete(@socket) if @socket && File.exist?(@socket)
end
end
end
class UnixConnection < Connection
protected
def socket_address
'127.0.0.1' # Unix domain sockets can only be local
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment