This commit is contained in:
voidlizard 2024-11-09 10:07:01 +03:00
parent 1e591bcbf8
commit 8cdecbeb38
1 changed files with 15 additions and 2 deletions

View File

@ -553,7 +553,8 @@ downloadDispatcher brains env = flip runContT pure do
rq <- newTQueueIO rq <- newTQueueIO
seen <- newTVarIO ( HPSQ.empty :: HashPSQ HashRef Int TimeSpec ) let seenLimit = 1000
seen <- newTVarIO ( HPSQ.empty :: HashPSQ HashRef TimeSpec () )
blkQ <- newTVarIO ( HPSQ.empty :: HashPSQ HashRef Int NominalDiffTime ) blkQ <- newTVarIO ( HPSQ.empty :: HashPSQ HashRef Int NominalDiffTime )
@ -568,6 +569,16 @@ downloadDispatcher brains env = flip runContT pure do
ContT $ withAsync $ forever do ContT $ withAsync $ forever do
join $ atomically (readTQueue work) join $ atomically (readTQueue work)
ContT $ withAsync $ forever do
pause @'Seconds 600
debug $ "CLEANUP SEEN"
atomically do
fix \next -> do
n <- readTVar seen <&> HPSQ.size
when (n > seenLimit) do
modifyTVar seen HPSQ.deleteMin
next
liftIO $ withPeerM env do liftIO $ withPeerM env do
subscribe @e DownloadReqKey $ \(DownloadReqData h) -> do subscribe @e DownloadReqKey $ \(DownloadReqData h) -> do
now <- getTimeCoarse now <- getTimeCoarse
@ -576,7 +587,7 @@ downloadDispatcher brains env = flip runContT pure do
if already then do if already then do
pure False pure False
else do else do
modifyTVar seen ( HPSQ.insert (HashRef h) 1 now ) modifyTVar seen ( HPSQ.insert (HashRef h) now () )
modifyTVar blkQ ( HPSQ.insert (HashRef h) 1 t0 ) modifyTVar blkQ ( HPSQ.insert (HashRef h) 1 t0 )
pure True pure True
when new do when new do
@ -610,8 +621,10 @@ downloadDispatcher brains env = flip runContT pure do
forever do forever do
pause @'Seconds 10 pause @'Seconds 10
size <- atomically $ readTVar blkQ <&> HPSQ.size size <- atomically $ readTVar blkQ <&> HPSQ.size
seenSize <- atomically $ readTVar seen <&> HPSQ.size
debug $ yellow $ "I'm download dispatcher" debug $ yellow $ "I'm download dispatcher"
debug $ yellow $ "wip:" <+> pretty size debug $ yellow $ "wip:" <+> pretty size
debug $ yellow $ "seen:" <+> pretty seenSize
where where