| ... |
... |
@@ -160,14 +160,19 @@ instance Exception PipeWriteClosedException |
|
160
|
160
|
-- and identify whether they are requests or replies.
|
|
161
|
161
|
|
|
162
|
162
|
writePipe :: Pipe -> IddMsg -> IO ()
|
|
163
|
|
-writePipe Pipe{..} msg = do
|
|
164
|
|
- var <- newEmptyTMVarIO
|
|
165
|
|
- atomically $ do
|
|
|
163
|
+writePipe p msg = do
|
|
|
164
|
+ var <- atomically $ writePipeAsync p msg
|
|
|
165
|
+ either throwIO pure =<< atomically (takeTMVar var)
|
|
|
166
|
+
|
|
|
167
|
+writePipeAsync :: Pipe -> IddMsg -> STM (TMVar (Either SomeException ()))
|
|
|
168
|
+writePipeAsync Pipe{..} msg = do
|
|
|
169
|
+ var <- newEmptyTMVar
|
|
166
|
170
|
readTVar pipeWriteClosed >>= \case
|
|
167
|
171
|
True -> throwSTM PipeWriteClosedException
|
|
168
|
172
|
False -> pure ()
|
|
169
|
173
|
writeTQueue (writeQueue pipeAsyncMngr) (msg, var)
|
|
170
|
|
- either throwIO pure =<< atomically (takeTMVar var)
|
|
|
174
|
+ pure var
|
|
|
175
|
+
|
|
171
|
176
|
|
|
172
|
177
|
-- | Only the 'drainPipe' thread should read from the pipe directly. Other
|
|
173
|
178
|
-- functions should wait for the matching reply to be delivered asynchronously.
|
| ... |
... |
@@ -239,14 +244,18 @@ remoteAsyncReq pipe msg ii = do |
|
239
|
244
|
-- Write request
|
|
240
|
245
|
writePipe pipe (Request uq ii (runPut msg))
|
|
241
|
246
|
`onException`
|
|
242
|
|
- -- the req wasn't done, or no one will be reading the reply:
|
|
243
|
|
- -- in either case, drop it from the pending map.
|
|
244
|
|
- atomically (modifyTVar pendingReplies (IntMap.delete uq))
|
|
|
247
|
+ -- the req wasn't done, or no one will be reading the reply:
|
|
|
248
|
+ -- in either case, drop it from the pending map.
|
|
|
249
|
+ atomically (modifyTVar pendingReplies (IntMap.delete uq))
|
|
245
|
250
|
|
|
246
|
251
|
-- Block waiting for reply
|
|
247
|
252
|
atomically (takeTMVar wv)
|
|
248
|
253
|
`onException`
|
|
249
|
|
- atomically (modifyTVar pendingReplies (IntMap.delete uq))
|
|
|
254
|
+ atomically (do
|
|
|
255
|
+ modifyTVar pendingReplies (IntMap.delete uq)
|
|
|
256
|
+ -- we don't wait for Cancel to be written on the pipe,
|
|
|
257
|
+ -- and won't get a reply: nowhere to report it anyway.
|
|
|
258
|
+ writePipeAsync pipe (Cancel uq))
|
|
250
|
259
|
|
|
251
|
260
|
-- | Receive a single asynchronous request and send a matching reply
|
|
252
|
261
|
-- asynchronously (the handler may fork a computation that eventually replies
|
| ... |
... |
@@ -258,7 +267,7 @@ remoteAsyncReq pipe msg ii = do |
|
258
|
267
|
-- branch, and if the handler otherwise throws, it is handled by 'handleAsyncReq'.
|
|
259
|
268
|
--
|
|
260
|
269
|
-- See Note [Exceptions during serialization] about exceptions the 'Put' argument might raise.
|
|
261
|
|
-handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> a -> IO r) -> IO r
|
|
|
270
|
+handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> (IO () -> IO ()) -> a -> IO r) -> IO r
|
|
262
|
271
|
handleAsyncReq pipe getit mii k = mask $ \restore -> do
|
|
263
|
272
|
let am = pipeAsyncMngr pipe
|
|
264
|
273
|
ack <- newEmptyMVar
|
| ... |
... |
@@ -274,7 +283,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
|
274
|
283
|
Nothing -> tryReadTQueue $ requestsQueue am
|
|
275
|
284
|
Just ii -> tryReadTQueue =<< getMailbox am ii
|
|
276
|
285
|
case taken of
|
|
277
|
|
- Just req -> pure (Just req)
|
|
|
286
|
+ Just req@(uq,_) -> fetchedRequest am uq >> pure (Just req)
|
|
278
|
287
|
Nothing | isClosed -> pure Nothing -- empty and closed
|
|
279
|
288
|
| otherwise -> retry -- wait for msg
|
|
280
|
289
|
|
| ... |
... |
@@ -282,9 +291,9 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
|
282
|
291
|
Nothing -> throwIO PipeReadClosedException -- can't expect reqs
|
|
283
|
292
|
Just (uq, bs) -> do
|
|
284
|
293
|
let reply p = do
|
|
285
|
|
- writePipe pipe (Reply uq (bimap toSerializableException runPut p))
|
|
286
|
|
- putMVar ack ()
|
|
287
|
|
-
|
|
|
294
|
+ b <- atomically $ doneHandling am uq
|
|
|
295
|
+ when b $ writePipe pipe (Reply uq (bimap toSerializableException runPut p))
|
|
|
296
|
+ putMVar ack () -- TODO: superfluous?
|
|
288
|
297
|
-- Run the continuation which must reply exactly once with 'writeReply'
|
|
289
|
298
|
case runGetOrFail getit bs of
|
|
290
|
299
|
Left (_, _, err) -> do
|
| ... |
... |
@@ -292,7 +301,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
|
292
|
301
|
reply (Left (toException exc))
|
|
293
|
302
|
throwIO exc
|
|
294
|
303
|
Right (_, _, it) ->
|
|
295
|
|
- restore (k (WriteReply reply) it)
|
|
|
304
|
+ restore (k (WriteReply reply) (setActive am uq) it)
|
|
296
|
305
|
|
|
297
|
306
|
-- If there's an exception before the reply is sent, reply with an error instead.
|
|
298
|
307
|
`catch` \(e :: SomeException) -> do
|
| ... |
... |
@@ -306,6 +315,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
|
306
|
315
|
reply (Left e)
|
|
307
|
316
|
throwIO e
|
|
308
|
317
|
|
|
|
318
|
+
|
|
309
|
319
|
-- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@
|
|
310
|
320
|
newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
|
|
311
|
321
|
instance Exception DecodeAsyncReqException
|
| ... |
... |
@@ -347,6 +357,15 @@ getMailbox am (InboxId ii) = do |
|
347
|
357
|
|
|
348
|
358
|
-- ** Async matching "internals" -----------------------------------------------
|
|
349
|
359
|
|
|
|
360
|
+-- | Requests go
|
|
|
361
|
+-- requestsQueue -> Fetched -> Active cancel -> gone
|
|
|
362
|
+-- .. -> Canceling -> gone
|
|
|
363
|
+-- Canceling is needed to avoid race condition in gap between Fetched and Active.
|
|
|
364
|
+data RequestStatus
|
|
|
365
|
+ = Fetched
|
|
|
366
|
+ | Active (IO ()) -- ^ must not be blocking.
|
|
|
367
|
+ | Canceling
|
|
|
368
|
+
|
|
350
|
369
|
data AsyncManager = AsyncManager
|
|
351
|
370
|
{ pendingReply :: !(TVar (IntMap (TMVar (Either SomeException LB.ByteString))))
|
|
352
|
371
|
-- ^ When a request is sent, keep track that we're expecting a reply. The
|
| ... |
... |
@@ -357,6 +376,9 @@ data AsyncManager = AsyncManager |
|
357
|
376
|
-- /without/ a specific 'InboxId', queue it here. A request handler will
|
|
358
|
377
|
-- pop the next request from it. (Nothing except the 'drainPipe' thread
|
|
359
|
378
|
-- should read from the pipe). Read with @'handleAsyncReq' p get Nothing@.
|
|
|
379
|
+ , cancelStatus :: !(TVar (IntMap RequestStatus))
|
|
|
380
|
+ -- ^ When a `Cancel uq` is received, we lookup `uq` in this map and call the
|
|
|
381
|
+ -- callback if present.
|
|
360
|
382
|
, mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString))))
|
|
361
|
383
|
-- ^ Requests for a specific 'InboxId' that can be read using
|
|
362
|
384
|
-- @'handleAsyncReq' p get (Just <mailboxid>)@.
|
| ... |
... |
@@ -389,15 +411,21 @@ type CorrelationId = Int |
|
389
|
411
|
-- exclusively.
|
|
390
|
412
|
data IddMsg = Request !CorrelationId !(Maybe InboxId) LB.ByteString
|
|
391
|
413
|
| Reply !CorrelationId (Either SerializableException LB.ByteString)
|
|
|
414
|
+ | Cancel !CorrelationId -- !(Maybe InboxId) --? ^ Technically a
|
|
|
415
|
+ -- Request, but no Reply is expected, the CorrelationId is the
|
|
|
416
|
+ -- one of the Request being canceled.
|
|
392
|
417
|
|
|
393
|
418
|
instance Binary IddMsg where
|
|
394
|
419
|
put (Request i ii bs) = putWord8 0 >> put i >> put ii >> put bs
|
|
395
|
420
|
put (Reply i bs) = putWord8 1 >> put i >> put bs
|
|
|
421
|
+ put (Cancel i) = putWord8 2 >> put i
|
|
|
422
|
+
|
|
396
|
423
|
get = do
|
|
397
|
424
|
b <- getWord8
|
|
398
|
425
|
case b of
|
|
399
|
426
|
0 -> Request <$> get <*> get <*> get
|
|
400
|
427
|
1 -> Reply <$> get <*> get
|
|
|
428
|
+ 2 -> Cancel <$> get
|
|
401
|
429
|
_ -> fail $ "impossible: Binary IddMsg - " ++ show b
|
|
402
|
430
|
|
|
403
|
431
|
newAsyncManager :: Pipe -> IO AsyncManager
|
| ... |
... |
@@ -405,6 +433,7 @@ newAsyncManager pipe = do |
|
405
|
433
|
asyncFreshId <- newIORef 0
|
|
406
|
434
|
pendingReply <- newTVarIO IntMap.empty
|
|
407
|
435
|
requestsQueue <- newTQueueIO
|
|
|
436
|
+ cancelStatus <- newTVarIO IntMap.empty
|
|
408
|
437
|
mailboxQueues <- newTVarIO IntMap.empty
|
|
409
|
438
|
writeQueue <- newTQueueIO
|
|
410
|
439
|
|
| ... |
... |
@@ -430,41 +459,84 @@ drainPipe :: Pipe -> (forall x. IO x -> IO x) -> IO () |
|
430
|
459
|
drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPendings where
|
|
431
|
460
|
loop = do
|
|
432
|
461
|
readPipe p >>= \case
|
|
433
|
|
- -- Receive a reply to a pending notification
|
|
434
|
|
- Just (Reply uq bs) -> do
|
|
435
|
|
- atomically $ do
|
|
436
|
|
- m <- readTVar (pendingReply am)
|
|
437
|
|
- case IntMap.lookup uq m of
|
|
438
|
|
- Nothing ->
|
|
439
|
|
- -- the req was dropped from pending bc there was an exception, and no
|
|
440
|
|
- -- one's listening any more. the reply is ignored
|
|
441
|
|
- pure ()
|
|
442
|
|
- Just wv -> do
|
|
443
|
|
- writeTVar (pendingReply am) (IntMap.delete uq m)
|
|
444
|
|
- -- signal matching request
|
|
445
|
|
- putTMVar wv (bimap (toException . RemoteProcessException) id bs)
|
|
446
|
|
- loop
|
|
447
|
|
- -- Receive a request and queue it
|
|
448
|
|
- Just (Request uq mbi bs) -> do
|
|
449
|
|
- atomically $ case mbi of
|
|
450
|
|
- Nothing ->
|
|
451
|
|
- -- "Global" request
|
|
452
|
|
- writeTQueue (requestsQueue am) (uq,bs)
|
|
453
|
|
- Just ii -> do
|
|
454
|
|
- -- "Specific mailbox" request
|
|
455
|
|
- q <- getMailbox am ii
|
|
456
|
|
- writeTQueue q (uq,bs)
|
|
|
462
|
+ Just msg -> do
|
|
|
463
|
+ handleMsg msg
|
|
457
|
464
|
loop
|
|
458
|
465
|
-- EOF on read handle
|
|
459
|
466
|
Nothing -> do
|
|
460
|
467
|
-- clean up pipe then exit loop
|
|
461
|
468
|
replyToPendings (toException PipeReadClosedException)
|
|
462
|
469
|
pure ()
|
|
|
470
|
+ handleMsg = \case
|
|
|
471
|
+ -- Receive a reply to a pending notification
|
|
|
472
|
+ Reply uq bs -> do
|
|
|
473
|
+ atomically $ do
|
|
|
474
|
+ m <- readTVar (pendingReply am)
|
|
|
475
|
+ case IntMap.lookup uq m of
|
|
|
476
|
+ Nothing ->
|
|
|
477
|
+ -- the req was dropped from pending bc there was an exception, and no
|
|
|
478
|
+ -- one's listening any more. the reply is ignored
|
|
|
479
|
+ pure ()
|
|
|
480
|
+ Just wv -> do
|
|
|
481
|
+ writeTVar (pendingReply am) (IntMap.delete uq m)
|
|
|
482
|
+ -- signal matching request
|
|
|
483
|
+ putTMVar wv (bimap (toException . RemoteProcessException) id bs)
|
|
|
484
|
+ -- Receive a request and queue it
|
|
|
485
|
+ Request uq mbi bs -> do
|
|
|
486
|
+ atomically $ case mbi of
|
|
|
487
|
+ Nothing ->
|
|
|
488
|
+ -- "Global" request
|
|
|
489
|
+ writeTQueue (requestsQueue am) (uq,bs)
|
|
|
490
|
+ Just ii -> do
|
|
|
491
|
+ -- "Specific mailbox" request
|
|
|
492
|
+ q <- getMailbox am ii
|
|
|
493
|
+ writeTQueue q (uq,bs)
|
|
|
494
|
+ Cancel uq -> cancelRequest am uq
|
|
463
|
495
|
|
|
464
|
496
|
replyToPendings e = atomically $ failPendingReplies p e
|
|
465
|
497
|
|
|
466
|
498
|
am = pipeAsyncMngr p
|
|
467
|
499
|
|
|
|
500
|
+cancelRequest :: AsyncManager -> CorrelationId -> IO ()
|
|
|
501
|
+cancelRequest am uq = join . atomically $ do
|
|
|
502
|
+ cs <- readTVar (cancelStatus am)
|
|
|
503
|
+ case IntMap.lookup uq cs of
|
|
|
504
|
+ Nothing -> pure (pure ())
|
|
|
505
|
+ Just Fetched -> writeTVar (cancelStatus am) (IntMap.insert uq Canceling cs) >> pure (pure ())
|
|
|
506
|
+ Just (Active cancel) -> writeTVar (cancelStatus am) (IntMap.delete uq cs) >> pure cancel
|
|
|
507
|
+ Just Canceling -> pure (pure ())
|
|
|
508
|
+
|
|
|
509
|
+doneHandling :: AsyncManager -> CorrelationId -> STM Bool
|
|
|
510
|
+doneHandling am uq = do
|
|
|
511
|
+ cs <- readTVar (cancelStatus am)
|
|
|
512
|
+ case IntMap.lookup uq cs of
|
|
|
513
|
+ Nothing -> pure False
|
|
|
514
|
+ Just s -> do
|
|
|
515
|
+ writeTVar (cancelStatus am) $ IntMap.delete uq cs
|
|
|
516
|
+ case s of
|
|
|
517
|
+ Canceling -> pure False
|
|
|
518
|
+ _ -> pure True
|
|
|
519
|
+
|
|
|
520
|
+setActive :: AsyncManager -> CorrelationId -> IO () -> IO ()
|
|
|
521
|
+setActive am uq cancel = join . atomically $ do
|
|
|
522
|
+ cs <- readTVar (cancelStatus am)
|
|
|
523
|
+ case IntMap.lookup uq cs of
|
|
|
524
|
+ Nothing -> pure (pure ()) -- reply already received.
|
|
|
525
|
+ Just Fetched -> writeTVar (cancelStatus am) (IntMap.insert uq (Active cancel) cs) >> pure (pure ())
|
|
|
526
|
+ Just (Active _) -> writeTVar (cancelStatus am) (IntMap.insert uq (Active cancel) cs) >> pure (pure ())
|
|
|
527
|
+ Just Canceling -> pure cancel
|
|
|
528
|
+
|
|
|
529
|
+fetchedRequest :: AsyncManager -> CorrelationId -> STM ()
|
|
|
530
|
+fetchedRequest am uq = do
|
|
|
531
|
+ cs <- readTVar (cancelStatus am)
|
|
|
532
|
+ case IntMap.lookup uq cs of
|
|
|
533
|
+ Nothing -> writeTVar (cancelStatus am) (IntMap.insert uq Fetched cs)
|
|
|
534
|
+ Just Fetched -> error "impossible"
|
|
|
535
|
+ Just (Active _) -> error "impossible"
|
|
|
536
|
+ Just Canceling -> error "impossible"
|
|
|
537
|
+
|
|
|
538
|
+
|
|
|
539
|
+
|
|
468
|
540
|
{-
|
|
469
|
541
|
Note [Exceptions during serialization]
|
|
470
|
542
|
-----------------------------------------------------------------------------
|
| ... |
... |
@@ -553,7 +625,7 @@ runWriteReply (WriteReply r) = r |
|
553
|
625
|
(|#>) (IO fs) s = fs s
|
|
554
|
626
|
|
|
555
|
627
|
(%>>=) :: (IO a) -> (a -> IO b) %1 -> IO b
|
|
556
|
|
-(%>>=) = \ m f -> IO (\s0 ->
|
|
|
628
|
+(%>>=) = \ m -> \ f -> IO (\s0 ->
|
|
557
|
629
|
case m |#> s0 of -- unIO for linearity checking
|
|
558
|
630
|
(# s1, x #) -> f x |#> s1)
|
|
559
|
631
|
infixl 1 %>>=
|