Skip to content

Commit

Permalink
inbound-governor: cached InboundGovernorCounters
Browse files Browse the repository at this point in the history
Traces cache changes only when needed

Added Eq instance to InboundGovernorCounters
  • Loading branch information
bolt12 authored and coot committed Oct 14, 2021
1 parent fd90aa5 commit d6065ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Expand Up @@ -41,6 +41,7 @@ import Control.Monad.Class.MonadTime
import Control.Monad.Class.MonadTimer
import Control.Tracer (Tracer, traceWith)

import Data.Cache
import Data.ByteString.Lazy (ByteString)
import Data.Void (Void)
import Data.List (sortOn)
Expand Down Expand Up @@ -99,7 +100,8 @@ inboundGovernor tracer serverControlChannel protocolIdleTimeout
connectionManager observableStateVar = do
let state = InboundGovernorState {
igsConnections = Map.empty,
igsObservableVar = observableStateVar
igsObservableVar = observableStateVar,
igsCountersCache = mempty
}
inboundGovernorLoop state
where
Expand All @@ -110,9 +112,10 @@ inboundGovernor tracer serverControlChannel protocolIdleTimeout
:: InboundGovernorState muxMode peerAddr m a b
-> m Void
inboundGovernorLoop !state = do
traceWith tracer ( TrInboundGovernorCounters
$ inboundGovernorCounters state)

mapTraceWithCache TrInboundGovernorCounters
tracer
(igsCountersCache state)
(inboundGovernorCounters state)
event
<- atomically $
(uncurry MuxFinished <$> firstMuxToFinish state)
Expand Down
Expand Up @@ -29,6 +29,7 @@ import Control.Exception (assert)
import Control.Monad.Class.MonadSTM.Strict
import Control.Monad.Class.MonadThrow hiding (handle)

import Data.Cache (Cache)
import Data.ByteString.Lazy (ByteString)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
Expand Down Expand Up @@ -94,7 +95,11 @@ data InboundGovernorState muxMode peerAddr m a b =

-- | PRNG available to 'PrunePolicy'.
--
igsObservableVar :: !(StrictTVar m InboundGovernorObservableState)
igsObservableVar :: !(StrictTVar m InboundGovernorObservableState),

-- | 'InboundGovernorCounters' counters cache. Allows to only trace
-- values when necessary.
igsCountersCache :: !(Cache InboundGovernorCounters)
}

-- | Counters for tracing and analysis purposes
Expand All @@ -105,7 +110,7 @@ data InboundGovernorCounters = InboundGovernorCounters {
hotPeersRemote :: !Int
-- ^ number of remote peers that have the local peer as hot
}
deriving Show
deriving (Eq, Show)

instance Semigroup InboundGovernorCounters where
InboundGovernorCounters w h <> InboundGovernorCounters w' h' =
Expand Down

0 comments on commit d6065ea

Please sign in to comment.