diff options
author | Lia Lenckowski <lialenck@protonmail.com> | 2023-08-01 00:55:12 +0200 |
---|---|---|
committer | Lia Lenckowski <lialenck@protonmail.com> | 2023-08-01 00:55:12 +0200 |
commit | 180ca78de50195a176b8125bd364838e905cc241 (patch) | |
tree | 99dfdafdf91493dbffb97ef8017e0abb21025e37 /src/BangState.hs | |
parent | f7f0d213af979531224cfd1a5f1148e3f0803adc (diff) | |
download | fastbangs-180ca78de50195a176b8125bd364838e905cc241.tar fastbangs-180ca78de50195a176b8125bd364838e905cc241.tar.bz2 fastbangs-180ca78de50195a176b8125bd364838e905cc241.tar.zst |
slightly faster & simpler file synchronization notification
Diffstat (limited to 'src/BangState.hs')
-rw-r--r-- | src/BangState.hs | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/src/BangState.hs b/src/BangState.hs index 019f219..fabd9e7 100644 --- a/src/BangState.hs +++ b/src/BangState.hs @@ -11,7 +11,7 @@ module BangState ( import Codec.Compression.Brotli (compress) import Control.Applicative ((<|>)) -import Control.Concurrent.STM.TChan +import Control.Concurrent.STM.TQueue import Control.Concurrent.STM.TVar import Control.Monad (forever) import Crypto.Hash (hashlazy, Digest, SHA512) @@ -32,7 +32,7 @@ data BangState = BangState { serializedBangs :: TVar ByteString, brotliBangs :: TVar ByteString, serializedHash :: TVar Text, - syncFileNotifications :: TChan () + syncFileNotifications :: TQueue () } reserialize :: BangState -> STM () @@ -46,18 +46,16 @@ reserialize s = do -- hash: for now, base64 of the first 120 bits of the SHA512 of the bangs writeTVar (serializedHash s) $ decodeASCII $ Data.ByteString.take 20 $ convertToBase Base64 $ (hashlazy jsonBs :: Digest SHA512) - writeTChan (syncFileNotifications s) () + writeTQueue (syncFileNotifications s) () -- spawns a thread for syncing the current state to disk. do *NOT* use forkIO on this -- function, it does so internally, and using forkIO may result in a (hard to trigger) -- race condition spawnFileSyncThread :: BangState -> IO () spawnFileSyncThread s = do - readableChan <- atomically $ dupTChan (syncFileNotifications s) - _ <- forkIO $ forever $ do nextBangs <- atomically $ do - readTChan readableChan + readTQueue $ syncFileNotifications s readTVar (ownBangs s) atomicWriteFile "bangs.json" $ encode nextBangs @@ -79,7 +77,7 @@ initBangState = do <*> newTVarIO empty -- initially filled in by 'spawnFileSyncThread' <*> newTVarIO empty -- same as above <*> newTVarIO "" -- same as above - <*> newBroadcastTChanIO + <*> newTQueueIO spawnFileSyncThread s return s |