@@ -91,6 +91,12 @@ import Control.Distributed.Process.Internal.StrictMVar
92 , takeMVar
92 , takeMVar
94+import Control.Distributed.Process.Internal.ThreadPool
94import Control.Concurrent.Chan (newChan, writeChan, readChan)
100import Control.Concurrent.Chan (newChan, writeChan, readChan)
95import qualified Control.Concurrent.MVar as MVar (newEmptyMVar, takeMVar)
101import qualified Control.Concurrent.MVar as MVar (newEmptyMVar, takeMVar)
96import Control.Concurrent.STM
102import Control.Concurrent.STM
@@ -264,12 +270,14 @@ createBareLocalNode endPoint rtable = do
264 , _localConnections = Map.empty
270 , _localConnections = Map.empty
266 ctrlChan <- newChan
272 ctrlChan <- newChan
273+ sendPool <- newThreadPool
267 let node = LocalNode { localNodeId = NodeId $ NT.address endPoint
274 let node = LocalNode { localNodeId = NodeId $ NT.address endPoint
268 , localEndPoint = endPoint
275 , localEndPoint = endPoint
269 , localState = state
276 , localState = state
270 , localCtrlChan = ctrlChan
277 , localCtrlChan = ctrlChan
271 , localEventBus = MxEventBusInitialising
278 , localEventBus = MxEventBusInitialising
272 , remoteTable = rtable
279 , remoteTable = rtable
280+ , localSendPool = sendPool
274 tracedNode <- startMxAgent node
282 tracedNode <- startMxAgent node
@@ -613,6 +621,8 @@ handleIncomingMessages node = go initConnectionState
613 , ctrlMsgSignal = Died nid DiedDisconnect
621 , ctrlMsgSignal = Died nid DiedDisconnect
615 let notLost k = not (k `Set.member` (st ^. incomingFrom theirAddr))
623 let notLost k = not (k `Set.member` (st ^. incomingFrom theirAddr))
624+ liftIO $ lookupWorker (localSendPool node) (NodeId theirAddr)
625+ >>= maybe (return ()) killThread
616 closeImplicitReconnections node nid
626 closeImplicitReconnections node nid
617 go ( (incomingFrom theirAddr ^= Set.empty)
627 go ( (incomingFrom theirAddr ^= Set.empty)
618 . (incoming ^: Map.filterWithKey (const . notLost))
628 . (incoming ^: Map.filterWithKey (const . notLost))
@@ -669,6 +679,12 @@ data NCState = NCState
669 , _registeredOnNodes :: !(Map ProcessId [(NodeId,Int)])
679 , _registeredOnNodes :: !(Map ProcessId [(NodeId,Int)])
682+submitSendPool :: LocalNode -> NodeId -> IO () -> IO ()
683+submitSendPool node nid task = submitTask (localSendPool node) fork nid task
686+ forkIO $ io `Exception.catch` \(NodeClosedException _) -> return ()
672newtype NC a = NC { unNC :: StateT NCState (ReaderT LocalNode IO) a }
688newtype NC a = NC { unNC :: StateT NCState (ReaderT LocalNode IO) a }
673 deriving ( Applicative
689 deriving ( Applicative
674 , Functor
690 , Functor
@@ -704,7 +720,7 @@ ncSendToProcessAndTrace shouldTrace pid msg = do
704 node <- ask
720 node <- ask
705 if processNodeId pid == localNodeId node
721 if processNodeId pid == localNodeId node
706 then ncEffectLocalSendAndTrace shouldTrace node pid msg
722 then ncEffectLocalSendAndTrace shouldTrace node pid msg
707− else liftIO $ sendBinary node
723+ else liftIO $ submitSendPool node (processNodeId pid) $ sendBinary node
708 (NodeIdentifier $ localNodeId node)
724 (NodeIdentifier $ localNodeId node)
709 (NodeIdentifier $ processNodeId pid)
725 (NodeIdentifier $ processNodeId pid)
710 WithImplicitReconnect
726 WithImplicitReconnect
@@ -717,7 +733,7 @@ ncSendToNode to msg = do
717 node <- ask
733 node <- ask
718 liftIO $ if to == localNodeId node
734 liftIO $ if to == localNodeId node
719 then writeChan (localCtrlChan node) $! msg
735 then writeChan (localCtrlChan node) $! msg
720− else sendBinary node
736+ else submitSendPool node to $ sendBinary node
721 (NodeIdentifier $ localNodeId node)
737 (NodeIdentifier $ localNodeId node)
722 (NodeIdentifier to)
738 (NodeIdentifier to)
723 WithImplicitReconnect
739 WithImplicitReconnect