Skip to content

Commit 222beb5

Browse files
Implement unsafeUSend.
1 parent deadc44 commit 222beb5

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/Control/Distributed/Process.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module Control.Distributed.Process
4040
, mergePortsRR
4141
-- * Unsafe messaging variants
4242
, unsafeSend
43+
, unsafeUSend
4344
, unsafeSendChan
4445
, unsafeNSend
4546
, unsafeWrapMessage
@@ -212,6 +213,7 @@ import Control.Distributed.Process.Internal.Primitives
212213
, mergePortsBiased
213214
, mergePortsRR
214215
, unsafeSend
216+
, unsafeUSend
215217
, unsafeSendChan
216218
, unsafeNSend
217219
-- Advanced messaging

src/Control/Distributed/Process/Internal/Primitives.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Control.Distributed.Process.Internal.Primitives
2323
, mergePortsRR
2424
-- * Unsafe messaging variants
2525
, unsafeSend
26+
, unsafeUSend
2627
, unsafeSendChan
2728
, unsafeNSend
2829
-- * Advanced messaging
@@ -279,6 +280,13 @@ usend them msg = do
279280
else sendCtrlMsg (Just there) $ UnreliableSend (processLocalId them)
280281
(createMessage msg)
281282

283+
-- | /Unsafe/ variant of 'usend'. This function makes /no/ attempt to serialize
284+
-- the message when the destination process resides on the same local
285+
-- node. Therefore, a local receiver would need to be prepared to cope with any
286+
-- errors resulting from evaluation of the message.
287+
unsafeUSend :: Serializable a => ProcessId -> a -> Process ()
288+
unsafeUSend = Unsafe.usend
289+
282290
-- | Wait for a message of a specific type
283291
expect :: forall a. Serializable a => Process a
284292
expect = receiveWait [match return]

src/Control/Distributed/Process/UnsafePrimitives.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module Control.Distributed.Process.UnsafePrimitives
4545
send
4646
, sendChan
4747
, nsend
48+
, usend
4849
, wrapMessage
4950
) where
5051

@@ -65,6 +66,7 @@ import Control.Distributed.Process.Internal.Types
6566
, ImplicitReconnect(..)
6667
, SendPortId(..)
6768
, Message
69+
, createMessage
6870
, sendPortProcessId
6971
, unsafeCreateUnencodedMessage
7072
)
@@ -96,6 +98,25 @@ send them msg = do
9698
unsafeSendLocal pid msg' =
9799
sendCtrlMsg Nothing $ LocalSend pid (unsafeCreateUnencodedMessage msg')
98100

101+
-- | Send a message unreliably.
102+
--
103+
-- Unlike 'send', this function is insensitive to 'reconnect'. It will
104+
-- try to send the message regardless of the history of connection failures
105+
-- between the nodes.
106+
--
107+
-- Message passing with 'usend' is ordered for a given sender and receiver
108+
-- if the messages arrive at all.
109+
--
110+
usend :: Serializable a => ProcessId -> a -> Process ()
111+
usend them msg = do
112+
proc <- ask
113+
let there = processNodeId them
114+
if localNodeId (processNode proc) == there
115+
then sendCtrlMsg Nothing $
116+
LocalSend them (unsafeCreateUnencodedMessage msg)
117+
else sendCtrlMsg (Just there) $ UnreliableSend (processLocalId them)
118+
(createMessage msg)
119+
99120
-- | Send a message on a typed channel
100121
sendChan :: Serializable a => SendPort a -> a -> Process ()
101122
sendChan (SendPort cid) msg = do

0 commit comments

Comments
 (0)