Skip to content

Commit

Permalink
Fix request and response checking in MoreToCome processing
Browse files Browse the repository at this point in the history
  • Loading branch information
darycabrera committed Nov 19, 2024
1 parent c083ead commit 5825c90
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions Database/MongoDB/Internal/Protocol.hs
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,11 @@ callOpMsg pipe request flagBit params = do
(rt, r) ->
case r of
ReplyOpMsg{..} ->
if flagBits == [MoreToCome]
then yieldResponses .| foldlC mergeResponses p
else return $ (rt, check reqId p)
if rt /= reqId then
error $ "expected response id (" ++ show rt ++ ") to match request id (" ++ show reqId ++ ")"
else
if flagBits == [MoreToCome] then yieldResponses .| foldlC mergeResponses p
else return (rt, r)
_ -> error "Impossible" -- see comment above
yieldResponses = repeatWhileMC
(do
Expand All @@ -298,27 +300,24 @@ callOpMsg pipe request flagBit params = do
readMVar var >>= either throwIO return :: IO Response
)
checkFlagBit
mergeResponses p@(rt,rep) p' =
case (p, p') of
((_, r), (_, r')) ->
case (r, r') of
(ReplyOpMsg _ sec _, ReplyOpMsg _ sec' _) -> do
let (section, section') = (head sec, head sec')
(cur, cur') = (maybe Nothing cast $ look "cursor" section,
maybe Nothing cast $ look "cursor" section')
case (cur, cur') of
(Just doc, Just doc') -> do
let (docs, docs') =
( fromJust $ cast $ valueAt "nextBatch" doc :: [Document]
, fromJust $ cast $ valueAt "nextBatch" doc' :: [Document])
id' = fromJust $ cast $ valueAt "id" doc' :: Int32
(rt, check id' (rt, rep{ sections = docs' ++ docs })) -- todo: avoid (++)
-- Since we use this to process moreToCome messages, we
-- know that there will be a nextBatch key in the document
_ -> error "Impossible"
_ -> error "Impossible" -- see comment above
check requestId (responseTo, reply) = if requestId == responseTo then reply else
error $ "expected response id (" ++ show responseTo ++ ") to match request id (" ++ show requestId ++ ")"
mergeResponses (rt, rep) (_, rep') =
case (rep, rep') of
(ReplyOpMsg _ sec _, ReplyOpMsg _ sec' _) -> do
let (section, section') = (head sec, head sec')
(cur, cur') = ( cast =<< look "cursor" section
, cast =<< look "cursor" section'
)
case (cur, cur') of
(Just doc, Just doc') -> do
let (docs, docs') =
( fromJust $ cast $ valueAt "nextBatch" doc :: [Document]
, fromJust $ cast $ valueAt "nextBatch" doc' :: [Document]
)
(rt, rep{ sections = docs' ++ docs }) -- todo: avoid (++)
-- Since we use this to process moreToCome messages, we
-- know that there will be a nextBatch key in the document
_ -> error "Impossible"
_ -> error "Impossible" -- see comment above

-- * Message

Expand Down

0 comments on commit 5825c90

Please sign in to comment.