junction: heartbeats while the model thinks, not just while it queues
A streaming caller already heard ': junction: waiting for a free slot' while queued. Once it had a slot and the upstream went quiet, the stream was silent again — the silence a proxy with a 60 s read timeout kills. Every 10 s of upstream silence now produces ': junction: model is still working (Ns)'; the reader's poll is woken by the connection's heartbeat_s instead of waiting out idle_timeout. Bounded by queue_timeout and timeout (a streamer then gets an error event + [DONE]), and a failed comment write means the client is gone, so the router stops waiting. tests: a stub that thinks 13 s proves heartbeats appear, the answer still arrives, and a non-streaming caller still receives nothing injected. 49 checks PASS.
This commit is contained in:
1 parent
c34082f51d
commit
e6db2f61af
6 files changed
+85
-13
No files matched your search
@@ -26,9 +26,17 @@ serves it — and what to do when none of them wants to.
|
||||
requests at a time, the rest wait in line instead of being refused. A waiting
|
||||
*streaming* caller is told why it waits — as SSE comments (`: junction: waiting for
|
||||
a free slot (2 ahead of you)`), which every conformant parser ignores and a human
|
||||
watching `curl` sees. As soon as the real answer starts, the comments simply stop;
|
||||
nothing is buffered, nothing is rewritten. A non-streaming caller gets nothing
|
||||
injected at all and keeps its real status code.
|
||||
watching `curl` sees. The same heartbeat keeps a *thinking* model's stream alive —
|
||||
every 10 s of upstream silence produces `: junction: model is still working (12s)`,
|
||||
so a proxy with a 60 s read timeout does not kill a stream that is perfectly alive.
|
||||
As soon as real data arrives the comments stop; nothing is buffered, nothing is
|
||||
rewritten. A non-streaming caller gets nothing injected at all and keeps its real
|
||||
status code.
|
||||
|
||||
This is bounded, not endless: `queue_timeout` caps the wait for a slot, `timeout`
|
||||
caps the upstream request, and when either expires a streamer receives an error
|
||||
event plus `data: [DONE]`. If the client vanishes, the next comment write fails and
|
||||
the router stops waiting for a model nobody is listening to.
|
||||
- **TLS both ways.** `https://` upstreams (SNI, optional chain + hostname
|
||||
verification, custom CA) and an optional TLS listener — no stunnel needed.
|
||||
- **Chunked transfer coding.** Chunked request bodies are decoded; chunked responses
|
||||
@@ -215,7 +223,7 @@ failover, hook retry, `still_working`, block rules, catch-all, model rewrite,
|
||||
`no_matching_rule`, config errors, chunked request decoding (byte-exact body),
|
||||
cache miss/hit/flush/bypass, header passthrough and injection, TLS listener, https
|
||||
upstream, and that certificate verification actually refuses a self-signed peer.
|
||||
Pass = `PASS` (45 checks), including two that would have caught the two mistakes this
|
||||
Pass = `PASS` (49 checks), including two that would have caught the two mistakes this
|
||||
design invites: a serial stub making a concurrent router look serialised, and a
|
||||
"waiting" comment leaking into a caller that never asked for a stream.
|
||||
|
||||
|
||||
+2
-1
@@ -14,9 +14,10 @@ typedef struct {
|
||||
int fd;
|
||||
void *tls; /* SSL * when TLS, NULL otherwise */
|
||||
const char *role; /* "client" or "upstream" — used for progress logging */
|
||||
int heartbeat_s; /* >0: the reader may be silent this long before a heartbeat */
|
||||
} j_conn;
|
||||
|
||||
#define J_CONN_INIT { -1, NULL, NULL }
|
||||
#define J_CONN_INIT { -1, NULL, NULL, 0 }
|
||||
|
||||
/* Client side. use_tls selects TLS; verify 1 = check chain + hostname. */
|
||||
int j_conn_open(j_conn *c, const char *host, const char *port, int use_tls, int verify,
|
||||
|
||||
+6
-3
@@ -65,9 +65,9 @@ int j_buf_append(j_buf *b, const void *p, size_t n)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void (*g_progress)(const char *role, double idle_s);
|
||||
static int (*g_progress)(const char *role, double idle_s);
|
||||
|
||||
void j_set_progress(void (*fn)(const char *role, double idle_s)) { g_progress = fn; }
|
||||
void j_set_progress(int (*fn)(const char *role, double idle_s)) { g_progress = fn; }
|
||||
|
||||
/* Wait for readability. A silent window is not an error: the request stays alive until
|
||||
* the deadline, and the wait is reported so a slow model looks like progress. */
|
||||
@@ -78,11 +78,14 @@ static int wait_readable(j_conn *c, double idle_s, double deadline)
|
||||
if (now_s() >= deadline) return 0;
|
||||
double left = deadline - now_s();
|
||||
double t = left < idle_s ? left : idle_s;
|
||||
/* a connection that carries a live stream is allowed to be woken earlier:
|
||||
* the caller wants to emit a heartbeat, not to give up */
|
||||
if (c->heartbeat_s > 0 && (double)c->heartbeat_s < t) t = (double)c->heartbeat_s;
|
||||
struct pollfd p = { .fd = c->fd, .events = POLLIN, .revents = 0 };
|
||||
int rc = poll(&p, 1, (int)(t * 1000.0) + 1);
|
||||
if (rc < 0) { if (errno == EINTR) continue; return -1; }
|
||||
if (rc == 0) {
|
||||
if (g_progress) g_progress(c->role ? c->role : "upstream", idle_s);
|
||||
if (g_progress && g_progress(c->role ? c->role : "upstream", t) != 0) return -1;
|
||||
continue; /* keep waiting until the deadline */
|
||||
}
|
||||
return 1;
|
||||
|
||||
+3
-1
@@ -30,7 +30,9 @@ typedef struct {
|
||||
|
||||
/* Called when a read waits longer than the idle window, so a thinking model shows up
|
||||
* as progress in the log instead of as a hang. */
|
||||
void j_set_progress(void (*fn)(const char *role, double idle_s));
|
||||
/* Called when a read has been silent for a while. Returning non-zero aborts the read
|
||||
* (the client is gone, or the caller has decided to stop waiting). */
|
||||
void j_set_progress(int (*fn)(const char *role, double idle_s));
|
||||
|
||||
void j_buf_init(j_buf *b);
|
||||
void j_buf_free(j_buf *b);
|
||||
|
||||
+26
-2
@@ -42,6 +42,11 @@ static j_config CFG;
|
||||
static pthread_mutex_t g_log_mu = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_mutex_t g_state_mu = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
/* Which connection, if any, wants a heartbeat from THIS thread right now. A thread
|
||||
* handles one request, so this is per-request state, not global. */
|
||||
static __thread j_conn *t_heartbeat;
|
||||
static __thread double t_heartbeat_started;
|
||||
|
||||
#define STATE_LOCK() pthread_mutex_lock(&g_state_mu)
|
||||
#define STATE_UNLOCK() pthread_mutex_unlock(&g_state_mu)
|
||||
|
||||
@@ -876,13 +881,21 @@ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq,
|
||||
free(req);
|
||||
|
||||
double deadline = now_s() + ep->timeout_s;
|
||||
up.heartbeat_s = head_sent ? 10 : 0; /* wake the reader every 10 s for a stream */
|
||||
if (head_sent) {
|
||||
t_heartbeat = client;
|
||||
t_heartbeat_started = now_s();
|
||||
}
|
||||
j_upstream_resp r;
|
||||
if (read_response(&up, &r, ep->idle_timeout_s, deadline, err, errlen)) {
|
||||
int rr = read_response(&up, &r, ep->idle_timeout_s, deadline, err, errlen);
|
||||
if (rr) {
|
||||
t_heartbeat = NULL;
|
||||
j_conn_close(&up);
|
||||
free(rewritten);
|
||||
return -1;
|
||||
}
|
||||
if (r.status >= 500 || r.status == 429) {
|
||||
t_heartbeat = NULL;
|
||||
snprintf(err, errlen, "upstream_status_%d", r.status);
|
||||
STATE_LOCK();
|
||||
snprintf(ep->last_error, sizeof ep->last_error, "upstream_status_%d", r.status);
|
||||
@@ -917,6 +930,7 @@ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq,
|
||||
} else {
|
||||
sse_error(client, r.status, msg);
|
||||
}
|
||||
t_heartbeat = NULL;
|
||||
resp_free(&r);
|
||||
j_conn_close(&up);
|
||||
free(rewritten);
|
||||
@@ -971,6 +985,7 @@ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq,
|
||||
if (!head_sent &&
|
||||
send_head(client, r.status, r.reason, &r.hdrs, r.chunked ? -1 : r.content_length,
|
||||
err, errlen)) {
|
||||
t_heartbeat = NULL;
|
||||
resp_free(&r);
|
||||
j_conn_close(&up);
|
||||
free(rewritten);
|
||||
@@ -980,6 +995,7 @@ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq,
|
||||
size_t pre_len = r.buf.len > pre_off ? r.buf.len - pre_off : 0;
|
||||
int rc = relay_stream(client, &up, r.buf.data + pre_off, pre_len,
|
||||
r.chunked ? -1 : r.content_length, ep->idle_timeout_s, deadline);
|
||||
t_heartbeat = NULL;
|
||||
resp_free(&r);
|
||||
j_conn_close(&up);
|
||||
free(rewritten);
|
||||
@@ -1219,9 +1235,17 @@ done:
|
||||
j_buf_free(&buf);
|
||||
}
|
||||
|
||||
static void progress_log(const char *role, double idle_s)
|
||||
static int progress_log(const char *role, double idle_s)
|
||||
{
|
||||
jlog("still_working", "side=%s silent_s=%.1f", role, idle_s);
|
||||
if (!t_heartbeat) return 0;
|
||||
/* A caller that asked for a stream is entitled to hear something while the model
|
||||
* thinks — otherwise a proxy with a 60 s read timeout kills a stream that is
|
||||
* perfectly alive. Comment lines: parsers must ignore them. */
|
||||
if (sse_comment(t_heartbeat, "junction: model is still working (%.0fs)",
|
||||
now_s() - t_heartbeat_started) < 0)
|
||||
return -1; /* client is gone; stop waiting for a model nobody listens to */
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *connection_thread(void *arg)
|
||||
|
||||
+36
-2
@@ -4,7 +4,7 @@ set -uo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
TMP=$(mktemp -d)
|
||||
PORT=8099
|
||||
P_FAIL=9990; P_GATED=9991; P_DEAD=9992; P_SLOW=9993
|
||||
P_FAIL=9990; P_GATED=9991; P_DEAD=9992; P_SLOW=9993; P_PONDER=9994
|
||||
PIDS=()
|
||||
FAILED=0
|
||||
|
||||
@@ -20,7 +20,7 @@ contains() { case "$1" in *"$2"*) ok "$3";; *) bad "$3 (missing '$2')";; esac; }
|
||||
|
||||
# A leftover process on a test port produces confusing failures (a stale junction can
|
||||
# answer requests with somebody else's config), so fail loudly and early.
|
||||
for port in 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 9443 9990 9991 9992 9993; do
|
||||
for port in 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 9443 9990 9991 9992 9993 9994; do
|
||||
if ss -tln 2>/dev/null | grep -q ":$port "; then
|
||||
echo "port $port is busy — a leftover junction/stub is still running:"
|
||||
ss -tlnp 2>/dev/null | grep ":$port " | sed 's/^/ /'
|
||||
@@ -36,6 +36,7 @@ TLSCC="-DJ_HAVE_TLS -lssl -lcrypto"
|
||||
tests/stub $P_FAIL "FAILING" --fail & PIDS+=($!)
|
||||
tests/stub $P_GATED "GATED" & PIDS+=($!)
|
||||
tests/stub $P_SLOW "SLOW" --delay 6 & PIDS+=($!)
|
||||
tests/stub $P_PONDER "PONDER" --delay 13 & PIDS+=($!)
|
||||
sleep 0.5
|
||||
|
||||
# a hook that refuses until a marker file exists (created after 3 s)
|
||||
@@ -426,6 +427,39 @@ esac
|
||||
grep -q "queued" "$TMP/queue.log" && ok "the queue is logged" || bad "the queue is logged"
|
||||
|
||||
|
||||
echo "== 15. a stream stays alive while the model is thinking =="
|
||||
HB=$((PORT+10))
|
||||
cat > "$TMP/hb.conf" <<CFG
|
||||
listen 127.0.0.1:$HB
|
||||
max_inflight 4
|
||||
endpoint pondering {
|
||||
base http://127.0.0.1:$P_PONDER/v1
|
||||
context 65536
|
||||
timeout 60
|
||||
idle_timeout 30
|
||||
}
|
||||
req for "*" check none pass pondering
|
||||
CFG
|
||||
./junction --config "$TMP/hb.conf" > "$TMP/hb.log" 2>&1 &
|
||||
PIDS+=($!)
|
||||
sleep 0.7
|
||||
HB_OUT=$(curl -sN -m 40 -X POST -H 'Accept: text/event-stream' -H 'Content-Type: application/json' \
|
||||
-d '{"model":"thinker","stream":true,"messages":[{"role":"user","content":"think hard"}]}' \
|
||||
"http://127.0.0.1:$HB/v1/chat/completions")
|
||||
contains "$HB_OUT" ": junction: model is still working" "a silent upstream produces heartbeats"
|
||||
contains "$HB_OUT" "served by PONDER" "and the answer still arrives after the silence"
|
||||
grep -q "still_working side=upstream" "$TMP/hb.log" && ok "the silence is logged as progress" \
|
||||
|| bad "the silence is logged as progress"
|
||||
# der stille Client bekommt weiterhin nichts injiziert
|
||||
HB_PLAIN=$(curl -s -m 40 -X POST -H 'Content-Type: application/json' \
|
||||
-d '{"model":"quiet","messages":[{"role":"user","content":"no stream"}]}' \
|
||||
"http://127.0.0.1:$HB/v1/chat/completions")
|
||||
case "$HB_PLAIN" in
|
||||
*junction:*) bad "a non-streaming caller received a heartbeat" ;;
|
||||
*) ok "no heartbeat without a stream" ;;
|
||||
esac
|
||||
|
||||
|
||||
echo
|
||||
if [ "$FAILED" -eq 0 ]; then echo "PASS"; else echo "FAILURES"; fi
|
||||
exit "$FAILED"
|
||||
Reference in new issue
Block a user