diff options
author | Eric Wong <normalperson@yhbt.net> | 2013-10-18 10:28:18 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2013-10-18 10:28:18 +0000 |
commit | ab067831e707b191d6dfdcd01de1f1d85fc90d05 (patch) | |
tree | b02861eb1521fb325ee4e1d91e1a194ca73e7a9e /examples | |
download | yahns-ab067831e707b191d6dfdcd01de1f1d85fc90d05.tar.gz |
Diffstat (limited to 'examples')
-rw-r--r-- | examples/README | 3 | ||||
-rw-r--r-- | examples/init.sh | 76 | ||||
-rw-r--r-- | examples/logger_mp_safe.rb | 28 | ||||
-rw-r--r-- | examples/logrotate.conf | 32 | ||||
-rw-r--r-- | examples/yahns_multi.conf.rb | 89 | ||||
-rw-r--r-- | examples/yahns_rack_basic.conf.rb | 27 |
6 files changed, 255 insertions, 0 deletions
diff --git a/examples/README b/examples/README new file mode 100644 index 0000000..c87947a --- /dev/null +++ b/examples/README @@ -0,0 +1,3 @@ +All files in this example directory (including this one) are CC0: +To the extent possible under law, Eric Wong has waived all copyright and +related or neighboring rights to these examples. diff --git a/examples/init.sh b/examples/init.sh new file mode 100644 index 0000000..9464220 --- /dev/null +++ b/examples/init.sh @@ -0,0 +1,76 @@ +#!/bin/sh +# To the extent possible under law, Eric Wong has waived all copyright and +# related or neighboring rights to this examples +set -e +# Example init script, this can be used with nginx, too, +# since nginx and yahns accept the same signals + +# Feel free to change any of the following variables for your app: +TIMEOUT=${TIMEOUT-60} +APP_ROOT=/home/x/my_app/current +PID=$APP_ROOT/tmp/pids/yahns.pid +CMD="/usr/bin/yahns -D -c $APP_ROOT/config/yahns.rb" +INIT_CONF=$APP_ROOT/config/init.conf +action="$1" +set -u + +test -f "$INIT_CONF" && . $INIT_CONF + +old_pid="$PID.oldbin" + +cd $APP_ROOT || exit 1 + +sig () { + test -s "$PID" && kill -$1 `cat $PID` +} + +oldsig () { + test -s $old_pid && kill -$1 `cat $old_pid` +} + +case $action in +start) + sig 0 && echo >&2 "Already running" && exit 0 + $CMD + ;; +stop) + sig QUIT && exit 0 + echo >&2 "Not running" + ;; +force-stop) + sig TERM && exit 0 + echo >&2 "Not running" + ;; +restart|reload) + sig HUP && echo reloaded OK && exit 0 + echo >&2 "Couldn't reload, starting '$CMD' instead" + $CMD + ;; +upgrade) + if sig USR2 && sleep 2 && sig 0 && oldsig QUIT + then + n=$TIMEOUT + while test -s $old_pid && test $n -ge 0 + do + printf '.' && sleep 1 && n=$(( $n - 1 )) + done + echo + + if test $n -lt 0 && test -s $old_pid + then + echo >&2 "$old_pid still exists after $TIMEOUT seconds" + exit 1 + fi + exit 0 + fi + echo >&2 "Couldn't upgrade, starting '$CMD' instead" + $CMD + ;; +reopen-logs) + sig USR1 + ;; +*) + echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>" + exit 1 + ;; +esac diff --git a/examples/logger_mp_safe.rb b/examples/logger_mp_safe.rb new file mode 100644 index 0000000..569d661 --- /dev/null +++ b/examples/logger_mp_safe.rb @@ -0,0 +1,28 @@ +# To the extent possible under law, Eric Wong has waived all copyright and +# related or neighboring rights to this examples +# +# Multi-Processing-safe monkey patch for Logger +# +# This monkey patch fixes the case where "preload: true" is used and +# the application spawns a background thread upon being loaded. +# +# This removes all lock from the Logger code and solely relies on the +# underlying filesystem to handle write(2) system calls atomically when +# O_APPEND is used. This is safe in the presence of both multiple +# threads (native or green) and multiple processes when writing to +# a filesystem with POSIX O_APPEND semantics. +# +# It should be noted that the original locking on Logger could _never_ be +# considered reliable on non-POSIX filesystems with multiple processes, +# either, so nothing is lost in that case. + +require 'logger' +class Logger::LogDevice + def write(message) + @dev.syswrite(message) + end + + def close + @dev.close + end +end diff --git a/examples/logrotate.conf b/examples/logrotate.conf new file mode 100644 index 0000000..ebc92a5 --- /dev/null +++ b/examples/logrotate.conf @@ -0,0 +1,32 @@ +# To the extent possible under law, Eric Wong has waived all copyright and +# related or neighboring rights to this examples +# +# example logrotate config file, I usually keep this in +# /etc/logrotate.d/yahns_app on my Debian systems +# +# See the logrotate(8) manpage for more information: +# http://linux.die.net/man/8/logrotate + +# Modify the following glob to match the logfiles your app writes to: +/var/log/yahns_app/*.log { + # this first block is mostly just personal preference, though + # I wish logrotate offered an "hourly" option... + daily + missingok + rotate 180 + compress # must use with delaycompress below + dateext + + # this is important if using "compress" since we need to call + # the "lastaction" script below before compressing: + delaycompress + + # note the lack of the evil "copytruncate" option in this + # config. yahns supports the USR1 signal and we send it + # as our "lastaction" action: + lastaction + # assuming your pid file is in /var/run/yahns_app/pid + pid=/var/run/yahns_app/pid + test -s $pid && kill -USR1 "$(cat $pid)" + endscript +} diff --git a/examples/yahns_multi.conf.rb b/examples/yahns_multi.conf.rb new file mode 100644 index 0000000..cdcb445 --- /dev/null +++ b/examples/yahns_multi.conf.rb @@ -0,0 +1,89 @@ +# To the extent possible under law, Eric Wong has waived all copyright and +# related or neighboring rights to this example. + +# By default, this based on the soft limit of RLIMIT_NOFILE +# count = Process.getrlimit(:NOFILE)[0]) * 0.5 +# yahns will start expiring idle clients once we hit it +client_expire_threshold 0.5 + +# each queue definition configures a thread pool and epoll_wait usage +# The default queue is always present +queue(:default) do + worker_threads 7 # this is the default value + max_events 1 # 1: fairest, best in all multi-threaded cases +end + +# This is an example of a small queue with fewer workers and unfair scheduling. +# It is rarely necessary or even advisable to configure multiple queues. +queue(:small) do + worker_threads 2 + + # increase max_events only under one of the following circumstances: + # 1) worker_threads is 1 + # 2) epoll_wait lock contention inside the kernel is the biggest bottleneck + # (this is unlikely outside of "hello world" apps) + max_events 64 +end + +# This is an example of a Rack application configured in yahns +# All values below are defaults +app(:rack, "/path/to/config.ru", preload: false) do + listen 8080, backlog: 1024, tcp_nodelay: false + client_max_body_size 1024*1024 + check_client_connection false + logger Logger.new($stderr) + client_timeout 15 + input_buffering true + output_buffering true # output buffering is always lazy if enabled + persistent_connections true + errors $stderr + queue :default +end + +# same as first, just listen on different port and small queue +app(:rack, "/path/to/config.ru") do + listen "10.0.0.1:10000" + client_max_body_size 1024*1024*10 + check_client_connection true + logger Logger.new("/path/to/another/log") + client_timeout 30 + persistent_connections true + errors "/path/to/errors.log" + queue :small +end + +# totally different app +app(:rack, "/path/to/another.ru", preload: true) do + listen 8081, sndbuf: 1024 * 1024 + listen "/path/to/unix.sock" + client_max_body_size 1024*1024*1024 + input_buffering :lazy + output_buffering false + client_timeout 4 + persistent_connections false + + # different apps may share the same queue, but listen on different ports. + queue :default +end + +# yet another totally different app, this app is not-thread safe but fast +# enough for multi-process to not matter. +# Use unicorn if you need multi-process performance on single-threaded apps +app(:rack, "/path/to/not_thread_safe.ru") do + # listeners _always_ get a private thread in yahns + listen "/path/to/yet_another.sock" + listen 8082 + + # inline private queue definition here + queue do + worker_threads 1 # single-threaded queue + max_events 64 # very high max_events is perfectly fair for single thread + end + + # single (or few)-threaded apps must use full buffering if serving + # untrusted/slow clients. + input_buffering true + output_buffering true +end +# Note: this file is used by test_config.rb, be sure to update that +# if we update this diff --git a/examples/yahns_rack_basic.conf.rb b/examples/yahns_rack_basic.conf.rb new file mode 100644 index 0000000..ea367cd --- /dev/null +++ b/examples/yahns_rack_basic.conf.rb @@ -0,0 +1,27 @@ +# To the extent possible under law, Eric Wong has waived all copyright and +# related or neighboring rights to this examples +# A typical Rack example for hosting a single Rack application with yahns +# and only frequently-useful config values + +worker_processes 1 +# working_directory "/path/to/my_app" +stdout_path "/path/to/my_logs/out.log" +stderr_path "/path/to/my_logs/err.log" +pid "/path/to/my_pids/yahns.pid" +client_expire_threshold 0.5 + +queue do + worker_threads 50 +end + +app(:rack, "config.ru", preload: false) do + listen 8080 + client_max_body_size 1024 * 1024 + input_buffering true + output_buffering true # this lazy by default + client_timeout 5 + persistent_connections true +end + +# Note: this file is used by test_config.rb, be sure to update that +# if we update this |