changeset 139:2281316c3423

Added sockTimeout option for prefork server
author Velko Ivanov <vivanov@ivanov-nest.com>
date Thu, 23 Feb 2012 15:13:09 +0200
parents 8d52f88effa3
children cc23b715b120
files flup/server/preforkserver.py
diffstat 1 files changed, 13 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/flup/server/preforkserver.py	Mon Jun 06 18:29:07 2011 -0700
     1.2 +++ b/flup/server/preforkserver.py	Thu Feb 23 15:13:09 2012 +0200
     1.3 @@ -83,13 +83,21 @@
     1.4      jobClass should have a run() method (taking no arguments) that does
     1.5      the actual work. When run() returns, the request is considered
     1.6      complete and the child process moves to idle state.
     1.7 +    
     1.8 +    -- added by Velko Ivanov
     1.9 +    sockTimeout is a timeout in seconds for the server's sockets. This
    1.10 +    should be 0 (pure non-blocking mode, default) or more (timeout mode).
    1.11 +    Fractional values (float) accepted. Setting this could help limit the
    1.12 +    damage in situations of bad connectivity.
    1.13 +    
    1.14      """
    1.15      def __init__(self, minSpare=1, maxSpare=5, maxChildren=50,
    1.16 -                 maxRequests=0, jobClass=None, jobArgs=()):
    1.17 +                 maxRequests=0, sockTimeout=0, jobClass=None, jobArgs=()):
    1.18          self._minSpare = minSpare
    1.19          self._maxSpare = maxSpare
    1.20          self._maxChildren = max(maxSpare, maxChildren)
    1.21          self._maxRequests = maxRequests
    1.22 +        self._sockTimeout = sockTimeout
    1.23          self._jobClass = jobClass
    1.24          self._jobArgs = jobArgs
    1.25  
    1.26 @@ -106,6 +114,9 @@
    1.27              raise ValueError("minSpare must be at least 1!")
    1.28          if maxSpare < minSpare:
    1.29              raise ValueError("maxSpare must be greater than, or equal to, minSpare!")
    1.30 +        t = type(sockTimeout)
    1.31 +        if (t != int and t != float) or (sockTimeout < 0):
    1.32 +            raise ValueError("sockTimeout must be an int or float, greater than or equal to 0")
    1.33  
    1.34      def run(self, sock):
    1.35          """
    1.36 @@ -119,7 +130,7 @@
    1.37          self._installSignalHandlers()
    1.38  
    1.39          # Don't want operations on main socket to block.
    1.40 -        sock.setblocking(0)
    1.41 +        sock.setblocking(self._sockTimeout)
    1.42  
    1.43          # Set close-on-exec
    1.44          setCloseOnExec(sock)