on master help sign in
rename withSocket to useSocket

operations like withFile create and a handle and then use it, however withSocket uses an existing socket, so useSocket is a better name to avoid confusion. Also, make useSocket an internal operation for now, as it is trivial to implement it.

commitdf8de8d5bdca2a09ac19136a1a49e940152804b2 authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 9 Oct 2019, 13:03 (7 years ago) parent33e6fd0
4 files changed+17−30
examples/EchoServer.hs+2−2 examples/FileSinkServer.hs+2−1 src/Streamly/Internal/Network/Socket.hs+12−22 src/Streamly/Network/Socket.hs+1−5
examples/EchoServer.hs+2−2
@@ -1,13 +1,13 @@
11-- A concurrent TCP server that echoes everything that it receives.
22
33import Streamly
4−import Streamly.Internal.Network.Socket (readArrays)
4+import Streamly.Internal.Network.Socket (useSocketM, readArrays)
55import Streamly.Network.Socket
66import Streamly.Network.Server
77import qualified Streamly.Prelude as S
88
99main :: IO ()
1010main = S.drain
11− $ parallely $ S.mapM (flip withSocketM echo)
11+ $ parallely $ S.mapM (flip useSocketM echo)
1212 $ serially $ S.unfold listenOnPort 8090
1313 where echo sk = S.fold (writeArrays sk) $ S.unfold readArrays sk
examples/FileSinkServer.hs+2−1
@@ -9,6 +9,7 @@ import System.Environment (getArgs)
99
1010import Streamly
1111import Streamly.Data.String
12+import Streamly.Internal.Network.Socket (useSocket)
1213import qualified Streamly.FileSystem.Handle as FH
1314import qualified Streamly.Memory.Array as A
1415import qualified Streamly.Network.Socket as NS
@@ -24,7 +25,7 @@ main = do
2425 (\src -> S.fold (FH.write src)
2526 $ encodeChar8Unchecked
2627 $ S.concatUnfold A.read
27− $ S.concatMapWith parallel (flip NS.withSocket recv)
28+ $ S.concatMapWith parallel (flip useSocket recv)
2829 $ S.unfold NS.listenOnPort 8090)
2930
3031 where
src/Streamly/Internal/Network/Socket.hs+12−22
@@ -19,8 +19,8 @@
1919module Streamly.Internal.Network.Socket
2020 (
2121 -- * Use a socket
22− withSocketM
23− , withSocket
22+ useSocketM
23+ , useSocket
2424
2525 -- * Read from connection
2626 , read
@@ -92,35 +92,25 @@ import qualified Streamly.Internal.Memory.Array.Types as A
9292import qualified Streamly.Prelude as S
9393import qualified Streamly.Streams.StreamD.Type as D
9494
95−-- | @'withSocketM' socket act@ runs the monadic computation @act@ passing the
96−-- socket handle to it. The handle will be closed on exit from 'withSocketM',
95+-- | @'useSocketM' socket act@ runs the monadic computation @act@ passing the
96+-- socket handle to it. The handle will be closed on exit from 'useSocketM',
9797-- whether by normal termination or by raising an exception. If closing the
9898-- handle raises an exception, then this exception will be raised by
99−-- 'withSocketM' rather than any exception raised by 'act'.
99+-- 'useSocketM' rather than any exception raised by 'act'.
100100--
101101-- @since 0.7.0
102−{-# INLINE withSocketM #-}
103−withSocketM :: (MonadMask m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
104−withSocketM sk f = do
105−-- f sk `onException` liftIO (Net.close sk)
106−-- liftIO (Net.close sk)
107− finally (liftIO (Net.close sk)) (f sk)
108−
109−-- XXX bracket performs 2x better than 'finally'. That's perhaps because of
110−-- better fusion of "A.flattenArrays . readArrays"?
111−--
112−-- withSocketS :: (IsStream t, MonadCatch m, MonadIO m) => Socket -> t m Word8
113−-- withSocketS sk = A.flattenArrays $
114−-- S.finally (liftIO (Net.close sk)) (readArrays sk)
102+{-# INLINE useSocketM #-}
103+useSocketM :: (MonadMask m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
104+useSocketM sk f = finally (liftIO (Net.close sk)) (f sk)
115105
116−-- | Like 'withSocketM' but runs a streaming computation instead of a monadic
106+-- | Like 'useSocketM' but runs a streaming computation instead of a monadic
117107-- computation.
118108--
119109-- @since 0.7.0
120−{-# INLINE withSocket #-}
121−withSocket :: (IsStream t, MonadCatch m, MonadIO m)
110+{-# INLINE useSocket #-}
111+useSocket :: (IsStream t, MonadCatch m, MonadIO m)
122112 => Socket -> (Socket -> t m a) -> t m a
123−withSocket sk = S.bracket (return sk) (liftIO . Net.close)
113+useSocket sk = S.bracket (return sk) (liftIO . Net.close)
124114
125115-------------------------------------------------------------------------------
126116-- Array IO (Input)
src/Streamly/Network/Socket.hs+1−5
@@ -23,12 +23,8 @@
2323
2424module Streamly.Network.Socket
2525 (
26− -- * Use a socket
27− withSocketM
28− , withSocket
29−
3026 -- * Read from connection
31− , read
27+ read
3228 -- , readUtf8
3329 -- , readLines
3430 -- , readFrames