svn commit: r381154 - in head/www: nginx nginx-devel nginx-devel/files nginx/files rubygem-passenger rubygem-passenger/files

Sergey A. Osokin osa at FreeBSD.org
Fri Mar 13 00:06:32 UTC 2015


Author: osa
Date: Fri Mar 13 00:06:29 2015
New Revision: 381154
URL: https://svnweb.freebsd.org/changeset/ports/381154
QAT: https://qat.redports.org/buildarchive/r381154/

Log:
  Upgrade from 4.0.59 to 5.0.2:
  
  o) www/rubygem-passenger;
  o) third-party modules for www/nginx and www/nginx-devel.
  
  <ChangeLog>
  
  Release 5.0.2
  -------------
  
   * Fixes a connection freeze that could occur when processing large responses. This would
     manifest itself under the error message "This website is under heavy load" or "Request
     queue is full, returning an error". Closes GH-1404.
   * Debian and Ubuntu packages have been reintroduces.
   * When `passenger-config restart-app` is run interactively, if Passenger is not serving
     any applications, then the command now prints an error message instead of showing a
     menu with only a "Cancel" option.
   * Fixes a compilation problem on FreeBSD 10 (contributed by: clemensg). Closes GH-1401.
   * [Standalone] Fixes a crash that would occur if you use the `--ctl` parameter.
   * [Enterprise] The `--max-request-time` option has been added to Passenger Standalone.
   * [Enterprise] The `max_request_time_reached` hook has been introduced. This hook allows
     you to run diagnostics on a process that that took too long to respond to a request.
  
  Release 5.0.1
  -------------
  
   * The `passenger-config restart-app` command is now more user friendly. When run in a
     terminal, it will show an interactive menu, allowing you to select the app to restart.
     Closes GH-1387.
   * Fixed a crash bug in the handling of sticky session cookies.
   * Log failed program in error message, not its command line (contributed by: paisleyrob).
     Closes GH-1397.
   * [Nginx] Fixes cases in which Passenger overrides the Nginx handler function even when
     it shouldn't, for example when Passenger is disabled. Closes GH-1393.
   * [Enterprise] The `sticky_sessions` and `envvars` options in Passengerfile.json is now
     also supported in mass deployment mode.
  
  Release 5.0.0 release candidate 2
  ---------------------------------
  
   * Fixes an installation problem with the Ruby gem due to incorrect Makefile generation.
     Closes GH-1382.
   * More helpful message when request queue is full. Closes GH-1375.
  
  Release 5.0.0 release candidate 1
  ---------------------------------
  
   * Fixed Date headers not being formatted in the GMT timezone. Closes GH-1367.
   * Fixed Passengerfile.json/passenger-standalone.json not being properly loaded in
     Passenger Standalone.
   * Fixed support for sticky sessions.
   * Fixed an infinite loop if the ApplicationPool garbage collector fails due to an
     exception. Closes GH-1360.
   * Fixed Passenger Standalone exiting prematurely when the HelperAgent crashes. Exiting
     prematurely is not supposed to happen because the watchdog will restart the HelperAgent.
     Closes GH-1339.
   * Fixed a crash that occurs when using a non-standard startup file value. Closes GH-1378.
   * When dumping system metrics during error page generation, the `passenger-config`
     command is now invoked under the same Ruby interpreter as the app, instead of the
     one in PATH. Closes GH-1381.
   * When a Ruby process crashes due to an uncaught exception, this fact is now properly
     logged.
   * Specifying 0 for the `max_pool_size` config option no longer results in a crash.
     Closes GH-1334.
   * The timeouts when downloading Passenger Standalone binaries and source files are
     now customizable. Closes GH-1295.
   * The `envvars` option is now supported in Passengerfile.json, for passing environment
     variables to the application. Closes GH-1377.
   * Introduced `hook_queue_full_error` for request queue overflows. Closes GH-1358.
   * [Ruby] Fixed handling of "transfer-encoding chunked" response bodies which contain
     zero-sized chunks.
   * [Nginx] It is no longer necessary to re-specify `passenger_enabled` in `location`
     contexts. Closes GH-1338.
   * [Enterprise] Fixed a bug in mass deployment reloading.
   * [Enterprise] Fixed a bug in mass deployment daemonization.
   * [Enterprise] The mass deployment mode now supports the `app_type` and `startup_file`
     configuration options in Passengerfile.json/passenger-standalone.json. Closes GH-1366.
  
  Release 5.0.0 beta 3
  --------------------
  
   * The turbocache has received major updates and fixes based on excellent feedback Chris
     Heald and the community. First, several bugs w.r.t. the handling of caching headers
     have been fixed. Second, the turbocache has become slightly more conservative for
     security reasons. In previous versions, default cacheable responses (as defined by RFC
     7234) were cached unless caching headers tell us not to. Now, default cacheable responses
     are only cached if caching headers explicitly tell us to. This change was introduced
     because there are many applications that set incorrect caching headers on private
     responses. This new behavior is currently not configurable, but there are plans to make
     it configurable in 5.0.0 release candidate 1.
   * Introduced a new configuration option, `passenger_response_buffer_high_watermark` (Nginx)
     and `PassengerResponseBufferHighWatermark` (Apache), for configuring the behavior of the
     response buffering system. Closes GH-1300.
   * Fixed more cookie handling issues. Closes GH-1310.
   * Fixed various WebSocket issues. Closes GH-1306.
   * Fixed some crashes caused by race conditions. Closes GH-1326.
   * Fixed issues with handling POST data. Closes GH-1331.
   * Fixed some issues on Heroku. Closes GH-1329.
   * Fixed some integer overflows. Fix contributed by Go Maeda. Closes GH-1357.
   * Fixed the `passenger-status --show=union_station` command. Closes GH-1336.
   * Nginx versions earlier than 1.6 are no longer supported.
   * Improved state introspection.
  
  Release 5.0.0 beta 2
  --------------------
  
   * Fixed handling of multiple Set-Cookie headers. Closes GH-1296.
   * `passenger-config system-metrics` now works properly if the agent is installed in
     ~/.passenger. Closes GH-1304.
   * Documentation enhancements by Igor Vuk. Closes GH-1318.
   * Fixed some crasher bugs.
   * [Standalone] User switching is now correctly disabled.
   * [Standalone] Fixed the `--thread-count` parameter.
   * [Apache] IPs set by mod_remoteip are now respected. Closes GH-1284.
   * [Apache] Fixed support for gzipped chunked responses. Closes GH-1309.
  
  Release 5.0.0 beta 1
  --------------------
  
  Version 5.0.0 beta 1 contains major changes. It's mostly compatible with version 4, but there
  are a few minor breakages, which are described below. Major changes and notable breakages are:
  
   * Performance has been much improved. This is thanks to months of optimization work. You can
     learn more at www.rubyraptor.org.
   * We've published a [server optimization guide]
     (https://www.phusionpassenger.com/documentation/ServerOptimizationGuide.html) for those who
     are interested in tuning Phusion Passenger.
   * Support for Rails 1.2 - 2.2 has been removed, for performance reasons. Rails 2.3 is still
     supported.
   * Phusion Passenger now supports integrated HTTP caching, which we call turbocaching. If your
     app sets the right HTTP headers then Phusion Passenger can tremendously accelerate your app.
     It is enabled by default, but you can disable it with `--disable-turbocaching` (Standalone),
     `PassengerTurbocaching off` (Apache), or 'passenger_turbocaching off' (Nginx).
   * Touching restart.txt will no longer restart your app immediately. This is because, for
     performance reasons, the stat throttle rate now defaults to 10. You can still get back the
     old behavior by setting `PassengerStatThrottleRate 0` (Apache) or
     `passenger_stat_throttle_rate 0` (Nginx), but this is not encouraged. Instead, we encourage
     you to use the `passenger-config restart-app` tool to initiate restarts, which has immediate
     effect.
   * Websockets are now properly disconnected on application restarts.
   * The Phusion Passneger log levels have been completely revamped. If you were setting a log
     level before (e.g. through `passenger_log_level`), please read the latest documentation to
     learn about the new log levels.
   * If you use out-of-band garbage collection, beware that the `X-Passenger-Request-OOB-Work`
     header has now been renamed to `!~Request-OOB-Work`.
   * When using Rack's full socket hijacking, you must now output an HTTP status line.
   * [Nginx] The `passenger_set_cgi_param` option has been removed and replaced by
     `passenger_set_header` and `passenger_env_var`.
   * [Nginx] `passenger_show_version_in_header` is now only valid in the `http` context.
   * [Apache] The `PassengerStatThrottleRate` option is now global.
  
  Minor changes:
  
   * The minimum required Nginx version is now 1.6.0.
   * The instance directory is now touched every hour instead of every 6 hours. This should
     hopefully prevent more problems with /tmp cleaner daemons.
   * Applications are not grouped not only on the application root path, but also on the
     environment. For example, this allows you to run the same app in both production and staging
     mode, with only a single directory, without further configuration. Closes GH-664.
   * The `passenger_temp_dir` option (Nginx) and the `PassengerTempDir` option (Apache) have been
     replaced by two config options. On Nginx they are `passenger_instance_registry_dir` and
     `passenger_data_buffer_dir`. On Apache they are `PassengerInstanceRegistryDir` and
     `PassengerDataBufferDir`. On Apache, `PassengerUploadBufferDir` has been replaced by
     `PassengerDataBufferDir`.
   * Command line tools no longer respect the `PASSENGER_TEMP_DIR` environment variable.
     Use `PASSENGER_INSTANCE_REGISTRY_DIR` instead.
   * `passenger-status --show=requests` has been deprecated in favor of
     `passenger-status --show=connections`.
   * Using the SIGUSR1 signal to restart a Ruby app without dropping connections, is no longer
     supported. Instead, use `passenger-config detach-process`.
   * Introduced the `passenger-config reopen-logs` command, which instructs all Phusion Passenger
     agent processes to reopen their log files. You should call this after having rotated the web
     server logs.
   * [Standalone] The Phusion Passenger Standalone config template has changed. Users are
     encouraged to update it.
   * [Standalone] `passenger-standalone.json` has been renamed to `Passengerfile.json`.
   * [Standalone] `passenger-standalone.json`/`Passengerfile.json` no longer overrides command
     line options. Instead, command line options now have the highest priority.
  
  Release 4.0.60
  --------------
  
   * Fixed the password protection of internal Phusion Passenger processes.
  
     For security reasons, Phusion Passenger limits access to internal processes, by using Unix
     file permissions and randomly generated passwords that only authorized internal processes
     know. It turns out that this password wasn't set correctly, which has now been fixed.
     There was no security vulnerability, because the file permissions already provide
     sufficient security. The password only serves as an extra layer of security just in case
     there is a problem with the former.
  
     This issue is not at all related to any application-level security or application-level
     passwords. Any database passwords, keys, or secrets used and generated by applications
     have got nothing to do with the nature of this issue. This issue only relates to some
     randomly generated passwords that Passenger uses internally, for its internal operations.
  
  </ChangeLog>

Added:
  head/www/rubygem-passenger/files/patch-ext-common-ServerKit-AcceptLoadBalancer.h   (contents, props changed)
Modified:
  head/www/nginx-devel/Makefile
  head/www/nginx-devel/distinfo
  head/www/nginx-devel/files/extra-patch-passenger-build-nginx.rb
  head/www/nginx/Makefile
  head/www/nginx/distinfo
  head/www/nginx/files/extra-patch-passenger-build-nginx.rb
  head/www/rubygem-passenger/Makefile
  head/www/rubygem-passenger/distinfo

Modified: head/www/nginx-devel/Makefile
==============================================================================
--- head/www/nginx-devel/Makefile	Thu Mar 12 23:29:49 2015	(r381153)
+++ head/www/nginx-devel/Makefile	Fri Mar 13 00:06:29 2015	(r381154)
@@ -642,7 +642,7 @@ CONFIGURE_ARGS+=--without-http_rewrite_m
 .endif
 
 .if ${PORT_OPTIONS:MPASSENGER}
-PASSENGER_VERSION=	4.0.59
+PASSENGER_VERSION=	5.0.2
 MASTER_SITES+=	http://s3.amazonaws.com/phusion-passenger/releases/:passenger
 DISTFILES+=	passenger-${PASSENGER_VERSION}.tar.gz:passenger
 CONFIGURE_ARGS+=--add-module=${WRKDIR}/passenger-${PASSENGER_VERSION}/ext/nginx

Modified: head/www/nginx-devel/distinfo
==============================================================================
--- head/www/nginx-devel/distinfo	Thu Mar 12 23:29:49 2015	(r381153)
+++ head/www/nginx-devel/distinfo	Fri Mar 13 00:06:29 2015	(r381154)
@@ -72,8 +72,8 @@ SHA256 (modsecurity-2.9.0.tar.gz) = e2bb
 SIZE (modsecurity-2.9.0.tar.gz) = 4246467
 SHA256 (naxsi-core-0.50.tgz) = 7cece5f9d9c5df9e09af1e1023bc8e04bbbbe953e67461b893b8240e82ca52ef
 SIZE (naxsi-core-0.50.tgz) = 49809
-SHA256 (passenger-4.0.59.tar.gz) = 8b4756564a5133dc97425883d15d1ba1c9f6566c79e92794dc3e1f5b749238ae
-SIZE (passenger-4.0.59.tar.gz) = 4521206
+SHA256 (passenger-5.0.2.tar.gz) = 93aa200fdc47597c0dcbe32ed6c55af0751114d2c268e4d30ff7f3d7afdbebe8
+SIZE (passenger-5.0.2.tar.gz) = 5287262
 SHA256 (ngx_postgres-0.9.tar.gz) = aac0141e73b5c440927b7e040072f120b36e49deb2ff36ad00ff96a80622fbec
 SIZE (ngx_postgres-0.9.tar.gz) = 41022
 SHA256 (openresty-rds-csv-nginx-module-v0.05-0-g607e26b.tar.gz) = d5f14e45c6dc0d039ad52ae889775b5af1ff80a1c8edb69361c1581aca5d0b8f

Modified: head/www/nginx-devel/files/extra-patch-passenger-build-nginx.rb
==============================================================================
--- head/www/nginx-devel/files/extra-patch-passenger-build-nginx.rb	Thu Mar 12 23:29:49 2015	(r381153)
+++ head/www/nginx-devel/files/extra-patch-passenger-build-nginx.rb	Fri Mar 13 00:06:29 2015	(r381154)
@@ -1,22 +1,18 @@
---- ../passenger-4.0.59/build/nginx.rb.orig	2013-10-27 02:00:00.000000000 +0400
-+++ ../passenger-4.0.59/build/nginx.rb	2014-06-02 05:55:12.000000000 +0400
+--- ../passenger-5.0.2/build/nginx.rb.orig	2013-10-27 02:00:00.000000000 +0400
++++ ../passenger-5.0.2/build/nginx.rb	2015-03-07 06:07:38.000000000 +0300
 @@ -32,6 +32,7 @@
  desc "Build Nginx support files"
  task :nginx => [
- 	:nginx_without_native_support,
-+	LIBBOOST_OXT,
- 	NATIVE_SUPPORT_TARGET
+   :nginx_without_native_support,
++  LIBBOOST_OXT,
+   NATIVE_SUPPORT_TARGET
  ].compact
  
-@@ -40,11 +41,6 @@
+@@ -40,7 +41,6 @@
  
  task :nginx_without_native_support => [
- 	auto_generated_sources,
--	AGENT_OUTPUT_DIR + 'PassengerHelperAgent',
--	AGENT_OUTPUT_DIR + 'PassengerWatchdog',
--	AGENT_OUTPUT_DIR + 'PassengerLoggingAgent',
--	AGENT_OUTPUT_DIR + 'SpawnPreparer',
--	AGENT_OUTPUT_DIR + 'TempDirToucher',
- 	COMMON_LIBRARY.only(*NGINX_LIBS_SELECTOR).link_objects
+   auto_generated_sources,
+-  AGENT_OUTPUT_DIR + AGENT_EXE,
+   COMMON_LIBRARY.only(*NGINX_LIBS_SELECTOR).link_objects
  ].flatten
  

Modified: head/www/nginx/Makefile
==============================================================================
--- head/www/nginx/Makefile	Thu Mar 12 23:29:49 2015	(r381153)
+++ head/www/nginx/Makefile	Fri Mar 13 00:06:29 2015	(r381154)
@@ -655,7 +655,7 @@ CONFIGURE_ARGS+=--without-http_rewrite_m
 .endif
 
 .if ${PORT_OPTIONS:MPASSENGER}
-PASSENGER_VERSION=	4.0.59
+PASSENGER_VERSION=	5.0.2
 MASTER_SITES+=	http://s3.amazonaws.com/phusion-passenger/releases/:passenger
 DISTFILES+=	passenger-${PASSENGER_VERSION}.tar.gz:passenger
 CONFIGURE_ARGS+=--add-module=${WRKDIR}/passenger-${PASSENGER_VERSION}/ext/nginx

Modified: head/www/nginx/distinfo
==============================================================================
--- head/www/nginx/distinfo	Thu Mar 12 23:29:49 2015	(r381153)
+++ head/www/nginx/distinfo	Fri Mar 13 00:06:29 2015	(r381154)
@@ -74,8 +74,8 @@ SHA256 (modsecurity-2.9.0.tar.gz) = e2bb
 SIZE (modsecurity-2.9.0.tar.gz) = 4246467
 SHA256 (naxsi-core-0.50.tgz) = 7cece5f9d9c5df9e09af1e1023bc8e04bbbbe953e67461b893b8240e82ca52ef
 SIZE (naxsi-core-0.50.tgz) = 49809
-SHA256 (passenger-4.0.59.tar.gz) = 8b4756564a5133dc97425883d15d1ba1c9f6566c79e92794dc3e1f5b749238ae
-SIZE (passenger-4.0.59.tar.gz) = 4521206
+SHA256 (passenger-5.0.2.tar.gz) = 93aa200fdc47597c0dcbe32ed6c55af0751114d2c268e4d30ff7f3d7afdbebe8
+SIZE (passenger-5.0.2.tar.gz) = 5287262
 SHA256 (ngx_postgres-0.9.tar.gz) = aac0141e73b5c440927b7e040072f120b36e49deb2ff36ad00ff96a80622fbec
 SIZE (ngx_postgres-0.9.tar.gz) = 41022
 SHA256 (openresty-rds-csv-nginx-module-v0.05-0-g607e26b.tar.gz) = d5f14e45c6dc0d039ad52ae889775b5af1ff80a1c8edb69361c1581aca5d0b8f

Modified: head/www/nginx/files/extra-patch-passenger-build-nginx.rb
==============================================================================
--- head/www/nginx/files/extra-patch-passenger-build-nginx.rb	Thu Mar 12 23:29:49 2015	(r381153)
+++ head/www/nginx/files/extra-patch-passenger-build-nginx.rb	Fri Mar 13 00:06:29 2015	(r381154)
@@ -1,22 +1,18 @@
---- ../passenger-4.0.59/build/nginx.rb.orig	2013-10-27 02:00:00.000000000 +0400
-+++ ../passenger-4.0.59/build/nginx.rb	2014-06-02 05:55:12.000000000 +0400
+--- ../passenger-5.0.2/build/nginx.rb.orig	2013-10-27 02:00:00.000000000 +0400
++++ ../passenger-5.0.2/build/nginx.rb	2015-03-07 06:07:38.000000000 +0300
 @@ -32,6 +32,7 @@
  desc "Build Nginx support files"
  task :nginx => [
- 	:nginx_without_native_support,
-+	LIBBOOST_OXT,
- 	NATIVE_SUPPORT_TARGET
+   :nginx_without_native_support,
++  LIBBOOST_OXT,
+   NATIVE_SUPPORT_TARGET
  ].compact
  
-@@ -40,11 +41,6 @@
+@@ -40,7 +41,6 @@
  
  task :nginx_without_native_support => [
- 	auto_generated_sources,
--	AGENT_OUTPUT_DIR + 'PassengerHelperAgent',
--	AGENT_OUTPUT_DIR + 'PassengerWatchdog',
--	AGENT_OUTPUT_DIR + 'PassengerLoggingAgent',
--	AGENT_OUTPUT_DIR + 'SpawnPreparer',
--	AGENT_OUTPUT_DIR + 'TempDirToucher',
- 	COMMON_LIBRARY.only(*NGINX_LIBS_SELECTOR).link_objects
+   auto_generated_sources,
+-  AGENT_OUTPUT_DIR + AGENT_EXE,
+   COMMON_LIBRARY.only(*NGINX_LIBS_SELECTOR).link_objects
  ].flatten
  

Modified: head/www/rubygem-passenger/Makefile
==============================================================================
--- head/www/rubygem-passenger/Makefile	Thu Mar 12 23:29:49 2015	(r381153)
+++ head/www/rubygem-passenger/Makefile	Fri Mar 13 00:06:29 2015	(r381154)
@@ -2,7 +2,7 @@
 # $FreeBSD$
 
 PORTNAME=	passenger
-PORTVERSION=	4.0.59
+PORTVERSION=	5.0.2
 CATEGORIES=	www rubygems
 MASTER_SITES=	RG
 PKGNAMEPREFIX=	rubygem-
@@ -83,7 +83,7 @@ pre-patch:
 		s!#{PlatformInfo.debugging_cflags}!${CFLAGS}!g; \
 		s!-O2!!g; \
 		s! -feliminate-unused-debug-symbols -feliminate-unused-debug-types!!g; \
-		204,206s!true!false!' \
+		210,212s!true!false!' \
 		${WRKSRC}/build/basics.rb
 .if ${PORT_OPTIONS:NDEBUG}
 	@${REINPLACE_CMD} \
@@ -120,7 +120,7 @@ post-build:
 post-install:
 	( cd ${WRKSRC} && \
 	${COPYTREE_SHARE} buildout ${STAGEDIR}${PREFIX}/${GEMS_DIR}/${PORTNAME}-${PORTVERSION} )
-	${CHMOD} +x ${STAGEDIR}${PREFIX}/${GEMS_DIR}/${PORTNAME}-${PORTVERSION}/buildout/agents/*
+	${CHMOD} +x ${STAGEDIR}${PREFIX}/${GEMS_DIR}/${PORTNAME}-${PORTVERSION}/buildout/support-binaries/*
 .if ${PORT_OPTIONS:MSYMLINK}
 	${LN} -s ${PREFIX}/${GEM_LIB_DIR} ${STAGEDIR}${PREFIX}/${GEMS_DIR}/${PORTNAME}
 .endif

Modified: head/www/rubygem-passenger/distinfo
==============================================================================
--- head/www/rubygem-passenger/distinfo	Thu Mar 12 23:29:49 2015	(r381153)
+++ head/www/rubygem-passenger/distinfo	Fri Mar 13 00:06:29 2015	(r381154)
@@ -1,2 +1,2 @@
-SHA256 (rubygem/passenger-4.0.59.gem) = c42f92d31ef19ebb093db676d549209c8279c4ef63aff373ebdf44072bf12668
-SIZE (rubygem/passenger-4.0.59.gem) = 4535296
+SHA256 (rubygem/passenger-5.0.2.gem) = 928e29155d6b256cf956478c9a1e63fe92ad54899bc4c965870b7f8b88a4bf4f
+SIZE (rubygem/passenger-5.0.2.gem) = 5297664

Added: head/www/rubygem-passenger/files/patch-ext-common-ServerKit-AcceptLoadBalancer.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/www/rubygem-passenger/files/patch-ext-common-ServerKit-AcceptLoadBalancer.h	Fri Mar 13 00:06:29 2015	(r381154)
@@ -0,0 +1,194 @@
+--- ext/common/ServerKit/AcceptLoadBalancer.h.orig	2015-03-13 02:38:22.000000000 +0300
++++ ext/common/ServerKit/AcceptLoadBalancer.h	2015-03-13 02:39:45.000000000 +0300
+@@ -1,6 +1,6 @@
+ /*
+  *  Phusion Passenger - https://www.phusionpassenger.com/
+- *  Copyright (c) 2014 Phusion
++ *  Copyright (c) 2014-2015 Phusion
+  *
+  *  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
+  *
+@@ -26,9 +26,12 @@
+ #define _PASSENGER_SERVER_KIT_ACCEPT_LOAD_BALANCER_H_
+ 
+ #include <boost/bind.hpp>
++#include <boost/cstdint.hpp>
++#include <boost/config.hpp>
+ #include <oxt/thread.hpp>
+ #include <oxt/macros.hpp>
+ #include <vector>
++#include <limits>
+ #include <cassert>
+ #include <cerrno>
+ 
+@@ -41,6 +44,7 @@
+ 
+ #include <Constants.h>
+ #include <Logging.h>
++#include <Utils.h>
+ #include <Utils/IOUtils.h>
+ 
+ namespace Passenger {
+@@ -50,6 +54,32 @@
+ using namespace boost;
+ 
+ 
++/**
++ * Listens for client connections and load balances them to multiple
++ * Server objects in a round-robin manner.
++ *
++ * Normally, the Server class listens for client connections directly.
++ * But this is inefficient in multithreaded situations where you are
++ * running one Server and event loop per CPU core, that all happen to
++ * listen on the same server socket. This is because every time a client
++ * connects, all threads wake up, but only one thread will succeed in
++ * accept()ing the client.
++ *
++ * Furthermore, it can also be very easy for threads to become
++ * unbalanced. If a burst of clients connect to the server socket,
++ * then it is very likely that a single Server accepts all of
++ * those clients. This can result in situations where, for example,
++ * thread 1 has 40 clients and thread 2 has only 3.
++ *
++ * The AcceptLoadBalancer solves this problem by being the sole entity
++ * that listens on the server socket. All client sockets that it
++ * accepts are distributed to all registered Server objects, in a
++ * round-robin manner.
++ *
++ * Inside the "PassengerAgent server", we activate AcceptLoadBalancer
++ * only if `server_threads > 1`, which is often the case because
++ * `server_threads` defaults to the number of CPU cores.
++ */
+ template<typename Server>
+ class AcceptLoadBalancer {
+ private:
+@@ -59,15 +89,24 @@
+ 	struct pollfd pollers[1 + SERVER_KIT_MAX_SERVER_ENDPOINTS];
+ 	int newClients[ACCEPT_BURST_COUNT];
+ 
+-	unsigned int nEndpoints: 2;
+-	bool accept4Available: 1;
+-	bool quit: 1;
+-	unsigned int newClientCount: 4;
++	unsigned int nEndpoints;
++	boost::uint8_t newClientCount;
++	boost::uint8_t nextServer;
++	bool accept4Available;
++	bool quit;
+ 
+-	unsigned int nextServer;
+ 	int exitPipe[2];
+ 	oxt::thread *thread;
+ 
++	#if __cplusplus >= 199711L && !defined(BOOST_NO_STATIC_ASSERT)
++		static_assert(std::numeric_limits<typeof(nEndpoints)>::max()
++			>= SERVER_KIT_MAX_SERVER_ENDPOINTS,
++			"nEndpoints's type is too small to accomodate for SERVER_KIT_MAX_SERVER_ENDPOINTS");
++		static_assert(std::numeric_limits<typeof(newClientCount)>::max()
++			>= ACCEPT_BURST_COUNT,
++			"newClientCount's type is too small to accomodate for ACCEPT_BURST_COUNT");
++	#endif
++
+ 	void pollAllEndpoints() {
+ 		pollers[0].fd = exitPipe[0];
+ 		pollers[0].events = POLLIN;
+@@ -75,18 +114,28 @@
+ 			pollers[i + 1].fd = endpoints[i];
+ 			pollers[i + 1].events = POLLIN;
+ 		}
+-		if (poll(pollers, nEndpoints + 1, -1) == -1) {
++
++		int ret;
++		do {
++			ret = poll(pollers, nEndpoints + 1, -1) == -1;
++		} while (ret == -1 && pollErrnoShouldRetry(errno));
++		if (ret == -1) {
+ 			int e = errno;
+ 			throw SystemException("poll() failed", e);
+ 		}
+ 	}
+ 
++	bool pollErrnoShouldRetry(int e) const {
++		return e == EAGAIN
++			|| e == EWOULDBLOCK
++			|| e == EINTR;
++	}
++
+ 	bool acceptNewClients(int endpoint) {
+-		unsigned int i;
+ 		bool error = false;
+ 		int fd, errcode = 0;
+ 
+-		for (i = 0; i < ACCEPT_BURST_COUNT; i++) {
++		while (newClientCount < ACCEPT_BURST_COUNT) {
+ 			fd = acceptNonBlockingSocket(endpoint);
+ 			if (fd == -1) {
+ 				error = true;
+@@ -94,11 +143,11 @@
+ 				break;
+ 			}
+ 
+-			newClients[i] = fd;
++			P_TRACE(2, "Accepted client file descriptor: " << fd);
++			newClients[newClientCount] = fd;
++			newClientCount++;
+ 		}
+ 
+-		newClientCount = i;
+-
+ 		if (error && errcode != EAGAIN && errcode != EWOULDBLOCK) {
+ 			P_ERROR("Cannot accept client: " << getErrorDesc(errcode) <<
+ 				" (errno=" << errcode << "). " <<
+@@ -121,6 +170,8 @@
+ 
+ 		for (i = 0; i < newClientCount; i++) {
+ 			ServerKit::Context *ctx = servers[nextServer]->getContext();
++			P_TRACE(2, "Feeding client to server thread " << nextServer <<
++				": file descriptor " << newClients[i]);
+ 			ctx->libev->runLater(boost::bind(feedNewClient, servers[nextServer],
+ 				newClients[i]));
+ 			nextServer = (nextServer + 1) % servers.size();
+@@ -182,13 +233,19 @@
+ 				quit = true;
+ 				break;
+ 			}
+-			for (unsigned i = 0; i < nEndpoints; i++) {
++
++			unsigned int i = 0;
++			newClientCount = 0;
++
++			while (newClientCount < ACCEPT_BURST_COUNT && i < nEndpoints) {
+ 				if (pollers[i + 1].revents & POLLIN) {
+ 					if (!acceptNewClients(endpoints[i])) {
+ 						break;
+ 					}
+ 				}
++				i++;
+ 			}
++
+ 			distributeNewClients();
+ 		}
+ 	}
+@@ -198,10 +255,10 @@
+ 
+ 	AcceptLoadBalancer()
+ 		: nEndpoints(0),
+-		  accept4Available(true),
+-		  quit(false),
+ 		  newClientCount(0),
+ 		  nextServer(0),
++		  accept4Available(true),
++		  quit(false),
+ 		  thread(NULL)
+ 	{
+ 		if (pipe(exitPipe) == -1) {
+@@ -248,7 +305,8 @@
+ 	}
+ 
+ 	void start() {
+-		thread = new oxt::thread(boost::bind(&AcceptLoadBalancer<Server>::mainLoop, this),
++		boost::function<void ()> func = boost::bind(&AcceptLoadBalancer<Server>::mainLoop, this);
++		thread = new oxt::thread(boost::bind(runAndPrintExceptions, func, true),
+ 			"Load balancer");
+ 	}
+ 


More information about the svn-ports-head mailing list