Commit faee30d4 authored by Amos Latteier's avatar Amos Latteier

Added logic the the http server to keep it from accepting new connections when...

Added logic the the http server to keep it from accepting new connections when the server is over loaded. This should keep the server from breaking by doing a select with too many descriptors, and should also help keep the server from running out of descriptors.
parent 1d113a5d
......@@ -117,7 +117,7 @@ from HTTPResponse import make_response
from ZPublisher.HTTPRequest import HTTPRequest
from medusa.http_server import http_server, http_channel
from medusa import counter, producers, asyncore
from medusa import counter, producers, asyncore, max_sockets
from medusa.default_handler import split_path, unquote, get_header
CONTENT_LENGTH = regex.compile('Content-Length: \([0-9]+\)',regex.casefold)
......@@ -337,5 +337,12 @@ class zhttp_server(http_server):
"http server"
SERVER_IDENT='Zope/%s ZServer/%s' % (ZOPE_VERSION,ZSERVER_VERSION)
CONNECTION_LIMIT=max_sockets.max_select_sockets()
channel_class = zhttp_channel
def handle_accept(self):
if len(asyncore.socket_map.keys()) >= self.CONNECTION_LIMIT:
return
http_server.handle_accept(self)
# -*- Mode: Python; tab-width: 4 -*-
import socket
import select
# several factors here we might want to test:
# 1) max we can create
# 2) max we can bind
# 3) max we can listen on
# 4) max we can connect
def max_server_sockets():
sl = []
while 1:
try:
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
s.bind (('',0))
s.listen(5)
sl.append (s)
except:
break
num = len(sl) - 1
for s in sl:
s.close()
del sl
return num
def max_client_sockets():
# make a server socket
server = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
server.bind (('', 9999))
server.listen (5)
sl = []
while 1:
try:
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
s.connect (('', 9999))
conn, addr = server.accept()
sl.append ((s,conn))
except:
break
num = len(sl) - 1
for s,c in sl:
s.close()
c.close()
del sl
return num
def max_select_sockets():
sl = []
while 1:
try:
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
s.bind (('',0))
s.listen(5)
sl.append (s)
select.select(sl,[],[],0)
except:
break
num = len(sl) - 1
for s in sl:
s.close()
del sl
return num
......@@ -117,7 +117,7 @@ from HTTPResponse import make_response
from ZPublisher.HTTPRequest import HTTPRequest
from medusa.http_server import http_server, http_channel
from medusa import counter, producers, asyncore
from medusa import counter, producers, asyncore, max_sockets
from medusa.default_handler import split_path, unquote, get_header
CONTENT_LENGTH = regex.compile('Content-Length: \([0-9]+\)',regex.casefold)
......@@ -337,5 +337,12 @@ class zhttp_server(http_server):
"http server"
SERVER_IDENT='Zope/%s ZServer/%s' % (ZOPE_VERSION,ZSERVER_VERSION)
CONNECTION_LIMIT=max_sockets.max_select_sockets()
channel_class = zhttp_channel
def handle_accept(self):
if len(asyncore.socket_map.keys()) >= self.CONNECTION_LIMIT:
return
http_server.handle_accept(self)
# -*- Mode: Python; tab-width: 4 -*-
import socket
import select
# several factors here we might want to test:
# 1) max we can create
# 2) max we can bind
# 3) max we can listen on
# 4) max we can connect
def max_server_sockets():
sl = []
while 1:
try:
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
s.bind (('',0))
s.listen(5)
sl.append (s)
except:
break
num = len(sl) - 1
for s in sl:
s.close()
del sl
return num
def max_client_sockets():
# make a server socket
server = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
server.bind (('', 9999))
server.listen (5)
sl = []
while 1:
try:
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
s.connect (('', 9999))
conn, addr = server.accept()
sl.append ((s,conn))
except:
break
num = len(sl) - 1
for s,c in sl:
s.close()
c.close()
del sl
return num
def max_select_sockets():
sl = []
while 1:
try:
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
s.bind (('',0))
s.listen(5)
sl.append (s)
select.select(sl,[],[],0)
except:
break
num = len(sl) - 1
for s in sl:
s.close()
del sl
return num
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment