Add cache_actor

This commit is contained in:
Emery Hemingway 2023-11-08 10:50:30 +00:00
parent 86be2a67c2
commit 72f5d04030
3 changed files with 78 additions and 3 deletions

View File

@ -1,5 +1,25 @@
# Syndicate utils
## cache_actor
An actor that observes patterns and reässerts the values they capture for a given lifetime. Takes the arguments `{ dataspace: #!any lifetime: double }`. The lifetime of a cache counts down from moment a value is asserted.
Example configuration:
```
? <nixspace ?nixspace> [
; Require the nix_actor during observations.
?nixspace> ? <Observe <rec eval _> _> [
$config <require-service <daemon nix_actor>> ]
?nixspace> ? <Observe <rec realise _> _> [
$config <require-service <daemon nix_actor>> ]
; Cache anything captured by observers in the $nixspace for an hour.
; The nix_actor is not required during caching.
$config ? <service-object <daemon cache_actor> ?cap> [
$cap { dataspace: $nixspace lifetime: 3600.0 } ]
]
```
## json_translator
Wrapper that executes a command, parses its JSON output, converts to Preserves record `<recv @jsonData any>`, and publishes and messages to its initial dataspace.

55
src/cache_actor.nim Normal file
View File

@ -0,0 +1,55 @@
# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/times
import preserves, syndicate,
syndicate/relays,
syndicate/durings,
syndicate/actors/timers
type BootArgs {.preservesDictionary.} = object
dataspace: Cap
lifetime: float64
proc afterTimeout(n: float64): LaterThan =
## Get a `LaterThan` record for `n` seconds in the future.
result.seconds = getTime().toUnixFloat() + n
type CacheEntity {.final.} = ref object of Entity
timeouts, target: Cap
# dataspaces for observing timeouts and publishing values
pattern: Pattern
lifetime: float64
method publish(cache: CacheEntity; turn: var Turn; ass: AssertionRef; h: Handle) =
## Re-assert pattern captures in a sub-facet.
discard inFacet(turn) do (turn: var Turn):
# TODO: a seperate facet for every assertion, too much?
var ass = depattern(cache.pattern, ass.value.sequence)
# Build an assertion with what he have of the pattern and capture.
discard publish(turn, cache.target, ass)
let timeout = afterTimeout(cache.lifetime)
onPublish(turn, cache.timeouts, ?timeout) do:
stop(turn) # end this facet
proc isObserve(pat: Pattern): bool =
pat.orKind == PatternKind.DCompound and
pat.dcompound.orKind == DCompoundKind.rec and
pat.dcompound.rec.label.isSymbol"Observe"
runActor("cache_actor") do (turn: var Turn; root: Cap):
spawnTimers(turn, root)
connectStdio(turn, root)
during(turn, root, ?BootArgs) do (ds: Cap, lifetime: float64):
onPublish(turn, ds, ?Observe) do (pat: Pattern, obs: Cap):
var cache: CacheEntity
if obs.relay != turn.facet and not(pat.isObserve):
# Watch pattern if the observer is not us
# and if the pattern isn't a recursive observe
cache = CacheEntity(
timeouts: root,
target: ds,
pattern: pat,
lifetime: lifetime,
)
discard observe(turn, ds, pat, cache)

View File

@ -1,13 +1,13 @@
# Package
version = "20231026"
version = "20231108"
author = "Emery Hemingway"
description = "Utilites for Syndicated Actors and Synit"
license = "unlicense"
srcDir = "src"
bin = @["json_socket_translator", "json_translator", "mintsturdyref", "msg", "net_mapper", "preserve_process_environment", "syndex_card", "syndump"]
bin = @["cache_actor", "json_socket_translator", "json_translator", "mintsturdyref", "msg", "net_mapper", "preserve_process_environment", "syndex_card", "syndump"]
# Dependencies
requires "nim >= 1.6.6", "illwill", "syndicate >= 20231021"
requires "nim >= 1.6.6", "illwill", "syndicate >= 20231107"