Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding an echo server example using the Prefork worker pool.

  • Loading branch information...
commit 681664c203e12ab94170621219dfb98f0598ae34 1 parent e6e8dd5
@TwP authored
Showing with 53 additions and 0 deletions.
  1. +53 −0 examples/echo.rb
View
53 examples/echo.rb
@@ -0,0 +1,53 @@
+# Pre-forking echo server using Servolux
+#
+# Run this code using "ruby echo.rb"
+#
+# You can test the server using NetCat from a separate terminal window.
+#
+# echo "hello world" | nc localhost 4242
+#
+# This example was stolen from Ryan Tomayko and modified to demonstrate the
+# Servolux gem. The original can be found here:
+#
+# http://tomayko.com/writings/unicorn-is-unix
+# --------
+
+STDOUT.sync = true
+require 'servolux'
+
+# Create a socket, bind it to localhost:4242, and start listening.
+# Runs once in the parent; all forked children inherit the socket's
+# file descriptor.
+acceptor = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
+address = Socket.pack_sockaddr_in(4242, '0.0.0.0')
+acceptor.bind(address)
+acceptor.listen(10)
+
+# Close the socket when we exit the parent or any child process. This
+# only closes the file descriptor in the calling process, it does not
+# take the socket out of the listening state (until the last fd is
+# closed).
+#
+# The trap is guaranteed to happen, and guaranteed to happen only
+# once, right before the process exits for any reason (unless
+# it's terminated with a SIGKILL).
+trap('EXIT') { acceptor.close }
+
+# Create the worker pool passing in the code to execute in each child
+# process.
+pool = Servolux::Prefork.new {
+ socket, addr = acceptor.accept
+ socket.write "child #$$ echo> "
+ socket.flush
+ message = socket.gets
+ socket.write message
+ socket.close
+ puts "child #$$ echo'd: '#{message.strip}'"
+}
+
+# Start up 3 child process to handle echo requests on the socket.
+pool.start 3
+
+# Stop the child processes when SIGINT is received.
+trap('INT') { pool.stop }
+Process.waitall
Please sign in to comment.
Something went wrong with that request. Please try again.