aboutsummaryrefslogtreecommitdiff
path: root/hsm-core/Hsm/Core/Zmq.hs
blob: 2f70d48249643008c4f48b70f60de1ce28b37104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{-# LANGUAGE OverloadedStrings #-}

module Hsm.Core.Zmq
  ( withSocket
  ) where

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 ::
     (Z.SocketType t, IOE :> es, Log :> es, Resource :> es)
  => t
  -> Eff es (Z.Socket t)
withSocket stype = withLogIO >>= bracket
  where
    bracket logIO = snd . snd <$> allocate acquire release
      where
        acquire =
          logIO LogTrace "Acquiring ZMQ context" >> do
            cont <- Z.context
            sock <- Z.socket cont stype
            return (cont, sock)
        release (cont, sock) =
          logIO LogTrace "Releasing ZMQ context" >> do
            Z.close sock
            Z.shutdown cont