Push a frame instead of storing and fetching it
The rate the media dtypes could carry was one frame every second or two: each
was a file on the data volume, an event on the socket, and a request back for
the bytes. This closes both halves of that, and they are one feature.
`save_artifact(..., volatile=True)` writes to a `VolatileStore` — the same
content-addressed store, in `/dev/shm`, bounded by size with the oldest falling
out (`ARTIFACT_VOLATILE_BYTES`, 48 MB under the container's raised `shm_size`).
Nothing sweeps it: a frame nobody kept is not worth walking the store to find.
`ArtifactStore.path` falls through to it, which is what lets a volatile frame be
an ordinary reference everywhere else — the dtype check, a panel's digest scope,
`load_artifact` in a node, and the widget's own fetch all work on one unchanged.
`adopt` copies one into the store when a run records it, so "returned media is
kept, emitted media is not" stays true.
The bytes then go down the flows websocket as a length-prefixed binary frame,
sent just ahead of the `message_value` naming them, so a tile has the frame when
it hears the value moved. Nothing is pushed unasked: a client names the messages
it is drawing (`{"type":"media","names":[…]}`), a panel's list is intersected
with the scope it already had, and only the newest frame per name in a batch is
sent — a client that fell behind is not handed frames it would draw over. The
tunnel relays text only, so a screen reached through a portal falls back to
fetching, which is why the rate table now has two rows.
Around the edges: the remote worker's fetch cache is bounded at last
(`FLUKSIO_ARTIFACT_CACHE_BYTES`), since content addressing means nothing in it
ever expires and a media stream fills it with chunks nothing asks for twice; a
port carrying an image draws the frame in the node panel rather than only
saying `image/png · frame.png · 1.79kB`; and an edge chip says that much instead
of a line of hash. The media screenshot stops waiting for `networkidle` — a
camera is a socket that never goes quiet, which is the point of it.
This commit is contained in:
@@ -83,6 +83,26 @@ const listeners = new Map<string, Set<Listener>>()
|
||||
let connected = false
|
||||
const connectionListeners = new Set<Listener>()
|
||||
|
||||
/**
|
||||
* Object URLs for frames the engine pushed, keyed by digest.
|
||||
*
|
||||
* Small on purpose: a camera's frames are worth exactly as long as the next
|
||||
* one takes to arrive, and a tab that held every one of them would grow
|
||||
* without bound. The oldest is revoked when the room runs out.
|
||||
*/
|
||||
const BYTES_LIMIT = 8
|
||||
const bytes = new Map<string, string>()
|
||||
|
||||
/**
|
||||
* Which messages this page is drawing bytes for, and how many tiles each.
|
||||
*
|
||||
* Refcounted because two tiles may show one camera and the first to unmount
|
||||
* must not stop the second's frames. The socket sends the key set whenever it
|
||||
* changes; nothing is pushed for a name nobody is looking at.
|
||||
*/
|
||||
const wanted = new Map<string, number>()
|
||||
const wantedListeners = new Set<Listener>()
|
||||
|
||||
function notify(key: string) {
|
||||
for (const listener of listeners.get(key) ?? []) listener()
|
||||
}
|
||||
@@ -247,6 +267,53 @@ export const liveStore = {
|
||||
isConnected() {
|
||||
return connected
|
||||
},
|
||||
/**
|
||||
* Take a frame the socket pushed, as an object URL the tile can draw.
|
||||
*
|
||||
* Content addressing means a digest already here is the same bytes, so the
|
||||
* blob is dropped rather than replacing an identical one.
|
||||
*/
|
||||
setBytes(digest: string, blob: Blob) {
|
||||
if (bytes.has(digest)) return
|
||||
bytes.set(digest, URL.createObjectURL(blob))
|
||||
while (bytes.size > BYTES_LIMIT) {
|
||||
const oldest = bytes.keys().next().value
|
||||
if (oldest === undefined) break
|
||||
const url = bytes.get(oldest)
|
||||
bytes.delete(oldest)
|
||||
if (url) URL.revokeObjectURL(url)
|
||||
notify(`bytes:${oldest}`)
|
||||
}
|
||||
notify(`bytes:${digest}`)
|
||||
},
|
||||
getBytes(digest: string) {
|
||||
return bytes.get(digest)
|
||||
},
|
||||
/** Ask for this message's frames while the caller is drawing it. */
|
||||
wantBytes(name: string) {
|
||||
wanted.set(name, (wanted.get(name) ?? 0) + 1)
|
||||
if (wanted.get(name) === 1)
|
||||
for (const listener of wantedListeners) listener()
|
||||
return () => {
|
||||
const left = (wanted.get(name) ?? 1) - 1
|
||||
if (left > 0) {
|
||||
wanted.set(name, left)
|
||||
return
|
||||
}
|
||||
wanted.delete(name)
|
||||
for (const listener of wantedListeners) listener()
|
||||
}
|
||||
},
|
||||
wantedNames() {
|
||||
return [...wanted.keys()]
|
||||
},
|
||||
/** Told when the set changes, so the socket can say so. */
|
||||
onWanted(listener: Listener) {
|
||||
wantedListeners.add(listener)
|
||||
return () => {
|
||||
wantedListeners.delete(listener)
|
||||
}
|
||||
},
|
||||
reset() {
|
||||
for (const key of values.keys()) notify(`value:${key}`)
|
||||
values.clear()
|
||||
@@ -264,6 +331,13 @@ export const liveStore = {
|
||||
notify("logs")
|
||||
for (const flow of paused) notify(`paused:${flow}`)
|
||||
paused.clear()
|
||||
for (const [digest, url] of bytes) {
|
||||
URL.revokeObjectURL(url)
|
||||
notify(`bytes:${digest}`)
|
||||
}
|
||||
bytes.clear()
|
||||
// `wanted` is deliberately kept: the tiles asking are still mounted, and a
|
||||
// reconnecting socket has to say what they want all over again.
|
||||
},
|
||||
}
|
||||
|
||||
@@ -274,6 +348,15 @@ export function useLiveValue(name: string | undefined): LiveValue | undefined {
|
||||
)
|
||||
}
|
||||
|
||||
/** The pushed bytes for a digest, if the socket carried them. */
|
||||
export function useLiveBytes(digest: string | undefined): string | undefined {
|
||||
return useSyncExternalStore(
|
||||
(listener) =>
|
||||
digest ? subscribeKey(`bytes:${digest}`, listener) : () => {},
|
||||
() => (digest ? bytes.get(digest) : undefined),
|
||||
)
|
||||
}
|
||||
|
||||
export function useNodeStatus(nodeId: string): LiveStatus | undefined {
|
||||
return useSyncExternalStore(
|
||||
(listener) => subscribeKey(`status:${nodeId}`, listener),
|
||||
|
||||
Reference in New Issue
Block a user