diff --git a/README.md b/README.md index 945b89d..64e399e 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,13 @@ Endpoint: | `timeout` | total wall clock for one request (seconds) | | `connect_timeout_ms` | per-endpoint connect limit; 0 = the global value | | `weight` | 1..100, how often this endpoint is chosen (default 1) | + + A chain can be named once and reused, and a pool may name another pool: + + pool lokal box3, box6 + pool alles cloud, lokal + req for "*" pass alles + | `idle_timeout` | silence before `still_working` is logged (seconds) | | `cooldown` | rest after a failed request (seconds) | | `hook` | shell command; exit 0 = usable, non-zero = not yet (repeatable) | diff --git a/examples/full-reference.conf b/examples/full-reference.conf index ce849e4..2ad39c6 100644 --- a/examples/full-reference.conf +++ b/examples/full-reference.conf @@ -348,6 +348,14 @@ req for "blocked-*" block # Alles Übrige: erst der wache lokale Rechner, dann die Cloud, dann Guthaben. # Kein Treffer für eine Anfrage bedeutet 503 no_matching_rule — eine Lücke in der # Konfiguration ist kein Fall für "irgendwohin schicken". +# Ketten einen Namen geben. Ein Pool darf auch auf einen anderen verweisen; beim Laden +# wird er aufgelöst, damit eine Regel nicht jedes Mal fünf Ziele ausschreiben muss. +pool lokal ollama1, ollama2 +pool alles cloud, lokal + +# Ein Endpunkt darf auch in einer Zeile stehen, wenn er klein ist: +# endpoint kurz { base http://127.0.0.1:8080/v1; timeout 60 } + # Diese eine Regel will reines Failover, egal was global eingestellt ist. req for "starr*" balance first pass workstation, cloud diff --git a/src/config.c b/src/config.c index a981919..74d4565 100644 --- a/src/config.c +++ b/src/config.c @@ -562,6 +562,65 @@ int j_config_load(const char *path, j_config *cfg, char *err, size_t errlen) continue; } + if (!strcmp(w[0], "pool")) { + /* pool [, …] [as ] … */ + if (cur) { + snprintf(err, errlen, "line %d: pools are not allowed inside an endpoint block", lineno); + fclose(fh); + return -1; + } + if (n < 3) { + snprintf(err, errlen, "line %d: pool needs a name and at least one target", lineno); + fclose(fh); + return -1; + } + if (cfg->pool_count >= J_MAX_POOLS) { + snprintf(err, errlen, "line %d: more than %d pools", lineno, J_MAX_POOLS); + fclose(fh); + return -1; + } + j_pool *pl = &cfg->pools[cfg->pool_count]; + memset(pl, 0, sizeof *pl); + pl->line = lineno; + snprintf(pl->name, sizeof pl->name, "%s", w[1]); + for (int i = 2; i < n; ) { + char t[J_NAME_MAX]; + snprintf(t, sizeof t, "%s", w[i++]); + size_t tl = strlen(t); + while (tl && (t[tl-1] == ',' || t[tl-1] == ' ')) t[--tl] = '\0'; + if (!tl) continue; + if (pl->count >= J_MAX_TARGETS) { + snprintf(err, errlen, "line %d: pool %s has more than %d targets", + lineno, pl->name, J_MAX_TARGETS); + fclose(fh); + return -1; + } + int idx = pl->count; + snprintf(pl->targets[idx], sizeof pl->targets[idx], "%s", t); + if (i < n && !strcmp(w[i], "as")) { + if (i + 1 >= n) { + snprintf(err, errlen, "line %d: 'as' without a model", lineno); + fclose(fh); + return -1; + } + char m[J_GLOB_MAX]; + snprintf(m, sizeof m, "%s", w[i+1]); + size_t ml = strlen(m); + while (ml && (m[ml-1] == ',' || m[ml-1] == ' ')) m[--ml] = '\0'; + snprintf(pl->target_as[idx], sizeof pl->target_as[idx], "%s", m); + i += 2; + } + pl->count++; + } + if (!pl->count) { + snprintf(err, errlen, "line %d: pool %s has no targets", lineno, pl->name); + fclose(fh); + return -1; + } + cfg->pool_count++; + continue; + } + if (!strcmp(w[0], "req")) { if (cur) { snprintf(err, errlen, "line %d: rules are not allowed inside an endpoint block", lineno); @@ -614,8 +673,33 @@ int j_config_load(const char *path, j_config *cfg, char *err, size_t errlen) } if (!strcmp(w[0], "endpoint") || !strcmp(w[0], "backend")) { - /* endpoint NAME { — the brace must be the last word */ - if (n != 3 || strcmp(w[2], "{")) { + /* endpoint NAME { … } — mehrzeilig, oder alles in einer Zeile. + * Einzeilig ist praktisch für kleine Konfigurationen. Der Rumpf wird aus den + * Wörtern zusammengesetzt, nicht aus der Zeile: tokenize() setzt Nullen in den + * Puffer, die Zeile ist danach nicht mehr lesbar. */ + int inline_body = 0; + char body[1024]; + body[0] = '\0'; + if (n >= 5 && !strcmp(w[2], "{")) { + /* Das schließende '}' steht entweder allein oder am letzten Wort. */ + int last = n - 1; + size_t ll = strlen(w[last]); + if (ll && w[last][ll - 1] == '}') { + w[last][ll - 1] = '\0'; + if (ll == 1) last--; + size_t used = 0; + for (int i = 3; i <= last; i++) { + size_t need = strlen(w[i]) + 2; + if (used + need >= sizeof body) break; + if (used) body[used++] = ' '; + memcpy(body + used, w[i], strlen(w[i])); + used += strlen(w[i]); + body[used] = '\0'; + } + inline_body = 1; + } + } + if (!inline_body && (n != 3 || strcmp(w[2], "{"))) { snprintf(err, errlen, "line %d: expected 'endpoint NAME {'", lineno); fclose(fh); return -1; @@ -633,6 +717,26 @@ int j_config_load(const char *path, j_config *cfg, char *err, size_t errlen) cur = &cfg->endpoints[cfg->endpoint_count++]; endpoint_defaults(cur); snprintf(cur->name, sizeof cur->name, "%s", w[1]); + if (inline_body) { + /* Den Rumpf in Wörter zerlegen und wie eigene Zeilen abarbeiten — + * Semikolon oder Komma trennen die Anweisungen. */ + char *sp = NULL; + for (char *tok = strtok_r(body, ";", &sp); tok; tok = strtok_r(NULL, ";", &sp)) { + char *ip = tok; + while (*ip == ' ' || *ip == '\t') ip++; + int in = 0; + char *iw[MAX_WORDS]; + for (char *q = strtok(ip, " \t"); q && in < MAX_WORDS; q = strtok(NULL, " \t")) + iw[in++] = q; + if (!in) continue; + if (!strcmp(iw[0], "}")) continue; + if (endpoint_kw(cur, iw, in, lineno, err, errlen)) { + fclose(fh); + return -1; + } + } + cur = NULL; /* der Block ist mit der Zeile beendet */ + } continue; } @@ -665,6 +769,44 @@ int j_config_load(const char *path, j_config *cfg, char *err, size_t errlen) } if (!ep->kind[0]) snprintf(ep->kind, sizeof ep->kind, "remote"); } + /* Pool-Namen expandieren. Das passiert nach dem Einlesen, damit ein Pool auch + * unterhalb der Regel stehen darf, die ihn benutzt. */ + /* Ein Pool darf auf einen anderen verweisen (alles → lokal → box3, box6). Deshalb + * wird so lange expandiert, bis sich nichts mehr ändert. Die Obergrenze verhindert, + * dass ein Ringschluss die Schleife endlos dreht. */ + for (int pass = 0; pass < 8; pass++) { + int changed = 0; + for (int i = 0; i < cfg->rule_count; i++) { + j_rule *r = &cfg->rules[i]; + for (int t = 0; t < r->target_count; t++) { + for (int p = 0; p < cfg->pool_count; p++) { + if (strcmp(cfg->pools[p].name, r->targets[t])) continue; + j_pool *pl = &cfg->pools[p]; + /* Platz für die Ziele des Pools schaffen (ein Ziel wird zu count Zielen). */ + if (r->target_count - 1 + pl->count > J_MAX_TARGETS) { + snprintf(err, errlen, + "line %d: pool %s does not fit into rule (more than %d targets)", + r->line, pl->name, J_MAX_TARGETS); + return -1; + } + for (int k = r->target_count - 1; k > t; k--) { + snprintf(r->targets[k + pl->count - 1], sizeof r->targets[0], "%s", r->targets[k]); + snprintf(r->target_as[k + pl->count - 1], sizeof r->target_as[0], "%s", r->target_as[k]); + } + for (int c = 0; c < pl->count; c++) { + snprintf(r->targets[t + c], sizeof r->targets[0], "%s", pl->targets[c]); + snprintf(r->target_as[t + c], sizeof r->target_as[0], "%s", pl->target_as[c]); + } + r->target_count += pl->count - 1; + t += pl->count - 1; + changed = 1; + break; + } + } + } + if (!changed) break; + } + for (int i = 0; i < cfg->rule_count; i++) { j_rule *r = &cfg->rules[i]; /* Eine ctx-Bedingung ohne Skalierungsfaktor kann nie erfüllen, was sie diff --git a/src/config.h b/src/config.h index c2a3aad..c6678eb 100644 --- a/src/config.h +++ b/src/config.h @@ -46,6 +46,7 @@ #define J_MAX_EP_HEADERS 8 #define J_HDR_LINE_MAX 256 #define J_MAX_VKEYS 16 +#define J_MAX_POOLS 16 /* Balance modes: how targets inside one rule are ordered. */ enum { J_BALANCE_FIRST = 0, J_BALANCE_RR = 1, J_BALANCE_USAGE = 2, J_BALANCE_COST = 3 }; @@ -166,6 +167,21 @@ typedef struct { int line; } j_rule; +/* Eine benannte Kette: macht aus fünf ausgeschriebenen Zielen einen Namen. + * + * pool lokal box3, box6 + * pool bild vision, cloud as "deepseek-v4.1-flash" + * + * Beim Laden wird der Name in den Regeln durch die Ziele ersetzt. Das ist Textarbeit, + * kein Laufzeitkonzept — eine Regel weiß hinterher nicht, dass sie aus einem Pool kam. */ +typedef struct { + char name[J_NAME_MAX]; + char targets[J_MAX_TARGETS][J_NAME_MAX]; + char target_as[J_MAX_TARGETS][J_GLOB_MAX]; + int count; + int line; +} j_pool; + typedef struct { char listen_host[J_NAME_MAX]; int listen_port; @@ -196,6 +212,9 @@ typedef struct { int vkey_count; int balance; /* J_BALANCE_* */ + j_pool pools[J_MAX_POOLS]; + int pool_count; + j_rule rules[J_MAX_RULES]; int rule_count; } j_config; diff --git a/tests/pools.sh b/tests/pools.sh new file mode 100644 index 0000000..8f6bbd1 --- /dev/null +++ b/tests/pools.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# Wird von tests/run.sh eingebunden: benannte Ketten (pool) und einzeilige Blöcke. + +echo "== 20. Pools und einzeilige Endpunkt-Blöcke ==" + +# 1. Einzeiliger Block: alles in einer Zeile, keine Klammerzeile nötig. +cat > "$TMP/inline.conf" < "$TMP/inline.log" 2>&1 & +PIDS+=($!) +sleep 0.8 +R=$(curl -s -m 10 -X POST -H 'Content-Type: application/json' \ + -d '{"model":"x","messages":[{"role":"user","content":"hi"}]}' \ + "http://127.0.0.1:$((PORT+31))/v1/chat/completions") +contains "$R" "served by GATED" "a one-line endpoint block is enough to serve a request" +grep -q 'endpoint kurz: block not closed' "$TMP/inline.log" && \ + bad "the one-line block was mistaken for an open block" || \ + ok "the one-line block leaves no block open" + +# 2. Pool: eine Kette bekommt einen Namen, die Regel benutzt ihn. +cat > "$TMP/pool.conf" < "$TMP/pool.log" 2>&1 & +PIDS+=($!) +sleep 0.8 +POOL=$(curl -s -m 5 "http://127.0.0.1:$((PORT+32))/status") +case "$POOL" in + *'"targets":"eins, zwei"'*|*'eins, zwei'*) ok "pool expands to its targets in the rule" ;; + *) bad "pool did not expand (status: $(printf '%s' "$POOL" | head -c 120))" ;; +esac + +# 3. Verschachtelte Pools: alles → lokal → zwei Ziele +cat > "$TMP/pool2.conf" < "$TMP/pool2.log" 2>&1 & +PIDS+=($!) +sleep 0.8 +P2=$(curl -s -m 5 "http://127.0.0.1:$((PORT+33))/status") +case "$P2" in + *'a, b'*) ok "a pool may name another pool" ;; + *) bad "nested pools did not resolve" ;; +esac + +# 4. Ein unbekannter Name bleibt ein Fehler — kein stiller Durchlauf. +cat > "$TMP/poolbad.conf" <&1 | head -1) +case "$OUT" in + *"unknown endpoint"*) ok "an unknown pool or endpoint name is refused" ;; + *) bad "unknown name was not refused: $OUT" ;; +esac diff --git a/tests/run.sh b/tests/run.sh index 7e77442..0290de6 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -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 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 9443 9990 9991 9992 9993 9994 9995 9996; do +for port in 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8165 8166 8167 8168 9443 9990 9991 9992 9993 9994 9995 9996; 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/^/ /' @@ -627,6 +627,10 @@ contains "$OUT" "bad arguments" "an impossible offpeak window is rejected" # shellcheck source=tests/balancing.sh . "$(dirname "$0")/balancing.sh" +# Benannte Ketten und einzeilige Blöcke. +# shellcheck source=tests/pools.sh +. "$(dirname "$0")/pools.sh" + echo if [ "$FAILED" -eq 0 ]; then echo "PASS"; else echo "FAILURES"; fi exit "$FAILED"