Support request timeouts in the thread pool
This commit is contained in:
parent
5bb7cf0c1c
commit
638e0442c3
1 changed files with 39 additions and 12 deletions
|
|
@ -32,6 +32,7 @@
|
||||||
prevent-inlining-for-tests
|
prevent-inlining-for-tests
|
||||||
|
|
||||||
thread-pool-channel
|
thread-pool-channel
|
||||||
|
thread-pool-request-timeout
|
||||||
make-thread-pool-channel
|
make-thread-pool-channel
|
||||||
parallel-via-thread-pool-channel
|
parallel-via-thread-pool-channel
|
||||||
par-map&
|
par-map&
|
||||||
|
|
@ -129,30 +130,56 @@
|
||||||
(iota threads))
|
(iota threads))
|
||||||
channel))
|
channel))
|
||||||
|
|
||||||
|
(define &thread-pool-request-timeout
|
||||||
|
(make-exception-type '&thread-pool-request-timeout
|
||||||
|
&error
|
||||||
|
'()))
|
||||||
|
|
||||||
|
(define make-thread-pool-request-timeout-error
|
||||||
|
(record-constructor &thread-pool-request-timeout))
|
||||||
|
|
||||||
|
(define thread-pool-request-timeout-error?
|
||||||
|
(record-predicate &thread-pool-request-timeout))
|
||||||
|
|
||||||
(define thread-pool-channel
|
(define thread-pool-channel
|
||||||
(make-parameter #f))
|
(make-parameter #f))
|
||||||
|
|
||||||
|
(define thread-pool-request-timeout
|
||||||
|
(make-parameter #f))
|
||||||
|
|
||||||
(define (defer-to-thread-pool-channel thunk)
|
(define (defer-to-thread-pool-channel thunk)
|
||||||
(let ((reply (make-channel)))
|
(let ((reply (make-channel)))
|
||||||
(spawn-fiber
|
(spawn-fiber
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(put-message (thread-pool-channel)
|
(let ((val
|
||||||
|
(perform-operation
|
||||||
|
(let ((put
|
||||||
|
(wrap-operation
|
||||||
|
(put-operation (thread-pool-channel)
|
||||||
(list reply
|
(list reply
|
||||||
(get-internal-real-time)
|
(get-internal-real-time)
|
||||||
thunk))))
|
thunk))
|
||||||
|
(const 'success))))
|
||||||
|
(or
|
||||||
|
(and=> (thread-pool-request-timeout)
|
||||||
|
(lambda (timeout)
|
||||||
|
(choice-operation
|
||||||
|
put
|
||||||
|
(wrap-operation (sleep-operation timeout)
|
||||||
|
(const 'request-timeout)))))
|
||||||
|
put)))))
|
||||||
|
(when (eq? val 'request-timeout)
|
||||||
|
(put-message reply val)))))
|
||||||
reply))
|
reply))
|
||||||
|
|
||||||
(define (fetch-result-of-defered-thunk reply-channel)
|
|
||||||
(match (get-message reply-channel)
|
|
||||||
(('worker-thread-error . exn)
|
|
||||||
(raise-exception exn))
|
|
||||||
(result
|
|
||||||
(apply values result))))
|
|
||||||
|
|
||||||
(define (fetch-result-of-defered-thunks . reply-channels)
|
(define (fetch-result-of-defered-thunks . reply-channels)
|
||||||
(let ((responses (map get-message reply-channels)))
|
(let ((responses (map get-message
|
||||||
|
reply-channels)))
|
||||||
(map
|
(map
|
||||||
(match-lambda
|
(match-lambda
|
||||||
|
('request-timeout
|
||||||
|
(raise-exception
|
||||||
|
(make-thread-pool-request-timeout-error)))
|
||||||
(('worker-thread-error . exn)
|
(('worker-thread-error . exn)
|
||||||
(raise-exception exn))
|
(raise-exception exn))
|
||||||
(result
|
(result
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue