Skip to content

Commit

Permalink
feat(sut): add worker that works in batches
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Feb 18, 2022
1 parent 5bbc585 commit 8a7a044
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/sut/dumblog/src/Dumblog/SQLite/Worker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ module Dumblog.SQLite.Worker where

import Control.Concurrent.MVar (putMVar)
import Control.Concurrent.STM (atomically)
import Control.Concurrent.STM.TBQueue (TBQueue, readTBQueue)
import Control.Concurrent.STM.TBQueue
(TBQueue, flushTBQueue, readTBQueue)

import Dumblog.SQLite.DB
import Dumblog.SQLite.Command
import Dumblog.SQLite.DB

------------------------------------------------------------------------

Expand All @@ -15,11 +16,22 @@ worker queue conn = go
go :: IO ()
go = do
cmd <- atomically (readTBQueue queue)
case cmd of
Read ix response -> do
bs <- readDB conn ix
putMVar response bs
Write bs response -> do
ix <- writeDB conn bs
putMVar response ix
execute conn cmd
go

batchingWorker :: TBQueue Command -> Connection -> IO ()
batchingWorker queue conn = go
where
go :: IO ()
go = do
cmds <- atomically (flushTBQueue queue)
mapM_ (execute conn) cmds
go

execute :: Connection -> Command -> IO ()
execute conn (Read ix response) = do
bs <- readDB conn ix
putMVar response bs
execute conn (Write bs response) = do
ix <- writeDB conn bs
putMVar response ix

0 comments on commit 8a7a044

Please sign in to comment.