-
Notifications
You must be signed in to change notification settings - Fork 49
daemon: handle event queueing and shutdown issues #692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -4,11 +4,15 @@ module Cachix.Daemon.EventLoop | |||||||
sendIO, | ||||||||
run, | ||||||||
exitLoopWith, | ||||||||
exitLoopWithFailure, | ||||||||
EventLoop, | ||||||||
) | ||||||||
where | ||||||||
|
||||||||
import Cachix.Daemon.ShutdownLatch (ShutdownLatch) | ||||||||
import Cachix.Daemon.ShutdownLatch qualified as ShutdownLatch | ||||||||
import Cachix.Daemon.Types.EventLoop (EventLoop (..), EventLoopError (..)) | ||||||||
import Control.Concurrent.STM | ||||||||
import Control.Concurrent.STM.TBMQueue | ||||||||
( isFullTBMQueue, | ||||||||
newTBMQueueIO, | ||||||||
|
@@ -21,9 +25,9 @@ import Protolude | |||||||
|
||||||||
new :: (MonadIO m) => m (EventLoop event a) | ||||||||
new = do | ||||||||
exitLatch <- liftIO newEmptyMVar | ||||||||
queue <- liftIO $ newTBMQueueIO 100 | ||||||||
return $ EventLoop {queue, exitLatch} | ||||||||
shutdownLatch <- ShutdownLatch.newShutdownLatch | ||||||||
queue <- liftIO $ newTBMQueueIO 100_000 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The increased queue capacity to 100_000 may have performance implications under high load; consider reviewing resource usage and potential memory constraints.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
return $ EventLoop {queue, shutdownLatch} | ||||||||
|
||||||||
-- | Send an event to the event loop with logging. | ||||||||
send :: (Katip.KatipContext m) => EventLoop event a -> event -> m () | ||||||||
|
@@ -38,41 +42,67 @@ sendIO = send' logger | |||||||
logger _ _ = return () | ||||||||
|
||||||||
send' :: (MonadIO m) => (Katip.Severity -> Katip.LogStr -> m ()) -> EventLoop event a -> event -> m () | ||||||||
send' logger eventloop@(EventLoop {queue}) event = do | ||||||||
res <- liftIO $ atomically $ tryWriteTBMQueue queue event | ||||||||
case res of | ||||||||
-- The queue is closed. | ||||||||
Nothing -> | ||||||||
logger Katip.DebugS "Ignored an event because the event loop is closed" | ||||||||
-- Successfully wrote to the queue | ||||||||
Just True -> return () | ||||||||
-- Failed to write to the queue | ||||||||
Just False -> do | ||||||||
isFull <- liftIO $ atomically $ isFullTBMQueue queue | ||||||||
let message = | ||||||||
if isFull | ||||||||
then "Event loop is full" | ||||||||
else "Unknown error" | ||||||||
logger Katip.ErrorS $ "Failed to write to event loop: " <> message | ||||||||
exitLoopWithFailure EventLoopFull eventloop | ||||||||
send' logger eventloop@(EventLoop {queue, shutdownLatch}) event = do | ||||||||
sandydoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
-- First check if shutdown has been requested | ||||||||
isExiting <- ShutdownLatch.isShuttingDown shutdownLatch | ||||||||
if isExiting | ||||||||
then logger Katip.DebugS "Ignored an event because the event loop is shutting down" | ||||||||
else do | ||||||||
res <- liftIO $ atomically $ tryWriteTBMQueue queue event | ||||||||
case res of | ||||||||
-- The queue is closed. | ||||||||
Nothing -> | ||||||||
logger Katip.DebugS "Ignored an event because the event loop is closed" | ||||||||
-- Successfully wrote to the queue | ||||||||
Just True -> return () | ||||||||
-- Failed to write to the queue | ||||||||
Just False -> do | ||||||||
isFull <- liftIO $ atomically $ isFullTBMQueue queue | ||||||||
let message = | ||||||||
if isFull | ||||||||
then "Event loop is full" | ||||||||
else "Unknown error" | ||||||||
logger Katip.ErrorS $ "Failed to write to event loop: " <> message | ||||||||
exitLoopWithFailure EventLoopFull eventloop | ||||||||
|
||||||||
-- | Run the event loop until it exits with 'exitLoopWith'. | ||||||||
run :: (MonadIO m) => EventLoop event a -> (event -> m ()) -> m (Either EventLoopError a) | ||||||||
run eventloop f = do | ||||||||
run eventloop@(EventLoop {queue, shutdownLatch}) f = | ||||||||
fix $ \loop -> do | ||||||||
mevent <- liftIO $ atomically $ readTBMQueue (queue eventloop) | ||||||||
case mevent of | ||||||||
Just event -> f event | ||||||||
Nothing -> exitLoopWithFailure EventLoopClosed eventloop | ||||||||
-- Wait for either a shutdown signal or a message from the queue | ||||||||
eitherResult <- | ||||||||
liftIO $ | ||||||||
atomically $ | ||||||||
fmap Left (ShutdownLatch.waitForShutdownSTM shutdownLatch) | ||||||||
`orElse` | ||||||||
-- Try to read from queue | ||||||||
( do | ||||||||
mevent <- readTBMQueue queue | ||||||||
case mevent of | ||||||||
-- Got an event, return it | ||||||||
Just event -> return $ Right event | ||||||||
-- Queue is closed, signal shutdown | ||||||||
Nothing -> do | ||||||||
ShutdownLatch.initiateShutdownWithResultSTM (Left EventLoopClosed) shutdownLatch | ||||||||
result <- ShutdownLatch.waitForShutdownSTM shutdownLatch | ||||||||
return $ Left result | ||||||||
) | ||||||||
|
||||||||
liftIO (tryReadMVar (exitLatch eventloop)) >>= \case | ||||||||
Just exitValue -> return exitValue | ||||||||
Nothing -> loop | ||||||||
-- Process the result | ||||||||
case eitherResult of | ||||||||
-- Shutdown requested, return the result | ||||||||
Left result -> return result | ||||||||
-- Got an event, process it and continue looping | ||||||||
Right event -> do | ||||||||
f event | ||||||||
loop | ||||||||
|
||||||||
-- | Short-circuit the event loop and exit with a given return value. | ||||||||
exitLoopWith :: (MonadIO m) => a -> EventLoop event a -> m () | ||||||||
exitLoopWith exitValue (EventLoop {exitLatch}) = void $ liftIO $ tryPutMVar exitLatch (Right exitValue) | ||||||||
exitLoopWith exitValue (EventLoop {shutdownLatch}) = | ||||||||
ShutdownLatch.initiateShutdown exitValue shutdownLatch | ||||||||
|
||||||||
-- | Short-circuit the event loop in case of an internal error. | ||||||||
exitLoopWithFailure :: (MonadIO m) => EventLoopError -> EventLoop event a -> m () | ||||||||
exitLoopWithFailure err (EventLoop {exitLatch}) = void $ liftIO $ tryPutMVar exitLatch (Left err) | ||||||||
exitLoopWithFailure err (EventLoop {shutdownLatch}) = | ||||||||
ShutdownLatch.initiateShutdownWithResult (Left err) shutdownLatch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider extracting the magic number for the exit timer delay into a named constant to improve maintainability.
Copilot uses AI. Check for mistakes.