node.py: raise RLIMIT_NOFILE on bsd/cygwin to more than 256

This commit is contained in:
Brian Warner 2007-11-15 22:53:55 -07:00
parent 8a2736480a
commit c6df66d736

View File

@ -1,5 +1,5 @@
import datetime, os.path, re, types
import datetime, os.path, re, types, resource
from base64 import b32decode, b32encode
import twisted
@ -75,6 +75,52 @@ class Node(service.MultiService):
" twisted: %s, zfec: %s"
% (allmydata.__version__, foolscap.__version__,
twisted.__version__, zfec.__version__,))
self.increase_rlimits()
def increase_rlimits(self):
# We'd like to raise our soft resource.RLIMIT_NOFILE, since certain
# systems (OS-X, probably solaris) start with a relatively low limit
# (256), and some unit tests want to open up more sockets than this.
# Most linux systems start with both hard and soft limits at 1024,
# which is plenty.
# unfortunately the values to pass to setrlimit() vary widely from
# one system to another. OS-X reports (256, HUGE), but the real hard
# limit is 10240, and accepts (-1,-1) to mean raise it to the
# maximum. Cygwin reports (256, -1), then ignores a request of
# (-1,-1): instead you have to guess at the hard limit (it appears to
# be 3200), so using (3200,-1) seems to work. Linux reports a
# sensible (1024,1024), then rejects (-1,-1) as trying to raise the
# maximum limit, so you could set it to (1024,1024) but you might as
# well leave it alone.
try:
current = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
except AttributeError:
# we're probably missing RLIMIT_NOFILE, maybe this is windows
return
if current >= 1024:
# good enough, leave it alone
return
try:
# this one works on OS-X (bsd), and gives us 10240, but
# it doesn't work on linux (on which both the hard and
# soft limits are set to 1024 by default).
resource.setrlimit(resource.RLIMIT_NOFILE, (-1,-1))
new = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
if new == current:
# probably cygwin, which ignores -1. Use a real value.
resource.setrlimit(resource.RLIMIT_NOFILE, (3200,-1))
except ValueError:
self.log("unable to set RLIMIT_NOFILE: current value %s"
% (resource.getrlimit(resource.RLIMIT_NOFILE),))
except:
# who knows what. It isn't very important, so log it and continue
log.err()
def get_config(self, name, mode="r", required=False):
"""Get the (string) contents of a config file, or None if the file