module Ouroboros.Network.PeerSelection.RootPeersDNS.DNSSemaphore
(
DNSSemaphore
, newLedgerAndPublicRootDNSSemaphore
, newDNSLocalRootSemaphore
, withDNSSemaphore
) where
import Control.Concurrent.Class.MonadSTM.Strict
import Control.Concurrent.Class.MonadSTM.TSem
import Control.Monad.Class.MonadThrow
maxDNSConcurrency :: Integer
maxDNSConcurrency :: Integer
maxDNSConcurrency = Integer
8
maxDNSLocalRootConcurrency :: Integer
maxDNSLocalRootConcurrency :: Integer
maxDNSLocalRootConcurrency = Integer
2
newtype DNSSemaphore m = DNSSemaphore (TSem m)
newLedgerAndPublicRootDNSSemaphore :: MonadSTM m => m (DNSSemaphore m)
newLedgerAndPublicRootDNSSemaphore :: forall (m :: * -> *). MonadSTM m => m (DNSSemaphore m)
newLedgerAndPublicRootDNSSemaphore = TSem m -> DNSSemaphore m
forall (m :: * -> *). TSem m -> DNSSemaphore m
DNSSemaphore (TSem m -> DNSSemaphore m) -> m (TSem m) -> m (DNSSemaphore m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM m (TSem m) -> m (TSem m)
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (Integer -> STM m (TSem m)
forall (m :: * -> *). MonadSTM m => Integer -> STM m (TSem m)
newTSem Integer
maxDNSConcurrency)
newDNSLocalRootSemaphore :: MonadSTM m => STM m (DNSSemaphore m)
newDNSLocalRootSemaphore :: forall (m :: * -> *). MonadSTM m => STM m (DNSSemaphore m)
newDNSLocalRootSemaphore = TSem m -> DNSSemaphore m
forall (m :: * -> *). TSem m -> DNSSemaphore m
DNSSemaphore (TSem m -> DNSSemaphore m)
-> STM m (TSem m) -> STM m (DNSSemaphore m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer -> STM m (TSem m)
forall (m :: * -> *). MonadSTM m => Integer -> STM m (TSem m)
newTSem Integer
maxDNSLocalRootConcurrency
withDNSSemaphore :: (MonadSTM m, MonadThrow m) => DNSSemaphore m -> m a -> m a
withDNSSemaphore :: forall (m :: * -> *) a.
(MonadSTM m, MonadThrow m) =>
DNSSemaphore m -> m a -> m a
withDNSSemaphore (DNSSemaphore TSem m
s) =
m () -> m () -> m a -> m a
forall a b c. m a -> m b -> m c -> m c
forall (m :: * -> *) a b c.
MonadThrow m =>
m a -> m b -> m c -> m c
bracket_ (STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ TSem m -> STM m ()
forall (m :: * -> *). MonadSTM m => TSem m -> STM m ()
waitTSem TSem m
s)
(STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ TSem m -> STM m ()
forall (m :: * -> *). MonadSTM m => TSem m -> STM m ()
signalTSem TSem m
s)