feat(cd): out_service/cd.py 支持任意 host:port, 去掉 IP 白名单

socket 槽位和 per-host 锁改为首次访问时懒注册, 用 double-checked locking
保证热路径无竞争。不再硬编码 4 个采集器地址。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
caoqianming 2026-05-27 14:14:12 +08:00
parent 86a8110a5e
commit 7ef9763a50
1 changed files with 16 additions and 12 deletions

View File

@ -4,14 +4,21 @@ from urllib.parse import urlparse, parse_qs
import socket
import threading
sc_all = {
"192.168.1.220_6000": None,
"192.168.1.235_6000": None,
"192.168.1.225_6000": None,
"192.168.1.230_6000": None,
}
sc_all = {}
sc_locks = {}
registry_lock = threading.Lock()
sc_locks = {addr: threading.Lock() for addr in sc_all}
def get_lock(addr):
lock = sc_locks.get(addr)
if lock is None:
with registry_lock:
lock = sc_locks.get(addr)
if lock is None:
lock = threading.Lock()
sc_locks[addr] = lock
sc_all.setdefault(addr, None)
return lock
def get_checksum(body_msg):
return sum(body_msg) & 0xFF
@ -106,13 +113,10 @@ class JSONRequestHandler(BaseHTTPRequestHandler):
host = query_params.get('host', ['127.0.0.1'])[0]
port = query_params.get('port', ['6000'])[0]
addr = f'{host}_{port}'
if addr not in sc_all:
self.error(f'{addr} 未找到')
return
lock = get_lock(addr)
def request_once():
with sc_locks[addr]:
with lock:
sc = sc_all[addr]
try:
if sc is None: