-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deliver status information using server-sent events (#1131)
* POC of delivering status information using server-sent events * deliver only events instead of status lines * Stream JSON events * Hide event server behind config --------- Co-authored-by: Artur Cygan <[email protected]>
- Loading branch information
1 parent
e0d243a
commit 988bda7
Showing
10 changed files
with
125 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Echidna.Server where | ||
|
||
import Control.Concurrent | ||
import Control.Monad (when, void) | ||
import Data.Aeson | ||
import Data.Binary.Builder (fromLazyByteString) | ||
import Data.IORef | ||
import Data.Time (LocalTime) | ||
import Data.Word (Word16) | ||
import Network.Wai.EventSource (ServerEvent(..), eventSourceAppIO) | ||
import Network.Wai.Handler.Warp (run) | ||
|
||
import Echidna.Types.Campaign (CampaignEvent (..)) | ||
import Echidna.Types.Config (Env(..)) | ||
|
||
newtype SSE = SSE (Int, LocalTime, CampaignEvent) | ||
|
||
instance ToJSON SSE where | ||
toJSON (SSE (workerId, time, event)) = | ||
object [ "worker" .= workerId | ||
, "timestamp" .= time | ||
, "data" .= event | ||
] | ||
|
||
runSSEServer :: MVar () -> Env -> Word16 -> Int -> IO () | ||
runSSEServer serverStopVar env port nworkers = do | ||
aliveRef <- newIORef nworkers | ||
sseChan <- dupChan env.eventQueue | ||
|
||
let sseListener = do | ||
aliveNow <- readIORef aliveRef | ||
if aliveNow == 0 then | ||
pure CloseEvent | ||
else do | ||
event@(_, _, campaignEvent) <- readChan sseChan | ||
let eventName = \case | ||
TestFalsified _ -> "test_falsified" | ||
TestOptimized _ -> "test_optimized" | ||
NewCoverage {} -> "new_coverage" | ||
TxSequenceReplayed _ _ -> "tx_sequence_replayed" | ||
WorkerStopped _ -> "worker_stopped" | ||
case campaignEvent of | ||
WorkerStopped _ -> do | ||
aliveAfter <- atomicModifyIORef' aliveRef (\n -> (n-1, n-1)) | ||
when (aliveAfter == 0) $ putMVar serverStopVar () | ||
_ -> pure () | ||
pure $ ServerEvent | ||
{ eventName = Just (eventName campaignEvent) | ||
, eventId = Nothing | ||
, eventData = [ fromLazyByteString $ encode (SSE event) ] | ||
} | ||
|
||
void . forkIO $ do | ||
run (fromIntegral port) $ eventSourceAppIO sseListener |
This file contains 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -89,3 +89,5 @@ rpcUrl: null | |
rpcBlock: null | ||
# number of workers | ||
workers: 1 | ||
# events server port | ||
server: null |