Teaching the sidebar who is home
Serie: SiteArchitectureTeil 4 von 5
For as long as this site has had a sidebar, the little row of icons at its foot has been a row of doors: GitHub, mail, Matrix, Discord, the Fediverse, each a plain link and nothing more, mute as to whether anybody might be found behind it. nowplaying, meanwhile, held open a WebSocket to every visitor of the home page and used it for the music alone. It seemed a waste of a perfectly good socket, and so the service has since been taught to carry rather more than songs: whether I can be reached on Discord or on Matrix, and what I have lately been committing to GitHub, now travel along the same line to the sidebar of every page.
What follows is the account of that extension — of three platforms with three entirely different notions of what it means to be present, of a homeserver that declined to say, and of a retraction I owe myself.
Three services, three temperaments
The three sources share an icon row and very little else.
GitHub is not a presence at all but a ledger, and a ledger is read rather than watched. The service looks in once a minute, and asks politely enough that a minute in which nothing happened costs nothing against GitHub’s rate limit; the thirty-day statistics — commits, the daily contributions, the streak — are fetched once an hour, there being nothing in them that a minute’s delay could spoil. A push to a private repository is reported as exactly that and is never named, on the server and, should the server ever forget itself, once more in the browser.
Discord offers no means of asking whether a person is online. It will tell a bot, and only a bot that shares a server with that person and has been granted the privilege of watching — which is to say that what I needed was not an API call but a resident. nowplaying keeps such a bot connected for as long as it runs, listens to a single user, and reports the state together with the kind of device it is set on. Should the privilege ever be withdrawn, the service recognises the matter as one for me rather than for a reconnect loop, and waits.
Matrix sits somewhere between the two. Presence is part of the protocol proper, but a homeserver shares it only with users who have a room in common, and so the arrangement is again one of a resident: a bot account of its own, sharing a room with mine, and listening for nothing but the presence of the one user it watches.
Matrix specification? Did you mean “suggestion”?
The Matrix integration ran first against matrix.org, and there it reported, with perfect consistency, that I was offline. I was not. matrix.org has had presence switched off for a good while now, and the weekly This Week in Matrix answered the perennial question as recently as September with a flat “Still no”. A homeserver without presence does not refuse the request; it simply answers every inquiry with offline, which is as convincing a lie as a protocol can possibly tell.
The service now follows one of my favorite quotes: Doveryai, no proveryai. On every start the bot sets itself online and reads its own state straight back; a server that answers offline a moment after being told online keeps no presence, and the integration reports just that — the icon shows a dashed outline rather than a hollow one, and the status reads presence not available. unknown must never look like offline, for the one says that I am away and the other that nobody knows whatsoever. I moved both accounts to tchncs.de, whose Synapse keeps presence as the specification intends.
There it promptly reported me offline again, with Element X open in my hand.
Element X, it turns out, speaks to the server through a newer means of synchronising, and Synapse takes no notice of presence along that road — only the classic one counts. Element’s own feature list for Element X states the matter plainly enough: no presence. The older client — Element Classic, as it is now called — still synchronises the classic way, and with that open the icon turned green inside five seconds. The fault, then, was neither the service’s nor the homeserver’s, but simply that the newest client has quietly stopped participating in a part of the protocol - which, in the case of matrix, is more a suggestion than anything else, as I’ve come to experience.
Tokens that expire on purpose
A bot that reconnects must authenticate, and on tchncs.de an access token lives for precisely five minutes, paired with a refresh token that facilitates access to the next one. The arrangement carries one sharp edge: a refresh token is spent the moment it is redeemed, so that a process which redeems it and then dies before writing the new pair anywhere has lost its session outright. The new pair is therefore written to disk before it is used for anything. Since then the session has rotated itself well over three hundred times a day without my having to give it another thought, which is very much what I was hoping for.
GitHub taught a different lesson. On the third of November it refused my token for eighteen minutes, and the service — believing it, as it had been written to — parked the integration for half an hour. When I restarted it, GitHub stumbled twice more and then accepted precisely the token it had been refusing all along. A single rejection, then, is not evidence of a broken token, and the service no longer treats it as one: it asks again, twice, before concluding anything.
One socket, and a card
The sidebar appears on every page, the music widget on the home page alone, and both want the same socket. That sounds trivial and very nearly was not: a browser handed the same script under two slightly different addresses will cheerfully load it twice, and open two sockets where one would do. A single map in the head of each page now pins the address down, and one page means one socket.
Hovering over the GitHub icon now opens a card — the site’s own card, accent and all, with a somewhat warmer glow — showing the thirty days as a small bar graph, the streak, the latest push, the public repositories and the followers. Its footer reads last change, not last update, and the distinction is load-bearing: the service checks every minute but records a snapshot only when something alters, so an old timestamp means that nothing has happened, never that nothing has been checked.
Instruments, once more — and a retraction
The previous post in this series recounted, with a certain satisfaction, the decision to give WebSocket sessions no span, since a span that lives for an afternoon is invisible for that afternoon. I then proceeded to make precisely the same mistake again: each integration ran beneath a single span for as long as it stayed healthy, which is to say indefinitely, with every poll and every refresh hung beneath it — one trace per integration, growing without bound, and never finished. Each of them now stands on its own. A handful of smaller defects came to light the same way, by looking at what the dashboards actually showed rather than at what I had meant them to show, and each was as unremarkable to fix as it had been to overlook.
That leaves the retraction. While building all of this I had made the authentication events carry their tokens in plain text, by deliberate choice, on the reasoning that the logs went to my own stack and nowhere else. The reasoning held, in a way, and yet, I found myself uneasy with even the thought of transmitting temporary access tokens, even encrypted as they were, to my LGTM stack. Secrets are now hashed for the simple purpose of asserting whether a secret has changed in the first place, compared to another trace in the same timeframe.
One Rust binary, then, that was written to listen to music without playing any, and has since taken to keeping a polite watch on three other front doors besides — resident in a Discord server, a Matrix room and a GitHub rate limit, and careful, now, to repeat nothing it was told in confidence.
Tags: Architecture, OpenTelemetry, Rust, Self-hosting
