Files
iptvnator/libs/shared/host-health/src/lib/socket-connect-observer.ts
T
4grayandClaude Fable 5.1 01c423ac43 fix(portals): stop treating a slow panel as a dead host; IPv4 fallback budget in Electron (#1621)
* fix(portals): apply the IPv6->IPv4 fallback budget in the Electron process

The 2500 ms happy-eyeballs attempt timeout from #1404 only ever ran in the
web backend. The Electron main process and its playlist-refresh and EPG
workers kept Node's 250 ms default, so a dual-stack panel hostname behind a
VPN or a slow link failed every connection attempt in a row and tripped the
host connectivity guard. The module now lives in `@iptvnator/shared/host-health`
and every Node isolate that opens connections applies it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(portals): stop treating a slow panel as a dead one in the host guard

axios raises the same ECONNABORTED whether the SYN went unanswered or the
panel accepted the connection and then thought for longer than the request
budget. Two such timeouts opened the breaker and every request to the panel
was refused for 30 s with "portal is not responding" — the shape behind the
"connection keeps dropping" reports on 0.23 and nightly.

Both transports now report whether the TCP connection was established
(`onConnect`: Electron through a per-request observed agent instead of the
shared keep-alive globalAgent, the web backend through the transport that owns
the ClientRequest), and `classifyHostRequestFailure(error, { connected })`
downgrades a host-level code observed after the handshake to inconclusive.
Redirect attribution keeps precedence. A host that never accepts the
connection trips the guard exactly as before.

The Xtream mock gains a `silent:silent` scenario whose detail actions accept
and never answer, plus a real-socket regression spec for the guard.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(portals): let an accepted connection clear the host-failure streak

Review finding: an unanswered SYN, then an accepted-but-slow timeout, then
another unanswered SYN still reached the two-failure threshold, because the
middle request was merely not counted. An accepted TCP connection is the
reachability the guard measures, so it now reads as `responded` and clears
the streak like an HTTP response would. Regression coverage for the mixed
sequence on one flapping loopback origin (Electron) and through the proxy
route (web backend).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(portals): credit an accepted connection when it happens, not when the request settles

Review findings. A request that connected and then hung for 30 s cleared,
on its eventual timeout, the failures later requests had recorded while it
waited — reopening a host that had just died on evidence older than theirs.
The connect hook now reports the connection the moment it fires through a
new `HostConnectivityGuard.reportConnected`, which clears the failure streak
but closes no open or half-open breaker (the trial keeps its slot until it
settles), and the settled timeout is inconclusive.

Electron also skips the socket observer while an environment proxy
(`http_proxy` / `https_proxy` / `all_proxy`) applies to the request: through
a proxy the socket connects to the proxy, whose handshake proves nothing
about the portal, so those requests keep the pre-observer behaviour.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(portals): decide the proxy exemption with axios' own resolution

Review findings. The hand-rolled environment check ignored `no_proxy`, so a
LAN portal exempted from the proxy lost its connect observer and slow
requests to it still tripped the breaker; it also read the variables with
`??`, letting an empty lowercase one mask a populated uppercase one that
axios would honour. The decision now calls `proxy-from-env`'s
`getProxyForUrl`, the same pinned package axios' http adapter uses,
declared as a direct dependency so the packaged app carries it.

The validated-axios spec clears and restores every proxy variable around each
case, so a runner that exports a proxy cannot change what the cases prove.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-19 14:50:50 +02:00

60 lines
2.1 KiB
TypeScript

import type { ClientRequest } from 'node:http';
import type { Socket } from 'node:net';
/**
* The one method every Node HTTP/HTTPS agent funnels a request through, pooled
* socket or fresh connection alike. `@types/node` leaves it undeclared, so the
* shape is spelled out here rather than reached through `any`.
*/
interface RequestAcceptingAgent {
addRequest(request: ClientRequest, ...rest: unknown[]): void;
}
/**
* Reports the moment the TCP connection carrying `request` is known to be
* established. A keep-alive agent can hand the request a socket whose
* `connect` event fired long ago, so a socket that is no longer connecting
* counts right away: the host DID accept a connection, which is the fact
* wanted. A socket that never connects (refused, unresolvable, unanswered
* SYN) never reports.
*/
export function observeRequestSocketConnect(
request: ClientRequest,
onConnect: () => void
): void {
request.once('socket', (socket: Socket) => {
if (socket.connecting) {
socket.once('connect', onConnect);
} else {
onConnect();
}
});
}
/**
* {@link observeRequestSocketConnect} for a transport that never hands its
* `ClientRequest` back (axios' default adapter): hooking the agent's
* `addRequest` sees every request the agent takes, pooled or fresh.
*
* Mutates and returns the same agent so it can be passed straight into an
* axios config. Never install this on a shared agent such as
* `http.globalAgent`: the callback belongs to one request, and a second
* install on the same agent stacks on the first rather than replacing it.
*/
export function observeAgentSocketConnections<TAgent extends object>(
agent: TAgent,
onConnect: () => void
): TAgent {
const target = agent as unknown as RequestAcceptingAgent;
const original = target.addRequest;
target.addRequest = function observedAddRequest(
this: RequestAcceptingAgent,
request: ClientRequest,
...rest: unknown[]
): void {
observeRequestSocketConnect(request, onConnect);
return original.call(this, request, ...rest);
};
return agent;
}