| ... |
... |
@@ -12,10 +12,13 @@ module GHCi.Message.Async |
|
12
|
12
|
|
|
13
|
13
|
-- * Interface for async bidirectional RPC
|
|
14
|
14
|
, remoteAsyncReq, handleAsyncReq
|
|
15
|
|
- , remoteAsyncReqWithResponse
|
|
16
|
|
- , AsyncReqResult(..), getAsyncReqReply
|
|
17
|
15
|
, RemoteProcessException(..), DecodeAsyncReqException(..)
|
|
18
|
16
|
|
|
|
17
|
+ -- ** Lower-level req/reply interface
|
|
|
18
|
+ , AsyncReqResult(..)
|
|
|
19
|
+ , remoteAsyncReqWithResponse
|
|
|
20
|
+ , getAsyncReqReply
|
|
|
21
|
+
|
|
19
|
22
|
-- ** Mailboxes
|
|
20
|
23
|
, InboxId(..)
|
|
21
|
24
|
, newMailbox
|
| ... |
... |
@@ -203,11 +206,6 @@ instance Exception PipeClosedException |
|
203
|
206
|
-- All messages on the wire are 'IddMsg', which contain a correlation header
|
|
204
|
207
|
-- and identify whether they are requests or replies.
|
|
205
|
208
|
|
|
206
|
|
-_writePipe :: Pipe -> IddMsg -> IO ()
|
|
207
|
|
-_writePipe p msg = do
|
|
208
|
|
- var <- maybe (throwIO PipeWriteClosedException) pure =<< atomically (writePipeAsync p msg)
|
|
209
|
|
- either throwIO pure =<< atomically (takeTMVar var)
|
|
210
|
|
-
|
|
211
|
209
|
-- | Enqueues the message for writing.
|
|
212
|
210
|
--
|
|
213
|
211
|
-- The returned TMVar can be checked for the outcome.
|
| ... |
... |
@@ -270,58 +268,6 @@ remoteAsyncReq :: Pipe -> Put -> Maybe InboxId |
|
270
|
268
|
remoteAsyncReq pipe msg ii dispose = mask_ $
|
|
271
|
269
|
getAsyncReqReply (pure ()) dispose =<< remoteAsyncReqWithResponse pipe msg ii
|
|
272
|
270
|
|
|
273
|
|
-
|
|
274
|
|
-getAsyncReqReply :: IO ()
|
|
275
|
|
- -> (Either SomeException LB.ByteString -> IO ())
|
|
276
|
|
- -> AsyncReqResult
|
|
277
|
|
- -> IO (Either SomeException LB.ByteString)
|
|
278
|
|
-getAsyncReqReply middle dispose = \case
|
|
279
|
|
- NotWritten e -> throwIO e
|
|
280
|
|
- MaybeWritten written waitRes cancel ->
|
|
281
|
|
- (do atomically (written >>= either throwSTM pure)
|
|
282
|
|
- middle
|
|
283
|
|
- atomically waitRes)
|
|
284
|
|
- `onException` do
|
|
285
|
|
- -- Do not care about write outcome, since reply could be coming either way.
|
|
286
|
|
- void $ atomically cancel
|
|
287
|
|
- -- exception must be happening while stuck in waitRes at the latest,
|
|
288
|
|
- -- so waitRes can be called again.
|
|
289
|
|
- --
|
|
290
|
|
- -- We forkIO to wait, to keep cleanup bounded. If the pipe is
|
|
291
|
|
- -- irrecoverable it will close eventually and this thread will terminate.
|
|
292
|
|
- void $ forkIO (atomically waitRes >>= dispose)
|
|
293
|
|
-
|
|
294
|
|
-
|
|
295
|
|
-data AsyncReqResult
|
|
296
|
|
- = NotWritten PipeClosedException
|
|
297
|
|
- | MaybeWritten
|
|
298
|
|
- (STM (Either SomeException ())) -- ^ block for write outcome
|
|
299
|
|
- (STM (Either SomeException LB.ByteString)) -- ^ block for reply
|
|
300
|
|
- (STM (Maybe (STM (Either SomeException ())))) -- ^ send cancel
|
|
301
|
|
-
|
|
302
|
|
-
|
|
303
|
|
-remoteAsyncReqWithResponse :: Pipe
|
|
304
|
|
- -> Put
|
|
305
|
|
- -> Maybe InboxId
|
|
306
|
|
- -> IO AsyncReqResult
|
|
307
|
|
-remoteAsyncReqWithResponse pipe msg ii = do
|
|
308
|
|
- -- Mark pending request
|
|
309
|
|
- uq <- freshId (pipeAsyncMngr pipe)
|
|
310
|
|
- wv <- newEmptyTMVarIO
|
|
311
|
|
-
|
|
312
|
|
- atomically $ readTVar (pipeReadStatus pipe) >>= \ readEnd -> case isActive readEnd of
|
|
313
|
|
- False -> pure $ NotWritten PipeReadClosedException
|
|
314
|
|
- True -> writePipeAsync pipe (Request uq ii (runPut msg)) >>= \case
|
|
315
|
|
- Nothing -> pure $ NotWritten PipeWriteClosedException
|
|
316
|
|
- Just waitWritten -> do
|
|
317
|
|
- -- request queued, so we register a pending reply. After this tx we
|
|
318
|
|
- -- always expect a reply, and when it arrives 'drainPipe' will clean up 'pendingReplies'.
|
|
319
|
|
- modifyTVar pendingReplies (IntMap.insert uq wv)
|
|
320
|
|
- pure (MaybeWritten (takeTMVar waitWritten) (takeTMVar wv)
|
|
321
|
|
- (fmap takeTMVar <$> writePipeAsync pipe (Cancel uq)))
|
|
322
|
|
- where
|
|
323
|
|
- pendingReplies = pendingReply (pipeAsyncMngr pipe)
|
|
324
|
|
-
|
|
325
|
271
|
-- | Receive a single asynchronous request and send a matching reply
|
|
326
|
272
|
-- asynchronously (the handler may fork a computation that eventually replies
|
|
327
|
273
|
-- and can return before actually replying, as long as it eventually does).
|
| ... |
... |
@@ -384,6 +330,10 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
|
384
|
330
|
void $ reply (Left e)
|
|
385
|
331
|
throwIO e
|
|
386
|
332
|
|
|
|
333
|
+-- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@
|
|
|
334
|
+newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
|
|
|
335
|
+instance Exception DecodeAsyncReqException
|
|
|
336
|
+
|
|
387
|
337
|
replyToReqAsync :: Pipe -> CorrelationId -> Either SomeException Put
|
|
388
|
338
|
-> STM (Maybe (TMVar (Either SomeException ())))
|
|
389
|
339
|
replyToReqAsync pipe uq p = do
|
| ... |
... |
@@ -392,15 +342,61 @@ replyToReqAsync pipe uq p = do |
|
392
|
342
|
where
|
|
393
|
343
|
am = pipeAsyncMngr pipe
|
|
394
|
344
|
|
|
395
|
|
-_replyToReq :: Pipe -> CorrelationId -> Either SomeException Put -> IO ()
|
|
396
|
|
-_replyToReq pipe uq p = atomically (replyToReqAsync pipe uq p) >>= \case
|
|
397
|
|
- Nothing -> throwIO PipeWriteClosedException
|
|
398
|
|
- Just written -> atomically (readTMVar written) >>= either throwIO pure
|
|
|
345
|
+-- ** Lower-level req/reply interface ------------------------------------------
|
|
399
|
346
|
|
|
400
|
|
--- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@
|
|
401
|
|
-newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
|
|
402
|
|
-instance Exception DecodeAsyncReqException
|
|
|
347
|
+data AsyncReqResult
|
|
|
348
|
+ = NotWritten PipeClosedException
|
|
|
349
|
+ | MaybeWritten
|
|
|
350
|
+ (STM (Either SomeException ())) -- ^ block for write outcome
|
|
|
351
|
+ (STM (Either SomeException LB.ByteString)) -- ^ block for reply
|
|
|
352
|
+ (STM (Maybe (STM (Either SomeException ())))) -- ^ send cancel
|
|
|
353
|
+
|
|
|
354
|
+-- | Like 'remoteAsyncReq', but instead of blocking waiting for a reply, return
|
|
|
355
|
+-- the 'AsyncReqResult' which can be given to 'getAsyncReqReply' to block
|
|
|
356
|
+-- waiting for a reply later.
|
|
|
357
|
+remoteAsyncReqWithResponse :: Pipe
|
|
|
358
|
+ -> Put
|
|
|
359
|
+ -> Maybe InboxId
|
|
|
360
|
+ -> IO AsyncReqResult
|
|
|
361
|
+remoteAsyncReqWithResponse pipe msg ii = do
|
|
|
362
|
+ -- Mark pending request
|
|
|
363
|
+ uq <- freshId (pipeAsyncMngr pipe)
|
|
|
364
|
+ wv <- newEmptyTMVarIO
|
|
|
365
|
+
|
|
|
366
|
+ atomically $ readTVar (pipeReadStatus pipe) >>= \ readEnd -> case isActive readEnd of
|
|
|
367
|
+ False -> pure $ NotWritten PipeReadClosedException
|
|
|
368
|
+ True -> writePipeAsync pipe (Request uq ii (runPut msg)) >>= \case
|
|
|
369
|
+ Nothing -> pure $ NotWritten PipeWriteClosedException
|
|
|
370
|
+ Just waitWritten -> do
|
|
|
371
|
+ -- request queued, so we register a pending reply. After this tx we
|
|
|
372
|
+ -- always expect a reply, and when it arrives 'drainPipe' will clean up 'pendingReplies'.
|
|
|
373
|
+ modifyTVar pendingReplies (IntMap.insert uq wv)
|
|
|
374
|
+ pure (MaybeWritten (takeTMVar waitWritten) (takeTMVar wv)
|
|
|
375
|
+ (fmap takeTMVar <$> writePipeAsync pipe (Cancel uq)))
|
|
|
376
|
+ where
|
|
|
377
|
+ pendingReplies = pendingReply (pipeAsyncMngr pipe)
|
|
403
|
378
|
|
|
|
379
|
+-- | Block waiting for the given 'AsyncReqResult': a result which will be
|
|
|
380
|
+-- delivered asynchronously in reply to a 'remoteAsyncReqWithResponse' request.
|
|
|
381
|
+getAsyncReqReply :: IO ()
|
|
|
382
|
+ -> (Either SomeException LB.ByteString -> IO ())
|
|
|
383
|
+ -> AsyncReqResult
|
|
|
384
|
+ -> IO (Either SomeException LB.ByteString)
|
|
|
385
|
+getAsyncReqReply middle dispose = \case
|
|
|
386
|
+ NotWritten e -> throwIO e
|
|
|
387
|
+ MaybeWritten written waitRes cancel ->
|
|
|
388
|
+ (do atomically (written >>= either throwSTM pure)
|
|
|
389
|
+ middle
|
|
|
390
|
+ atomically waitRes)
|
|
|
391
|
+ `onException` do
|
|
|
392
|
+ -- Do not care about write outcome, since reply could be coming either way.
|
|
|
393
|
+ void $ atomically cancel
|
|
|
394
|
+ -- exception must be happening while stuck in waitRes at the latest,
|
|
|
395
|
+ -- so waitRes can be called again.
|
|
|
396
|
+ --
|
|
|
397
|
+ -- We forkIO to wait, to keep cleanup bounded. If the pipe is
|
|
|
398
|
+ -- irrecoverable it will close eventually and this thread will terminate.
|
|
|
399
|
+ void $ forkIO (atomically waitRes >>= dispose)
|
|
404
|
400
|
|
|
405
|
401
|
-- ** Mailboxes ----------------------------------------------------------------
|
|
406
|
402
|
|
| ... |
... |
@@ -640,11 +636,9 @@ receivedRequest am uq = do |
|
640
|
636
|
Just Canceling -> error "impossible"
|
|
641
|
637
|
|
|
642
|
638
|
|
|
643
|
|
-
|
|
644
|
639
|
{-
|
|
645
|
640
|
Note [Exceptions during serialization]
|
|
646
|
|
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
647
|
|
-
|
|
|
641
|
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
648
|
642
|
For efficiency reasons, any `IddMsg` to be sent is forced only when already
|
|
649
|
643
|
writing onto the Pipe, so we cannot discern whether a partial write already
|
|
650
|
644
|
occurred. The only option then is to tear down the write end of the pipe.
|