diff options
author | Paul Oliver <contact@pauloliver.dev> | 2024-12-29 17:05:34 +0000 |
---|---|---|
committer | Paul Oliver <contact@pauloliver.dev> | 2025-01-16 18:30:09 -0800 |
commit | cc639b06c7126fac7b445d8f778455620d7f8f50 (patch) | |
tree | a4c5c7c0b0a9cdb5bea0891e198003035065e57d /hsm-core/Hsm/Core/Zmq.hs |
Initial
Diffstat (limited to 'hsm-core/Hsm/Core/Zmq.hs')
-rw-r--r-- | hsm-core/Hsm/Core/Zmq.hs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/hsm-core/Hsm/Core/Zmq.hs b/hsm-core/Hsm/Core/Zmq.hs new file mode 100644 index 0000000..8c12133 --- /dev/null +++ b/hsm-core/Hsm/Core/Zmq.hs @@ -0,0 +1,34 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Hsm.Core.Zmq + ( withSocket + ) where + +import Data.Text (Text) +import Effectful (Eff, IOE, (:>)) +import Effectful.Log (Log, LogLevel(LogTrace)) +import Effectful.Resource (Resource, allocate) +import Hsm.Core.Log (withLogIO) +import System.ZMQ4 qualified as Z + +withSocket :: + forall t es. (Z.SocketType t, IOE :> es, Log :> es, Resource :> es) + => t + -> Eff es (Z.Socket t) +withSocket stype = withLogIO >>= bracket + where + bracket :: (LogLevel -> Text -> IO ()) -> Eff es (Z.Socket t) + bracket logIO = snd . snd <$> allocate acquire release + where + acquire :: IO (Z.Context, Z.Socket t) + acquire = do + logIO LogTrace "Acquiring ZMQ context" + cont <- Z.context + sock <- Z.socket cont stype + return (cont, sock) + -- + release :: (Z.Context, Z.Socket t) -> IO () + release (cont, sock) = do + logIO LogTrace "Releasing ZMQ context" + Z.close sock + Z.shutdown cont |