Move handler flag check from bus to writer with skip logging

This commit is contained in:
2026-09-30 08:06:59 +03:00
parent a7a11e96b7
commit c2425e8fb0
2 changed files with 42 additions and 6 deletions
+18 -5
View File
@@ -4,6 +4,7 @@
module HomeAssistant.Runtime.Connection
( readerAction
, writerAction
, dispatchService
, encodeService
, isTriggerEvent
) where
@@ -29,9 +30,10 @@ import qualified Data.Text as T
import Data.Void (Void)
import HomeAssistant.Controller (Service (..), Target (..))
import HomeAssistant.Runtime.Bus
import HomeAssistant.Runtime.Flags (isEnabled)
import HomeAssistant.Runtime.Supervisor (Fatal (..))
import qualified Network.WebSockets as WS
import Katip (sl, logFM, Severity (..), ls, KatipContext, katipAddNamespace, katipAddContext)
import Katip (sl, logFM, Severity (..), ls, Namespace (Namespace), KatipContext, katipAddNamespace, katipAddContext)
import Data.UUID (toText)
import AFRP (Request(..), Event(..))
import Control.Monad.IO.Class (liftIO, MonadIO)
@@ -122,10 +124,21 @@ sendWithId bus conn svc = katipAddNamespace "connection" $ do
writerAction :: (MonadIO m, KatipContext m, MonadCatch m) => RateLimiter -> Bus -> m Void
writerAction rateLimiter bus = forever $ do
(request, svc) <- liftIO $ atomically $ readTChan (busOutbound bus)
katipAddContext (sl "traceId" (toText (requestTraceId request))) $ do
conn <- liftIO $ atomically $ readTVar (busConn bus) >>= maybe retry pure
-- runRateLimited rateLimiter $ sendWithId bus conn request svc
runRateLimited rateLimiter $ sendWithId bus conn svc
katipAddContext (sl "traceId" (toText (requestTraceId request))) $
katipAddNamespace (Namespace [requestHandler request]) $
dispatchService rateLimiter bus request svc
-- | Sends the service call, or logs and drops it when its handler is disabled.
dispatchService
:: (MonadIO m, KatipContext m, MonadCatch m)
=> RateLimiter -> Bus -> Request -> Service -> m ()
dispatchService rateLimiter bus request svc = do
enabled <- isEnabled (busFlags bus) (requestHandler request)
if enabled
then do
conn <- liftIO $ atomically $ readTVar (busConn bus) >>= maybe retry pure
runRateLimited rateLimiter $ sendWithId bus conn svc
else logFM InfoS (ls $ "skipped disabled handler: " <> show svc)
encodeService :: Int -> Service -> Value
encodeService callId Service{..} = object $