Repository navigation
di.clienttracking - #132
Merged
Merged
Conversation
…ting
A kdb-x module for TorQ's code/handlers/trackclients.q on the consolidated di.*
conventions: init[config;deps] with settings under a `clienttracking section
(enabled, maxidle, retain, trackusage), log and handlers injected, every .z.*
observer registered through di.torq.handlers, no manifest (zero hard deps).
Behaviour:
- an unkeyed clients table (w, ipa, u, a, startp, endp, lastp, hits, sz) fed by
simple observers on .z.po/.z.pc/.z.wo/.z.wc; getclients/addclient/cleanup
exported plus getapimeta and version
- usage counting (hits/sz/lastp) as a post-phase watcher on .z.pg/.z.ps/.z.ws,
attached only once an exec owner exists on that event, otherwise deferred
(enableusage[] re-attaches later); sz measures sync (.z.pg) results only, as
kdb+ discards the ps/ws return value
- cleanup[] stamps endp on rows whose handle has left .z.W, reaps idle handles
past maxidle, and drops closed rows older than retain; idle reaping is
suspended (with a warning) while usage counting is not live, because lastp
cannot advance and every session would otherwise look idle
- timespan settings accept the TOML string form ("0D00:15:00", "1D"); the D is
mandatory - "900" is rejected rather than read as 9 hours
Found and fixed during socket and TorqX-POC smoke testing, each with a test:
- cleanup could close the caller's own handle mid-request (w<>.z.w guard)
- a reused handle number left two open rows (the stale row is closed on .z.po)
- the clients update copied the whole table per request (amend by name via
.z.M.clients; 24x faster at 50k rows)
Tests: 123 unit checks (hermetic, recording handlers) and 18 integration checks
that spawn a real child q hosting the module and drive it over IPC, proving the
self-reap guard, a genuine idle reap out of .z.W, and the no-exec-owner gate.
Both pass on KDB-X 0.1.2/2025.11.17 and 5.0/2026.07.23.
Known limitation, documented: no di.torq process type currently claims exec on
.z.pg/.z.ps, so on a stock stack usage counting stays deferred and idle reaping
suspended until a host wires an exec owner. Session tracking is unaffected.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
DIReview Summary0 critical | 2 warning(s) | 2 suggestion(s)
Suggestions
|
A DIReview comment on clienttracking.q:94 (coercespan) flagged that retain:0D and maxidle:0D are accepted by the same guard but mean different things: maxidle:0D is an explicitly-gated disable value (cleanup's idle-reap branch checks 0D<maxidle), but retain:0D has no equivalent gate - it flows straight into `endp<now-retain`, which reduces to `endp<now` and purges essentially every closed row on the very next sweep. So retain:0D means "purge closed rows immediately", not "keep forever". This is correct, existing behaviour, not a bug - the fix is documentation plus a test pinning it, not a coercespan change: - clienttracking.md: the retain row in the Configuration table now flags that 0D is not a keep-forever value, and a new paragraph next to the existing maxidle:0D note spells out the contrast and the endp<now-0D arithmetic explicitly. - test.csv: a new block inits with retain:0D, opens and closes a synthetic handle, runs cleanup[], and asserts the closed row is gone - proving the documented behaviour rather than just asserting it in prose. No change to clienttracking.q. Full suite green: 129/129 unit (was 123, +6 for the new block), 18/18 integration, both on KDB-X 0.1.2/2025.11.17 and 5.0/2026.07.23. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
DIReview Summary0 critical | 0 warning(s) | 1 suggestion(s)
Suggestions
|
Papamaci444
approved these changes
Sep 18, 2026
Papamaci444
left a comment
There was a problem hiding this comment.
Read all, tried the tests, looks good to me!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
di.clienttracking- inbound client session tracking and usage countingSummary
A kdb-x module for TorQ's
code/handlers/trackclients.q, built on the consolidateddi.*conventions (post-#128/#129). It records every inbound IPC/WebSocket session in a
clientstable, optionally counts per-request hits and result bytes, and reaps idle connections.
init[config;deps]with settings under a`clienttrackingsection:enabled(1b),maxidle(0D- idle reaping off by default),retain(0D02:00:00),trackusage(1b).Defaults are TorQ's effective ones from
config/settings/default.q. Absent section = defaults.logandhandlersare injected (required, strictly validated); no hardusedeps, so nomanifest - same as
di.serverselect..z.*observer goes throughdi.torq.handlers. No direct.z.*assignment.init,getclients,addclient,cleanup,enableusage,getapimeta,version."0D00:15:00","1D"); theDis mandatory -"900"is rejected rather than silently read as 9 hours.enabled:0bon init (or re-init) unhooks every observer and watcher this module registered andstops, leaving the session table intact; a later enabled re-init puts everything back.
How it works
clients(w ipa u a startp endp lastp hits sz) fed by simpleobservers on
.z.po/.z.pc/.z.wo/.z.wc. Handle-number reuse closes the stale row onopen, so there is never more than one open row per handle.
post-phase watcher on.z.pg/.z.ps/.z.ws. It can only attachonce that event has an
execowner; until then it is deferred (info log) andenableusage[]re-attaches it later.szcounts sync (.z.pg) results only - kdb+ discardsthe
.z.ps/.z.wsreturn value, so measuring it would report bytes never sent.endpon rows whose handle has left.z.W, force-closes handles idlepast
maxidle(never the caller's own handle), and drops closed rows older thanretain.Idle reaping is suspended with a warning while usage counting is not live:
lastpcannot advance without a post watcher, so every session would otherwise look idle and get
reaped on the first sweep.
.z.M.clients) rather than by value - measured 24× faster at 50krows (3.4µs vs 83µs per request). Rationale and numbers are in the design-decisions section.
Testing
init[config;deps]arity(a one-arg call silently returns a projection rather than erroring - pinned explicitly),
config validation (types, string timespans, negative/null/infinite rejection), enabled/disabled
init, deferred attach, hit/sz accounting on pg vs ps, handle reuse, retain expiry, the
suspended-reap warning, and the
getapimetacontract.the module with an exec owner, and this process is a genuine client of it - proves a real
.z.po, hits on real requests, and the self-reap guard (callingcleanup[]over your own idlehandle doesn't close it under you) on a real IPC round-trip. Outbound: this process tracks the
handle to the child, proves idle reaping stays suspended with no exec owner here, then proves a
real idle reap once one is registered. Skips cleanly if no q binary is found.
TorqX-POC stack (tp, hdb, rdb, wdb, gateway, feed) wired from
code/common. Three bugsfound in that pass are fixed here with regression tests: self-reap, duplicate rows on handle
reuse, and the by-value table copy.
Run the unit suite:
The integration suite needs a fresh session (
moduletestonly loadstest.csv):Known limitation - please read before reviewing
No
di.torqprocess type currently claimsexecon.z.pg/.z.ps. On a stock stackthat means usage counting stays deferred and idle reaping stays suspended (with a clear
warning) until a host registers an exec owner. Session tracking - the table, open/close
stamps, retain cleanup - is fully live regardless. The gate is tested in both suites and
documented; the fix belongs in
di.torq, not here (see follow-up 1 - note that landing itwould also mean
di.querylog's wrappers get displaced the moment something claimsexec,so the two need reconciling together, not just this module).
di.torqalso has no settings-section hook for this module yet (the waydi.queryloghas);a host wires it from app code as shown in the Initialisation section of the docs.
Follow-ups (separate PRs/issues, not in this change)
di.torq/di.torq.handlers: a defaultexecowner on.z.pg/.z.ps. Root causeof the limitation above. Note that once anything claims exec via handlers,
di.querylog'sdirect-assignment wrappers are displaced - the two need to be reconciled together.
di.util.toml: two sections with different key sets throw'mismatch. Found whilefeeding this module a real TOML config. Independent of this PR - worth filing/fixing
promptly since it's already live on
main, not just a design note.di/torq/handlers/handlers.mdis stale - it doesn't mention the shippedtransformphase, which this module's docs rely on to explain what
postsees.