Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ExitPolicy - compute reconnect delay
ExitPolicy governs the delay for reconnecting either if any of the mini-protocol exists cleanly or throws an exception.
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |