Make delete-role idempotent

This commit is contained in:
Tony Garnock-Jones 2012-03-24 15:59:52 -04:00
parent c278e10673
commit 523446fa71
1 changed files with 27 additions and 24 deletions

51
os2.rkt
View File

@ -193,30 +193,33 @@
hs))])) hs))]))
(define (do-unsubscribe pid eid reason state) (define (do-unsubscribe pid eid reason state)
(define endpoint-to-remove (hash-ref (vm-endpoints state) eid)) (cond
(define removed-topic (endpoint-topic endpoint-to-remove)) [(hash-has-key? (vm-endpoints state) eid)
(define old-process (hash-ref (vm-processes state) pid)) (define endpoint-to-remove (hash-ref (vm-endpoints state) eid))
(define new-process (struct-copy process old-process (define removed-topic (endpoint-topic endpoint-to-remove))
[endpoints (set-remove (process-endpoints old-process) eid)])) (define old-process (hash-ref (vm-processes state) pid))
(let ((state (struct-copy vm state (define new-process (struct-copy process old-process
[endpoints (hash-remove (vm-endpoints state) eid)] [endpoints (set-remove (process-endpoints old-process) eid)]))
[processes (if (set-empty? (process-endpoints new-process)) (let ((state (struct-copy vm state
(hash-remove (vm-processes state) pid) [endpoints (hash-remove (vm-endpoints state) eid)]
(hash-set (vm-processes state) pid new-process))]))) [processes (if (set-empty? (process-endpoints new-process))
(for*/fold ([state state]) (hash-remove (vm-processes state) pid)
([(matching-pid p) (in-hash (vm-processes state))] (hash-set (vm-processes state) pid new-process))])))
[matching-eid (in-set (process-endpoints p))] (for*/fold ([state state])
[e (in-value (hash-ref (vm-endpoints state) matching-eid))] ([(matching-pid p) (in-hash (vm-processes state))]
[matching-topic (in-value (endpoint-topic e))] [matching-eid (in-set (process-endpoints p))]
[flow-pattern (in-value (topic-intersection removed-topic matching-topic))] [e (in-value (hash-ref (vm-endpoints state) matching-eid))]
#:when flow-pattern) [matching-topic (in-value (endpoint-topic e))]
(define outbound-flow (refine-topic removed-topic flow-pattern)) [flow-pattern (in-value (topic-intersection removed-topic matching-topic))]
(run-trapk state #:when flow-pattern)
matching-pid (define outbound-flow (refine-topic removed-topic flow-pattern))
(handlers-absence (endpoint-handlers e)) (run-trapk state
matching-eid matching-pid
outbound-flow (handlers-absence (endpoint-handlers e))
reason)))) matching-eid
outbound-flow
reason)))]
[else state]))
(define (route-and-deliver message-topic body state) (define (route-and-deliver message-topic body state)
(define pids-and-endpoints (define pids-and-endpoints