X-Git-Url: https://jxself.org/git/?p=8sync.git;a=blobdiff_plain;f=8sync%2Factors.scm;h=6e772c6b344ecefc66181919174a54fbf1be6e9a;hp=867e3d08e2e0a2d2525aa5d06189231c18648b99;hb=dc2155083a90de90e24f5341b837d4d96ce2898c;hpb=5fa0225a261a031fc8cd2988932e6a3e8f4528fb diff --git a/8sync/actors.scm b/8sync/actors.scm index 867e3d0..6e772c6 100644 --- a/8sync/actors.scm +++ b/8sync/actors.scm @@ -25,7 +25,6 @@ #:use-module (ice-9 match) #:use-module (ice-9 pretty-print) #:use-module (8sync agenda) - #:use-module (8sync repl) #:export (;; utilities... ought to go in their own module big-random-number big-random-number-string @@ -48,6 +47,8 @@ actor-id-hive actor-id-string + actor-alive? + build-actions define-simple-actor @@ -70,11 +71,11 @@ message-auto-reply? - <- <-wait <-wait* <-reply <-reply-wait <-reply-wait* + <- <-* <-wait <-wait* <-reply <-reply* <-reply-wait <-reply-wait* call-with-message msg-receive msg-val - ez-run-hive + run-hive bootstrap-message serialize-message write-message @@ -195,51 +196,78 @@ ;; confusing. (8sync (hive-process-message hive new-message))))) - -(define (<- from-actor to-id action . message-body-args) +(define (<- to-id action . message-body-args) "Send a message from an actor to another actor" - (send-message '() from-actor to-id action + (send-message '() (%current-actor) to-id action #f #f message-body-args)) -(define (<-wait* send-options from-actor to-id action . message-body-args) - "Like <-wait, but allows extra parameters, for example whether to -#:accept-errors" - (apply wait-maybe-handle-errors - (send-message send-options from-actor to-id action - #f #t message-body-args) - send-options)) +(define (<-* send-options to-id action . message-body-args) + "Like <-*, but allows extra parameters via send-options" + (define* (really-send #:key (actor (%current-actor)) + #:allow-other-keys) + (send-message send-options actor to-id action + #f #f message-body-args)) + (apply really-send send-options)) -(define (<-wait from-actor to-id action . message-body-args) +(define (<-wait to-id action . message-body-args) "Send a message from an actor to another, but wait until we get a response" - (apply <-wait* '() from-actor to-id action message-body-args)) + (wait-maybe-handle-errors + (send-message '() (%current-actor) to-id action + #f #t message-body-args))) + +(define (<-wait* send-options to-id action . message-body-args) + "Like <-wait, but allows extra parameters, for example whether to +#:accept-errors" + (define* (really-send #:key (actor (%current-actor)) + #:allow-other-keys) + (apply wait-maybe-handle-errors + (send-message send-options actor to-id action + #f #t message-body-args) + send-options)) + (apply really-send send-options)) ;; TODO: Intelligently ~propagate(ish) errors on -wait functions. ;; We might have `send-message-wait-brazen' to allow callers to ;; not have an exception thrown and instead just have a message with ;; the appropriate '*error* message returned. -(define (<-reply from-actor original-message . message-body-args) +(define (<-reply original-message . message-body-args) "Reply to a message" - (send-message '() from-actor (message-from original-message) '*reply* + (send-message '() (%current-actor) (message-from original-message) '*reply* original-message #f message-body-args)) -(define (<-auto-reply from-actor original-message) +(define (<-reply* send-options original-message . message-body-args) + "Like <-reply, but allows extra parameters via send-options" + (define* (really-send #:key (actor (%current-actor)) + #:allow-other-keys) + (send-message send-options actor + (message-from original-message) '*reply* + original-message #f message-body-args)) + (apply really-send send-options)) + +(define (<-auto-reply actor original-message) "Auto-reply to a message. Internal use only!" - (send-message '() from-actor (message-from original-message) '*auto-reply* + (send-message '() actor (message-from original-message) '*auto-reply* original-message #f '())) -(define (<-reply-wait* send-options from-actor original-message - . message-body-args) +(define (<-reply-wait original-message . message-body-args) "Reply to a messsage, but wait until we get a response" - (apply wait-maybe-handle-errors - (send-message send-options from-actor - (message-from original-message) '*reply* - original-message #t message-body-args) - send-options)) + (wait-maybe-handle-errors + (send-message '() (%current-actor) + (message-from original-message) '*reply* + original-message #t message-body-args))) -(define (<-reply-wait from-actor original-message . message-body-args) - "Reply to a messsage, but wait until we get a response" - (apply <-reply-wait* '() from-actor original-message message-body-args)) +(define (<-reply-wait* send-options original-message + . message-body-args) + "Like <-reply-wait, but allows extra parameters via send-options" + (define* (really-send #:key (actor (%current-actor)) + #:allow-other-keys) + (apply wait-maybe-handle-errors + (send-message send-options actor + (message-from original-message) '*reply* + original-message #t message-body-args) + send-options)) + (apply really-send send-options)) (define* (wait-maybe-handle-errors message #:key accept-errors @@ -282,6 +310,14 @@ raise an exception if an error." (call/ec find-message-handler)) (apply method actor message (message-body message))) +(define-syntax-rule (build-actions (symbol method) ...) + "Construct an alist of (symbol . method), where the method is wrapped +with wrap-apply to facilitate live hacking and allow the method definition +to come after class definition." + (list + (cons (quote symbol) + (wrap-apply method)) ...)) + (define-class () ;; An address object (id #:init-keyword #:id @@ -295,15 +331,15 @@ raise an exception if an error." ;; @@: There's no reason not to use #:class instead of ;; #:each-subclass anywhere in this file, except for ;; Guile bug #25211 (#:class is broken in Guile 2.2) - #:allocation #:each-subclass) + #:allocation #:each-subclass + #:getter actor-message-handler) ;; This is the default, "simple" way to inherit and process messages. - (actions #:init-value '() + (actions #:init-value (build-actions + ;; Default cleanup method is to do nothing. + (*cleanup* (const #f))) #:allocation #:each-subclass)) -(define-method (actor-message-handler (actor )) - (slot-ref actor 'message-handler)) - ;;; So these are the nicer representations of addresses. ;;; However, they don't serialize so easily with scheme read/write, so we're ;;; using the simpler cons cell version below for now. @@ -322,13 +358,13 @@ raise an exception if an error." ;; (define (make-address actor-id hive-id) - (cons actor-id hive-id)) + (vector actor-id hive-id)) (define (address-actor-id address) - (car address)) + (vector-ref address 0)) (define (address-hive-id address) - (cdr address)) + (vector-ref address 1)) (define (address->string address) (string-append (address-actor-id address) "@" @@ -349,19 +385,14 @@ raise an exception if an error." (define %current-actor (make-parameter #f)) +(define (actor-alive? actor) + (hive-resolve-local-actor (actor-hive actor) (actor-id actor))) + ;;; Actor utilities ;;; =============== -(define-syntax-rule (build-actions (symbol method) ...) - "Construct an alist of (symbol . method), where the method is wrapped -with wrap-apply to facilitate live hacking and allow the method definition -to come after class definition." - (list - (cons (quote symbol) - (wrap-apply method)) ...)) - (define-syntax-rule (define-simple-actor class action ...) (define-class class () (actions #:init-value (build-actions action ...) @@ -401,12 +432,25 @@ to come after class definition." (build-actions ;; This is in the case of an ambassador failing to forward a ;; message... it reports it back to the hive - (*failed-forward* hive-handle-failed-forward)))) + (*failed-forward* hive-handle-failed-forward) + (*cleanup-all* hive-handle-cleanup-all)))) (define-method (hive-handle-failed-forward (hive ) message) "Handle an ambassador failing to forward a message" 'TODO) +(define-method (hive-handle-cleanup-all (hive ) message) + "Send a message to all actors in our registry to clean themselves up." + ;; Unfortunately we have to do this hack and run over the list + ;; twice, because hash-for-each would result in an unrewindable + ;; continuation. + (define actor-ids + (hash-map->list (lambda (actor-id actor) actor-id) + (hive-actor-registry hive))) + (for-each (lambda (actor-id) + (<- actor-id '*cleanup*)) + actor-ids)) + (define* (make-hive #:key hive-id) (let ((hive (make #:id (make-address @@ -414,6 +458,8 @@ to come after class definition." (big-random-number-string)))))) ;; Set the hive's actor reference to itself (set! (actor-hive hive) hive) + ;; Register the actor with itself + (hive-register-actor! hive hive) hive)) (define-method (hive-id (hive )) @@ -421,7 +467,7 @@ to come after class definition." (define-method (hive-gen-actor-id (hive ) cookie) (make-address (if cookie - (string-append cookie "-" (big-random-number-string)) + (string-append cookie ":" (big-random-number-string)) (big-random-number-string)) (hive-id hive))) @@ -628,10 +674,13 @@ that method for documentation." actor-id)) (define* (hive-create-actor hive actor-class #:rest init) + "Create an actor on HIVE using ACTOR-CLASS passing in INIT args" (%hive-create-actor hive actor-class - init #f)) + init (symbol->string (class-name actor-class)))) (define* (hive-create-actor* hive actor-class id-cookie #:rest init) + "Create an actor, but also allow customizing a 'cookie' added to the id +for debugging" (%hive-create-actor hive actor-class init id-cookie)) @@ -685,8 +734,13 @@ Like create-actor, but permits supplying an id-cookie." init id-cookie)) -(define (self-destruct actor) - "Remove an actor from the hive." +(define* (self-destruct actor #:key (cleanup #t)) + "Remove an actor from the hive. + +Unless #:cleanup is set to #f, this will first have the actor handle +its '*cleanup* action handler." + (when cleanup + (<-wait (actor-id actor) '*cleanup*)) (hash-remove! (hive-actor-registry (actor-hive actor)) (actor-id actor))) @@ -695,26 +749,30 @@ Like create-actor, but permits supplying an id-cookie." ;;; 8sync bootstrap utilities ;;; ========================= -(define* (ez-run-hive hive initial-tasks #:key repl-server) - "Start up an agenda and run HIVE in it with INITIAL-TASKS. - -Should we start up a cooperative REPL for live hacking? REPL-SERVER -wants to know! You can pass it #t or #f, or if you want to specify a port, -an integer." - (let* ((queue (list->q initial-tasks)) - (agenda (make-agenda #:pre-unwind-handler print-error-and-continue - #:queue queue))) - (cond - ;; If repl-server is an integer, we'll use that as the port - ((integer? repl-server) - (spawn-and-queue-repl-server! agenda repl-server)) - (repl-server - (spawn-and-queue-repl-server! agenda))) - (start-agenda agenda))) +(define* (run-hive hive initial-tasks + #:key (cleanup #t)) + "Start up an agenda and run HIVE in it with INITIAL-TASKS." + (dynamic-wind + (const #f) + (lambda () + (let* ((queue (list->q initial-tasks)) + (agenda (make-agenda #:pre-unwind-handler print-error-and-continue + #:queue queue))) + (run-agenda agenda))) + ;; Run cleanup + (lambda () + (when cleanup + (run-hive-cleanup hive))))) + +(define (run-hive-cleanup hive) + (let ((queue (list->q (list (bootstrap-message hive (actor-id hive) + '*cleanup-all*))))) + (run-agenda + (make-agenda #:queue queue)))) (define (bootstrap-message hive to-id action . message-body-args) (wrap - (apply <- hive to-id action message-body-args))) + (apply <-* `(#:actor ,hive) to-id action message-body-args)))