mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-22 23:59:41 +08:00
Change waiting loops to semaphores for child counters
This commit is contained in:
1 parent
d4eaf5acc8
commit
77f8870c5d
9 files changed
+288
-68
No files matched your search
+4
-3
@@ -69,7 +69,7 @@ means to stand on its own should start with one - otherwise an earlier
|
||||
|
||||
## What is not covered yet
|
||||
|
||||
41 of the 112 configuration commands appear in a test, and the count says
|
||||
42 of the 112 configuration commands appear in a test, and the count says
|
||||
nothing about service options: the IPv6 case, for instance, exercises -4,
|
||||
-6, -46, -64 and -i without adding a command to it. What follows is
|
||||
roughly the order worth working through: how much of the product a gap
|
||||
@@ -78,12 +78,13 @@ covers, and how much of a fixture it needs.
|
||||
### Traffic limits and accounting
|
||||
|
||||
`bandlimin` `bandlimout` `nobandlimin` `nobandlimout` `connlim` `noconnlim`
|
||||
`countin` `countout` `countall` and the `no*` forms, `maxconn`.
|
||||
`countin` `countout` `countall` and the `no*` forms.
|
||||
|
||||
Cheap and worth doing first: `data?size=` and a stopwatch measure a
|
||||
bandwidth limit, and the admin counters page already shows what a counter
|
||||
holds. `countin` appears in a configuration today but nothing checks that it
|
||||
counts. `connlim` and `maxconn` need concurrent connections.
|
||||
counts. `connlim` needs concurrent connections, held open the way the
|
||||
`maxconn` case holds them.
|
||||
|
||||
### The mail proxies
|
||||
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
"""maxconn: a service serves at most that many connections at once, and a
|
||||
connection over the limit waits for one of them to finish."""
|
||||
|
||||
import os
|
||||
import signal
|
||||
import socket
|
||||
import time
|
||||
|
||||
|
||||
def run(t):
|
||||
# The origin runs in a process of its own, so the limit only applies to
|
||||
# the mapper in front of it.
|
||||
origin = t.free_port()
|
||||
t.start("maxconn_origin", f"""
|
||||
auth iponly
|
||||
allow *
|
||||
http echo * /echo**
|
||||
httpsrv -p{origin}
|
||||
""", ports=[origin])
|
||||
|
||||
mapped = t.free_port()
|
||||
server = t.start("maxconn", f"""
|
||||
log
|
||||
maxconn 2
|
||||
auth iponly
|
||||
allow *
|
||||
tcppm {mapped} 127.0.0.1 {origin}
|
||||
""", ports=[mapped])
|
||||
|
||||
def connect():
|
||||
sock = socket.create_connection(("127.0.0.1", mapped), 5)
|
||||
opened.append(sock)
|
||||
return sock
|
||||
|
||||
def served(sock, timeout):
|
||||
"""Ask over a kept alive connection; True once the whole reply is in,
|
||||
so the connection is ready for the next request."""
|
||||
sock.sendall(b"GET /echo HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n")
|
||||
sock.settimeout(timeout)
|
||||
data = b""
|
||||
try:
|
||||
while b"\r\n\r\n" not in data:
|
||||
chunk = sock.recv(4096)
|
||||
if not chunk:
|
||||
return False
|
||||
data += chunk
|
||||
head, body = data.split(b"\r\n\r\n", 1)
|
||||
length = 0
|
||||
for line in head.split(b"\r\n")[1:]:
|
||||
name, _, value = line.partition(b":")
|
||||
if name.strip().lower() == b"content-length":
|
||||
length = int(value)
|
||||
while len(body) < length:
|
||||
chunk = sock.recv(4096)
|
||||
if not chunk:
|
||||
return False
|
||||
body += chunk
|
||||
return head.startswith(b"HTTP/1.1 200")
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
opened = []
|
||||
try:
|
||||
# the probe that saw the port open holds a slot until its session
|
||||
# ends, so wait for both slots to be free before taking them
|
||||
deadline = time.time() + 5
|
||||
while True:
|
||||
first, second = connect(), connect()
|
||||
both = served(first, 1) and served(second, 1)
|
||||
if both or time.time() > deadline:
|
||||
break
|
||||
first.close()
|
||||
second.close()
|
||||
t.eq(True, both, "two connections are served under maxconn 2")
|
||||
|
||||
third = connect()
|
||||
t.eq(False, served(third, 1.5),
|
||||
"a third connection waits while both slots are held")
|
||||
t.contains(t.wait_output(server, "too many connected clients"),
|
||||
"too many connected clients",
|
||||
"a service at its limit says so in the log")
|
||||
|
||||
first.close()
|
||||
t.eq(True, served(third, 5),
|
||||
"the waiting connection is served once a slot is freed")
|
||||
t.eq(True, served(second, 5),
|
||||
"a connection holding a slot keeps being served")
|
||||
|
||||
if os.name == "nt":
|
||||
t.skip("reload while at the limit (no SIGUSR1 on Windows)")
|
||||
else:
|
||||
waiting = connect()
|
||||
t.eq(False, served(waiting, 1),
|
||||
"the limit holds again with two connections open")
|
||||
server.proc.send_signal(signal.SIGUSR1)
|
||||
# A connection made the instant the signal arrives can land on
|
||||
# the listener being replaced and be reset; give the new service
|
||||
# the moment it needs.
|
||||
time.sleep(1.5)
|
||||
fresh = connect()
|
||||
t.eq(True, served(fresh, 5),
|
||||
"a reload while the limit is reached starts a service with "
|
||||
"its slots free")
|
||||
t.eq(None, server.proc.poll(), "the process survives the reload")
|
||||
finally:
|
||||
for sock in opened:
|
||||
sock.close()
|
||||
Reference in new issue
Block a user