Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update docs and examples for environment variable interface

  • Loading branch information...
commit ba1a2d67152ee1a3d9c683cd3b59185f490a8cb9 1 parent c2edca3
Greg Brockman gdb authored

Showing 3 changed files with 25 additions and 29 deletions. Show diff stats Hide diff stats

  1. +13 14 bin/einhorn
  2. +5 4 example/thin_example
  3. +7 11 example/time_server
27 bin/einhorn
@@ -45,24 +45,23 @@ You can communicate your running Einhorn process via `einhornsh`:
45 45
46 46 If your process is a server and listens on one or more sockets,
47 47 Einhorn can open these sockets and pass them to the workers. You can
48   -specify the addresses to bind by setting the EINHORN_SRVS environment
49   -variable to an address or an array of addresses:
  48 +specify the addresses to bind by passing one or more `-b ADDR`
  49 +arguments:
50 50
51   - EINHORN_SRVS=ADDR
52   - EINHORN_SRVS=[ADDR, ADDR, ...]
  51 + einhorn -b 127.0.0.1:1234 my-command
  52 + einhorn -b 127.0.0.1:1234,r -b 127.0.0.1:1235 my-command
53 53
54 54 Each address is specified as an ip/port pair, possibly accompanied by options:
55 55
56 56 ADDR := (IP:PORT)[<,OPT>...]
57 57
58   -The opened file descriptors will be represented as an array in EINHORN_FDS:
  58 +In the worker process, the opened file descriptors will be represented
  59 +as a space-separated list of file descriptor numbers in the
  60 +EINHORN_FDS environment variable (respecting the order that the `-b`
  61 +options were provided in):
59 62
60   - EINHORN_FDS = [FD]
61   - EINHORN_FDS = [FD, FD, ...]
62   -
63   -Where each FD is the file descriptor number of the corresponding
64   -socket. If EINHORN_SRVS is a string, EINHORN_FDS will be a 1-element
65   -array.
  63 + EINHORN_FDS="6" # 127.0.0.1:1234
  64 + EINHORN_FDS="6 7" # 127.0.0.1:1234,r 127.0.0.1:1235
66 65
67 66 Valid opts are:
68 67
@@ -71,11 +70,11 @@ Valid opts are:
71 70
72 71 You can for example run:
73 72
74   - $ EINHORN_SRVS=127.0.0.1:2345,r einhorn -m manual -n 4 example/time_server
  73 + $ einhorn -b 127.0.0.1:2345,r -m manual -n 4 -- example/time_server
75 74
76 75 Which will run 4 copies of
77 76
78   - EINHORN_FDS=[6] example/time_server
  77 + EINHORN_FDS=6 example/time_server
79 78
80 79 Where file descriptor 6 is a server socket bound to `127.0.0.1:2345`
81 80 and with `SO_REUSEADDR` set. It is then your application's job to
@@ -131,7 +130,7 @@ string
131 130 to the UNIX socket pointed to by the environment variable
132 131 `EINHORN_SOCK_PATH`. (Be sure to include a trailing newline.)
133 132
134   -To make things even easier, you can pass a `-b` to Einhorn, in which
  133 +To make things even easier, you can pass a `-g` to Einhorn, in which
135 134 case you just need to `write()` the above message to the open file
136 135 descriptor pointed to by `EINHORN_SOCK_FD`.
137 136
9 example/thin_example
@@ -28,8 +28,10 @@ end
28 28 def einhorn_main
29 29 puts "Called with #{ARGV.inspect}"
30 30
31   - if ARGV.length == 0
32   - raise "Need to call with at least one argument. Try running 'einhorn #{$0} srv:127.0.0.1:5000,r,n srv:127.0.0.1:5001,r,n' and then running 'curl 127.0.0.1:5000' or 'curl 127.0.0.1:5001'"
  31 + einhorn_fds = Einhorn::Worker.einhorn_fds
  32 +
  33 + unless einhorn_fds
  34 + 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'"
33 35 end
34 36
35 37 Einhorn::Worker.graceful_shutdown do
@@ -39,8 +41,7 @@ def einhorn_main
39 41 Einhorn::Worker.ack!
40 42
41 43 EventMachine.run do
42   - ARGV.each_with_index do |arg, i|
43   - sock = Integer(arg)
  44 + einhorn_fds.each_with_index do |sock, i|
44 45 srv = Thin::Server.new(sock, App.new(i), :reuse => true)
45 46 srv.start
46 47 end
18 example/time_server
... ... @@ -1,28 +1,24 @@
1 1 #!/usr/bin/env ruby
2 2 #
3 3 # A simple example showing how to use Einhorn's shared-socket
4   -# features. Einhorn translates the srv:(addr:port[,flags...]) spec in
5   -# the arg string into a file descriptor number.
  4 +# features. Einhorn translates the (addr:port[,flags...]) bind spec in
  5 +# into a file descriptor number in the EINHORN_FDS environment variable.
6 6 #
7 7 # Invoke through Einhorn as
8 8 #
9   -# einhorn ./time_server srv:127.0.0.1:2345,r
  9 +# einhorn -b 127.0.0.1:2345,r ./time_server
10 10 #
11 11 # or, if you want to try out preloading:
12 12 #
13   -# einhorn -p ./time_server ./time_server srv:127.0.0.1:2345,r
14   -
  13 +# einhorn -b 127.0.0.1:2345,r -p ./time_server ./time_server
15 14 require 'rubygems'
16 15 require 'einhorn/worker'
17 16
18 17 def einhorn_main
19   - puts "Called with #{ARGV.inspect}"
20   -
21   - if ARGV.length != 1
22   - raise "Need to call with a port spec as the first argument. Try running 'einhorn #{$0} srv:127.0.0.1:2345,r' and then running 'nc 127.0.0.1 2345'"
23   - end
  18 + puts "Called with ENV['EINHORN_FDS']: #{ENV['EINHORN_FDS']}"
24 19
25   - socket = Socket.for_fd(Integer(ARGV[0]))
  20 + fd_num = Einhorn::Worker.socket!
  21 + socket = Socket.for_fd(fd_num)
26 22
27 23 # Came up successfully, so let's set up graceful handler and ACK the
28 24 # master.

0 comments on commit ba1a2d6

Please sign in to comment.
Something went wrong with that request. Please try again.