@@ -83,6 +83,12 @@ import Control.Distributed.Process.Internal.StrictMVar
84 , takeMVar
84 , takeMVar
86+import Control.Distributed.Process.Internal.ThreadPool
86import Control.Concurrent.Chan (newChan, writeChan, readChan)
92import Control.Concurrent.Chan (newChan, writeChan, readChan)
87import qualified Control.Concurrent.MVar as MVar (newEmptyMVar, takeMVar)
93import qualified Control.Concurrent.MVar as MVar (newEmptyMVar, takeMVar)
88import Control.Concurrent.STM
94import Control.Concurrent.STM
@@ -249,12 +255,14 @@ createBareLocalNode endPoint rtable = do
249 , _localConnections = Map.empty
255 , _localConnections = Map.empty
251 ctrlChan <- newChan
257 ctrlChan <- newChan
258+ sendPool <- newThreadPool
252 let node = LocalNode { localNodeId = NodeId $ NT.address endPoint
259 let node = LocalNode { localNodeId = NodeId $ NT.address endPoint
253 , localEndPoint = endPoint
260 , localEndPoint = endPoint
254 , localState = state
261 , localState = state
255 , localCtrlChan = ctrlChan
262 , localCtrlChan = ctrlChan
256 , localEventBus = MxEventBusInitialising
263 , localEventBus = MxEventBusInitialising
257 , remoteTable = rtable
264 , remoteTable = rtable
265+ , localSendPool = sendPool
259 tracedNode <- startMxAgent node
267 tracedNode <- startMxAgent node
@@ -580,6 +588,8 @@ handleIncomingMessages node = go initConnectionState
580 , ctrlMsgSignal = Died nid DiedDisconnect
588 , ctrlMsgSignal = Died nid DiedDisconnect
582 let notLost k = not (k `Set.member` (st ^. incomingFrom theirAddr))
590 let notLost k = not (k `Set.member` (st ^. incomingFrom theirAddr))
591+ liftIO $ lookupWorker (localSendPool node) (NodeId theirAddr)
592+ >>= maybe (return ()) killThread
583 closeImplicitReconnections node nid
593 closeImplicitReconnections node nid
584 go ( (incomingFrom theirAddr ^= Set.empty)
594 go ( (incomingFrom theirAddr ^= Set.empty)
585 . (incoming ^: Map.filterWithKey (const . notLost))
595 . (incoming ^: Map.filterWithKey (const . notLost))
@@ -634,6 +644,12 @@ data NCState = NCState
634 , _registeredOnNodes :: !(Map ProcessId [(NodeId,Int)])
644 , _registeredOnNodes :: !(Map ProcessId [(NodeId,Int)])
647+submitSendPool :: LocalNode -> NodeId -> IO () -> IO ()
648+submitSendPool node nid task = submitTask (localSendPool node) fork nid task
651+ forkIO $ io `Exception.catch` \(NodeClosedException _) -> return ()
637newtype NC a = NC { unNC :: StateT NCState (ReaderT LocalNode IO) a }
653newtype NC a = NC { unNC :: StateT NCState (ReaderT LocalNode IO) a }
638 deriving ( Applicative
654 deriving ( Applicative
639 , Functor
655 , Functor
@@ -644,11 +660,12 @@ newtype NC a = NC { unNC :: StateT NCState (ReaderT LocalNode IO) a }
646initNCState :: NCState
662initNCState :: NCState
647−initNCState = NCState { _links = Map.empty
663+initNCState = NCState
664+ { _links = Map.empty
648− , _monitors = Map.empty
665+ , _monitors = Map.empty
649− , _registeredHere = Map.empty
666+ , _registeredHere = Map.empty
650− , _registeredOnNodes = Map.empty
667+ , _registeredOnNodes = Map.empty
653-- | Thrown in response to the user invoking 'kill' (see Primitives.hs). This
670-- | Thrown in response to the user invoking 'kill' (see Primitives.hs). This
654-- type is deliberately not exported so it cannot be caught explicitly.
671-- type is deliberately not exported so it cannot be caught explicitly.
@@ -666,7 +683,7 @@ ncSendToProcess pid msg = do
666 node <- ask
683 node <- ask
667 if processNodeId pid == localNodeId node
684 if processNodeId pid == localNodeId node
668 then ncEffectLocalSend node pid msg
685 then ncEffectLocalSend node pid msg
669− else liftIO $ sendBinary node
686+ else liftIO $ submitSendPool node (processNodeId pid) $ sendBinary node
670 (NodeIdentifier $ localNodeId node)
687 (NodeIdentifier $ localNodeId node)
671 (NodeIdentifier $ processNodeId pid)
688 (NodeIdentifier $ processNodeId pid)
672 WithImplicitReconnect
689 WithImplicitReconnect
@@ -679,7 +696,7 @@ ncSendToNode to msg = do
679 node <- ask
696 node <- ask
680 liftIO $ if to == localNodeId node
697 liftIO $ if to == localNodeId node
681 then writeChan (localCtrlChan node) $! msg
698 then writeChan (localCtrlChan node) $! msg
682− else sendBinary node
699+ else submitSendPool node to $ sendBinary node
683 (NodeIdentifier $ localNodeId node)
700 (NodeIdentifier $ localNodeId node)
684 (NodeIdentifier to)
701 (NodeIdentifier to)
685 WithImplicitReconnect
702 WithImplicitReconnect