server.py: windows doesn't even have the 'resource' module
I thought it might exist, but be empty. But it doesn't even exist on windows, so we must guard against an ImportError.
This commit is contained in:
parent
d44c7d2c1a
commit
b9d1d11b03
|
@ -2,7 +2,11 @@
|
|||
# a str on Python 2
|
||||
from __future__ import print_function
|
||||
import os, time, json
|
||||
import resource
|
||||
try:
|
||||
# 'resource' is unix-only
|
||||
from resource import getrlimit, setrlimit, RLIMIT_NOFILE
|
||||
except ImportError: # pragma: nocover
|
||||
getrlimit, setrlimit, RLIMIT_NOFILE = None, None, None # pragma: nocover
|
||||
from twisted.python import log
|
||||
from twisted.internet import reactor, endpoints
|
||||
from twisted.application import service, internet
|
||||
|
@ -105,11 +109,10 @@ class RelayServer(service.MultiService):
|
|||
self._transit_service = transit_service
|
||||
|
||||
def increase_rlimits(self):
|
||||
try:
|
||||
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
|
||||
except AttributeError:
|
||||
log.msg("AttributeError during getrlimit, leaving it alone")
|
||||
if getrlimit is None:
|
||||
log.msg("unable to import 'resource', leaving rlimit alone")
|
||||
return
|
||||
soft, hard = getrlimit(RLIMIT_NOFILE)
|
||||
if soft >= 10000:
|
||||
log.msg("RLIMIT_NOFILE.soft was %d, leaving it alone" % soft)
|
||||
return
|
||||
|
@ -122,7 +125,7 @@ class RelayServer(service.MultiService):
|
|||
log.msg("changing RLIMIT_NOFILE from (%s,%s) to (%s,%s)" %
|
||||
(soft, hard, newlimit, hard))
|
||||
try:
|
||||
resource.setrlimit(resource.RLIMIT_NOFILE, (newlimit, hard))
|
||||
setrlimit(RLIMIT_NOFILE, (newlimit, hard))
|
||||
log.msg("setrlimit successful")
|
||||
return
|
||||
except ValueError as e:
|
||||
|
|
|
@ -34,30 +34,26 @@ class RLimits(unittest.TestCase):
|
|||
ep = endpoints.TCP4ServerEndpoint(None, 0)
|
||||
with patch_s("endpoints.serverFromString", return_value=ep):
|
||||
s = server.RelayServer("fake", None, None)
|
||||
noattrs = object()
|
||||
fakelog = []
|
||||
def checklog(*expected):
|
||||
self.assertEqual(fakelog, list(expected))
|
||||
fakelog[:] = []
|
||||
NF = "NOFILE"
|
||||
mock_NF = patch_s("resource.RLIMIT_NOFILE", NF)
|
||||
mock_NF = patch_s("RLIMIT_NOFILE", NF)
|
||||
|
||||
with patch_s("log.msg", fakelog.append):
|
||||
with patch_s("resource", noattrs):
|
||||
with patch_s("getrlimit", None):
|
||||
s.increase_rlimits()
|
||||
checklog("AttributeError during getrlimit, leaving it alone")
|
||||
checklog("unable to import 'resource', leaving rlimit alone")
|
||||
|
||||
with mock_NF:
|
||||
with patch_s("resource.getrlimit",
|
||||
return_value=(20000, 30000)) as gr:
|
||||
with patch_s("getrlimit", return_value=(20000, 30000)) as gr:
|
||||
s.increase_rlimits()
|
||||
self.assertEqual(gr.mock_calls, [mock.call(NF)])
|
||||
checklog("RLIMIT_NOFILE.soft was 20000, leaving it alone")
|
||||
|
||||
with patch_s("resource.getrlimit",
|
||||
return_value=(10, 30000)) as gr:
|
||||
with patch_s("resource.setrlimit",
|
||||
side_effect=TypeError("other")):
|
||||
with patch_s("getrlimit", return_value=(10, 30000)) as gr:
|
||||
with patch_s("setrlimit", side_effect=TypeError("other")):
|
||||
with patch_s("log.err") as err:
|
||||
s.increase_rlimits()
|
||||
self.assertEqual(err.mock_calls, [mock.call()])
|
||||
|
@ -82,8 +78,7 @@ class RLimits(unittest.TestCase):
|
|||
else:
|
||||
expected.append("unable to change rlimit, leaving it alone")
|
||||
|
||||
with patch_s("resource.setrlimit",
|
||||
side_effect=setrlimit) as sr:
|
||||
with patch_s("setrlimit", side_effect=setrlimit) as sr:
|
||||
s.increase_rlimits()
|
||||
self.assertEqual(sr.mock_calls, calls)
|
||||
checklog(*expected)
|
||||
|
|
Loading…
Reference in New Issue
Block a user