Skip to content

Commit 5825c90

Browse files
committed
Fix request and response checking in MoreToCome processing
Addresses mongodb-haskell#153
1 parent c083ead commit 5825c90

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

Database/MongoDB/Internal/Protocol.hs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,11 @@ callOpMsg pipe request flagBit params = do
287287
(rt, r) ->
288288
case r of
289289
ReplyOpMsg{..} ->
290-
if flagBits == [MoreToCome]
291-
then yieldResponses .| foldlC mergeResponses p
292-
else return $ (rt, check reqId p)
290+
if rt /= reqId then
291+
error $ "expected response id (" ++ show rt ++ ") to match request id (" ++ show reqId ++ ")"
292+
else
293+
if flagBits == [MoreToCome] then yieldResponses .| foldlC mergeResponses p
294+
else return (rt, r)
293295
_ -> error "Impossible" -- see comment above
294296
yieldResponses = repeatWhileMC
295297
(do
@@ -298,27 +300,24 @@ callOpMsg pipe request flagBit params = do
298300
readMVar var >>= either throwIO return :: IO Response
299301
)
300302
checkFlagBit
301-
mergeResponses p@(rt,rep) p' =
302-
case (p, p') of
303-
((_, r), (_, r')) ->
304-
case (r, r') of
305-
(ReplyOpMsg _ sec _, ReplyOpMsg _ sec' _) -> do
306-
let (section, section') = (head sec, head sec')
307-
(cur, cur') = (maybe Nothing cast $ look "cursor" section,
308-
maybe Nothing cast $ look "cursor" section')
309-
case (cur, cur') of
310-
(Just doc, Just doc') -> do
311-
let (docs, docs') =
312-
( fromJust $ cast $ valueAt "nextBatch" doc :: [Document]
313-
, fromJust $ cast $ valueAt "nextBatch" doc' :: [Document])
314-
id' = fromJust $ cast $ valueAt "id" doc' :: Int32
315-
(rt, check id' (rt, rep{ sections = docs' ++ docs })) -- todo: avoid (++)
316-
-- Since we use this to process moreToCome messages, we
317-
-- know that there will be a nextBatch key in the document
318-
_ -> error "Impossible"
319-
_ -> error "Impossible" -- see comment above
320-
check requestId (responseTo, reply) = if requestId == responseTo then reply else
321-
error $ "expected response id (" ++ show responseTo ++ ") to match request id (" ++ show requestId ++ ")"
303+
mergeResponses (rt, rep) (_, rep') =
304+
case (rep, rep') of
305+
(ReplyOpMsg _ sec _, ReplyOpMsg _ sec' _) -> do
306+
let (section, section') = (head sec, head sec')
307+
(cur, cur') = ( cast =<< look "cursor" section
308+
, cast =<< look "cursor" section'
309+
)
310+
case (cur, cur') of
311+
(Just doc, Just doc') -> do
312+
let (docs, docs') =
313+
( fromJust $ cast $ valueAt "nextBatch" doc :: [Document]
314+
, fromJust $ cast $ valueAt "nextBatch" doc' :: [Document]
315+
)
316+
(rt, rep{ sections = docs' ++ docs }) -- todo: avoid (++)
317+
-- Since we use this to process moreToCome messages, we
318+
-- know that there will be a nextBatch key in the document
319+
_ -> error "Impossible"
320+
_ -> error "Impossible" -- see comment above
322321

323322
-- * Message
324323

0 commit comments

Comments
 (0)