-
Notifications
You must be signed in to change notification settings - Fork 51
/
thin_example
executable file
·53 lines (44 loc) · 1.18 KB
/
thin_example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby
#
# An example application using our patched Thin and EventMachine. You
# can obtain these from:
#
# https://github.com/stripe/thin.git, and
# https://github.com/stripe/eventmachine.git
require 'rubygems'
require 'einhorn'
require 'eventmachine-le'
require 'thin'
require 'thin/attach_socket'
class App
def initialize(id)
@id = id
end
def call(env)
return [200, {}, "[#{$$}] From server instance #{@id}: Got your request!\n"]
end
end
def einhorn_main
puts "Called with #{ARGV.inspect}"
fd_count = Einhorn::Worker.einhorn_fd_count
unless fd_count > 0
raise "Need to call with at least one bound socket. Try running 'einhorn -b 127.0.0.1:5000,r,n -b 127.0.0.1:5001,r,n #{$0}' and then running 'curl 127.0.0.1:5000' or 'curl 127.0.0.1:5001'"
end
Einhorn::Worker.graceful_shutdown do
puts "#{$$} is now exiting..."
exit(0)
end
Einhorn::Worker.ack!
EventMachine.run do
(0...fd_count).each do |i|
sock = Einhorn::Worker.socket!(i)
srv = Thin::Server.new(App.new(i),
:backend => Thin::Backends::AttachSocket,
:socket => IO.for_fd(sock))
srv.start
end
end
end
if $0 == __FILE__
einhorn_main
end