Previous: , Up: Persistence stores   [Contents][Index]


8.7.5 Sleepy actors

Goblins has the ability to have actors enter a sleep state, and wake back up when they’re needed. This allows the persistence system to act somewhat like a database’s working set or a hot cache, where actors do not need to take up resources when not in use. When an actor is asleep only the reference and a small subset of the object remains. The local references an actor holds within its portrait are kept around in memory, however all the other portrait data as well as the behaviour closure will be freed. This allows sleeping actors to have a much smaller footprint.

This functionality is particularly useful if, for example, you were to have a blog where all posts are actors. Much of the data is inert (largely textual in the case of blogs) and can be kept on disk, woken up only when needed. This would also be good for chat logs ActivityPub implementations, and many other situations.

Goblins code can be written as normal using $, <-, or <-np with references (asleep or awake). If an object is asleep then it’ll be restored to handle the message. After each churn, the vat will query the sleep strategy to determine which (if any) objects should be put to sleep. Goblins comes with sleep strategies out of the box. The sleep strategy is determined per vat when spawning.

To use sleepy actors with a vat, you can enable it by simply specifying a sleep strategy. Here’s how to use the sleep:always included with Goblins:

(use-modules (goblins)
             (goblins persistence-store memory)
             (goblins sleep-strategy always)
             (goblins actor-lib cell))

(define store (make-memory-store))
(define-values (vat cell)
  (spawn-persistent-vat
   cell-env
   (lambda () (spawn ^cell (spawn ^cell 'inner)))
   store
   #:sleep-strategy sleep:lru))

When a brand new vat is spawned, it initially spawns the objects from the thunk provided. These will initially be awake, after the first churn the sleep strategy will determine which actors will be put to sleep. When restoring a vat, all the actors will be restored asleep. Actors will then be woken up only after messaging them.


Previous: Web localStorage store, Up: Persistence stores   [Contents][Index]