diff options
author | Christopher Baines <mail@cbaines.net> | 2023-11-10 13:51:46 +0000 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2023-11-10 13:51:46 +0000 |
commit | 67c8ede1c53810fdd47039d9796920ef07b60849 (patch) | |
tree | f7ece6d9da3ffb9c5d50ef8234212831c34d5f16 /guix-build-coordinator/utils | |
parent | 34463558e589aa260b15e53422652a37848aec95 (diff) | |
download | build-coordinator-67c8ede1c53810fdd47039d9796920ef07b60849.tar build-coordinator-67c8ede1c53810fdd47039d9796920ef07b60849.tar.gz |
Add a with-fibers-timeout utility
Diffstat (limited to 'guix-build-coordinator/utils')
-rw-r--r-- | guix-build-coordinator/utils/fibers.scm | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/guix-build-coordinator/utils/fibers.scm b/guix-build-coordinator/utils/fibers.scm index c9a04fb..3dc612c 100644 --- a/guix-build-coordinator/utils/fibers.scm +++ b/guix-build-coordinator/utils/fibers.scm @@ -17,7 +17,9 @@ run-server/patched - letpar&)) + letpar& + + with-fibers-timeout)) (define %worker-thread-args (make-parameter #f)) @@ -331,3 +333,34 @@ If already in the worker thread, call PROC immediately." (lambda () (parallel-via-fibers e ...)) (lambda (v ...) b0 b1 ...))) + +(define* (with-fibers-timeout thunk #:key timeout on-timeout) + (let ((channel (make-channel))) + (spawn-fiber + (lambda () + (with-exception-handler + (lambda (exn) + (perform-operation + (choice-operation + (put-operation channel (cons 'exception exn)) + (sleep-operation timeout)))) + (lambda () + (call-with-values thunk + (lambda vals + (perform-operation + (choice-operation + (put-operation channel vals) + (sleep-operation timeout)))))) + #:unwind? #t))) + + (match (perform-operation + (choice-operation + (get-operation channel) + (wrap-operation (sleep-operation timeout) + (const 'timeout)))) + ('timeout + (on-timeout)) + (('exception . exn) + (raise-exception exn)) + (vals + (apply values vals))))) |