Skip to content

Commit

Permalink
ExitPolicy - compute reconnect delay
Browse files Browse the repository at this point in the history
ExitPolicy governs the delay for reconnecting either if any of the
mini-protocol exists cleanly or throws an exception.
  • Loading branch information
coot committed Oct 28, 2022
1 parent 73f53e4 commit 655f326
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions ouroboros-network/ouroboros-network.cabal
Expand Up @@ -65,6 +65,7 @@ library
Ouroboros.Network.Diffusion.P2P
Ouroboros.Network.Diffusion.NonP2P
Ouroboros.Network.Diffusion.Policies
Ouroboros.Network.ExitPolicy
Ouroboros.Network.KeepAlive
Ouroboros.Network.Magic
Ouroboros.Network.NodeToNode
Expand Down
54 changes: 54 additions & 0 deletions ouroboros-network/src/Ouroboros/Network/ExitPolicy.hs
@@ -0,0 +1,54 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}

module Ouroboros.Network.ExitPolicy
( ReconnectDelay (..)
, ExitPolicy (..)
, stdExitPolicy
, ReturnPolicy
, alwaysCleanReturnPolicy
) where

import Control.Monad.Class.MonadTime

newtype ReconnectDelay = ReconnectDelay { reconnectDelay :: DiffTime }
deriving Eq
deriving newtype Num

-- | 'ReconnectDelay' is an additive monoid.
--
instance Semigroup ReconnectDelay where
ReconnectDelay a <> ReconnectDelay b = ReconnectDelay (a + b)

instance Monoid ReconnectDelay where
mempty = ReconnectDelay 0

type ReturnPolicy a = a -> ReconnectDelay

-- | 'ReturnPolicy' allows to compute reconnection delay from value return by
-- a mini-protocol. If a mini-protocol returned with an error 'epErrorDelay'
-- is used.
data ExitPolicy a =
ExitPolicy {
-- | Compute 'ReturnCommand' from return value.
--
epReturnDelay :: ReturnPolicy a,

-- | The delay when a mini-protocol returned with an error.
--
epErrorDelay :: ReconnectDelay
}

alwaysCleanReturnPolicy :: ReconnectDelay -- ^ reconnection delay on error
-> ExitPolicy a
alwaysCleanReturnPolicy = ExitPolicy mempty

-- | 'ExitPolicy' with 10s error delay.
--
stdExitPolicy :: ReturnPolicy a -> ExitPolicy a
stdExitPolicy epReturnDelay =
ExitPolicy {
epReturnDelay,
epErrorDelay = 10
}

0 comments on commit 655f326

Please sign in to comment.