1module Test.SharedState where
5import Control.Monad.Reader.Trans (ask)
8import Effect.Aff (Aff, launchAff_)
9import Effect.Class (liftEffect)
10import Effect.Class.Console as Console
11import Node.Path as Path
12import Node.Process as Process
13import Node.WorkerBees as WB
14import Node.WorkerBees.Aff.Pool as Pool
15import Yoga.Fastify.Fastify (Host(..), Port(..))
16import Yoga.Fastify.Fastify as F
17import Yoga.HTTP.API.Path (type (/), type (:))
18import Yoga.Fastify.Om.Route (GET, Route, Request, Handler, handle, respond, reject)
19import Yoga.Fastify.Om.API (registerAPI)
21import Yoga.Om.WorkerBees.SharedInt as SharedInt
25type CounterInput = { n :: Int }
26type CounterOutput = { result :: Int, count :: Int }
30type FibRoute = Route GET
33 ( ok :: { body :: { n :: Int, result :: Int, totalProcessed :: Int } }
34 , badRequest :: { body :: { error :: String } }
37type CountRoute = Route GET
40 ( ok :: { body :: { totalProcessed :: Int } }
50fibHandler :: Pool.WorkerPool CounterInput CounterOutput -> Handler FibRoute ()
51fibHandler pool = handle do
52 { path: { n } } <- ask
54 reject @"badRequest" { error: "n must be non-negative" }
56 reject @"badRequest" { error: "n must be <= 40" }
57 { result, count } <- Om.fromAff $ Pool.invoke pool { n }
58 respond @"ok" { n, result, totalProcessed: count }
60countHandler :: SharedInt.SharedInt -> Handler CountRoute ()
61countHandler counter = handle do
62 totalProcessed <- Om.fromAff $ liftEffect $ SharedInt.read counter
63 respond @"ok" { totalProcessed }
67startServer :: Aff Unit
69 cwd <- liftEffect Process.cwd
70 let workerPath = Path.concat [ cwd, "dist", "workers", "CounterWorker.js" ]
72 -- Create shared counter visible to all workers
73 counter <- liftEffect $ SharedInt.new 0
74 Console.log "Created shared counter (initial: 0)"
76 -- Create worker pool, passing the shared counter as workerData
77 let worker = WB.unsafeWorkerFromPath workerPath
78 pool <- Pool.make worker (SharedInt.toSendable counter) 4
79 Console.log "Worker pool started (4 workers with shared counter)"
81 app <- liftEffect $ F.fastify {}
82 liftEffect $ registerAPI @API
83 { fib: fibHandler pool
84 , count: countHandler counter
89 Console.log $ " curl http://localhost:" <> show port <> "/fib/30"
90 Console.log $ " curl http://localhost:" <> show port <> "/count"
92 address <- F.listen { port: Port port, host: Host "0.0.0.0" } app
93 Console.log $ "Listening on " <> address
96main = launchAff_ startServer
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list