From cba74369c454a573f1c73f9366966ad5b453dff0 Mon Sep 17 00:00:00 2001 From: voidlizard Date: Fri, 1 Nov 2024 16:44:38 +0300 Subject: [PATCH] HttpWorker exceptions --- hbs2-core/lib/HBS2/Net/Messaging/TCP.hs | 16 +++++---- hbs2-peer/app/HttpWorker.hs | 44 ++++++++++++++++--------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/hbs2-core/lib/HBS2/Net/Messaging/TCP.hs b/hbs2-core/lib/HBS2/Net/Messaging/TCP.hs index cf44f686..1e412154 100644 --- a/hbs2-core/lib/HBS2/Net/Messaging/TCP.hs +++ b/hbs2-core/lib/HBS2/Net/Messaging/TCP.hs @@ -22,7 +22,7 @@ import HBS2.System.Logger.Simple import HBS2.Misc.PrettyStuff import Control.Concurrent.STM (flushTQueue) -import Control.Monad +import Control.Monad.Trans.Maybe import Data.Bits import Data.ByteString.Lazy (ByteString) import Data.ByteString.Lazy qualified as LBS @@ -228,8 +228,8 @@ spawnConnection tp env@MessagingTCP{..} so sa = liftIO do debug $ "USED:" <+> viaShow tp <+> pretty own <+> pretty used - when ( used <= 2 ) do - atomically $ modifyTVar (view tcpPeerConn env) (HashMap.insert newP connId) + -- when ( used <= 2 ) do + atomically $ modifyTVar (view tcpPeerConn env) (HashMap.insert newP connId) when (used == 1) do @@ -427,7 +427,11 @@ runMessagingTCP env@MessagingTCP{..} = liftIO do dePips <- readTVarIO defs <&> HashMap.keys - forM_ dePips $ \pip -> do + forM_ dePips $ \pip -> void $ runMaybeT do + + -- FIXME: make-sure-it-is-correct + already <- readTVarIO _tcpPeerXp <&> HashMap.member pip + msgs <- readTVarIO defs <&> HashMap.findWithDefault mempty pip unless (L.null msgs) do @@ -442,9 +446,9 @@ runMessagingTCP env@MessagingTCP{..} = liftIO do co' <- atomically $ readTVar (view tcpPeerConn env) <&> HashMap.lookup pip when (isNothing co') do - debug $ red "No session for" <+> pretty pip + trace $ red "No session for" <+> pretty pip - maybe1 co' (void $ fireTCP env pip (connectPeerTCP env pip)) $ \co -> do + lift $ maybe1 co' (void $ fireTCP env pip (connectPeerTCP env pip)) $ \co -> do q' <- atomically $ readTVar (view tcpConnQ env) <&> HashMap.lookup co maybe1 q' none $ \q -> do atomically do diff --git a/hbs2-peer/app/HttpWorker.hs b/hbs2-peer/app/HttpWorker.hs index 6677f4e9..6c8dc16f 100644 --- a/hbs2-peer/app/HttpWorker.hs +++ b/hbs2-peer/app/HttpWorker.hs @@ -102,12 +102,16 @@ httpWorker (PeerConfig syn) pmeta e = do get "/size/:hash" do - what <- param @String "hash" <&> fromString - size <- liftIO $ hasBlock sto what - case size of - Nothing -> status status404 - Just n -> do - json n + void $ flip runContT pure do + what <- lift (param @String "hash") + <&> fromStringMay + >>= orElse (status status404) + + size <- liftIO $ hasBlock sto what + case size of + Nothing -> lift $ status status404 + Just n -> do + lift $ json n -- TODO: key-to-disable-tree-streaming @@ -131,18 +135,26 @@ httpWorker (PeerConfig syn) pmeta e = do -- TODO: define-parsable-instance-for-our-types get "/tree/:hash" do - what <- param @String "hash" <&> fromString - getTreeHash sto what + void $ flip runContT pure do + what <- lift (param @String "hash") + <&> fromStringMay + >>= orElse (status status404) + + lift $ getTreeHash sto what get "/cat/:hash" do - what <- param @String "hash" <&> fromString - blob <- liftIO $ getBlock sto what - case blob of - Nothing -> status status404 - Just lbs -> do - addHeader "content-type" "application/octet-stream" - addHeader "content-length" [qc|{LBS.length lbs}|] - raw lbs + void $ flip runContT pure do + what <- lift (param @String "hash") + <&> fromStringMay + >>= orElse (status status404) + lift do + blob <- liftIO $ getBlock sto what + case blob of + Nothing -> status status404 + Just lbs -> do + addHeader "content-type" "application/octet-stream" + addHeader "content-length" [qc|{LBS.length lbs}|] + raw lbs get "/reflog/:ref" do re <- param @String "ref" <&> fromStringMay