Skip to content

Commit

Permalink
Use threadDelay instead of threadWaitRead to poll for notification on…
Browse files Browse the repository at this point in the history
… windows since it seems to be a more robust approach.
  • Loading branch information
diogob committed Aug 19, 2023
1 parent d6a3cfa commit 243f01b
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Hasql/Notifications.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-|
This module has functions to send commands LISTEN and NOTIFY to the database server.
It also has a function to wait for and handle notifications on a database connection.
Expand Down Expand Up @@ -30,7 +31,11 @@ import qualified Data.Text.Encoding as T
import Data.ByteString.Char8 (ByteString)
import Data.Functor.Contravariant (contramap)
import Control.Monad (void, forever)
#if defined(mingw32_HOST_OS)
import Control.Concurrent ( threadDelay )
#else
import Control.Concurrent (threadWaitRead)
#endif
import Control.Exception (Exception, throw)

-- | A wrapped text that represents a properly escaped and quoted PostgreSQL identifier
Expand Down Expand Up @@ -160,8 +165,13 @@ waitForNotifications sendNotification con =
mfd <- PQ.socket pqCon
case mfd of
Nothing -> panic "Error checking for PostgreSQL notifications"
#if defined(mingw32_HOST_OS)
Just _ -> do
void $ threadDelay 1000000
#else
Just fd -> do
void $ threadWaitRead fd
#endif
void $ PQ.consumeInput pqCon
Just notification ->
sendNotification (PQ.notifyRelname notification) (PQ.notifyExtra notification)
Expand Down

0 comments on commit 243f01b

Please sign in to comment.