| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Polysemy.Conc
Description
Synopsis
- data Queue d :: Effect
- data QueueResult d
- interpretQueueTBM :: forall d r. Members [Resource, Race, Embed IO] r => Int -> InterpreterFor (Queue d) r
- interpretQueueTB :: forall d r. Members [Race, Embed IO] r => Natural -> InterpreterFor (Queue d) r
- interpretQueueListReadOnlyAtomic :: forall d r. Member (Embed IO) r => [d] -> InterpreterFor (Queue d) r
- interpretQueueListReadOnlyAtomicWith :: forall d r. Member (AtomicState [d]) r => InterpreterFor (Queue d) r
- interpretQueueListReadOnlyState :: forall d r. [d] -> InterpreterFor (Queue d) r
- interpretQueueListReadOnlyStateWith :: forall d r. Member (State [d]) r => InterpreterFor (Queue d) r
- resultToMaybe :: QueueResult d -> Maybe d
- loop :: Member (Queue d) r => (d -> Sem r ()) -> Sem r ()
- loopOr :: Member (Queue d) r => Sem r Bool -> (d -> Sem r Bool) -> Sem r ()
- data Sync d :: Effect
- data SyncRead (d :: Type) :: Effect
- type ScopedSync res a = Scoped (SyncResources res) (Sync a)
- interpretSync :: forall d r. Members [Race, Embed IO] r => InterpreterFor (Sync d) r
- interpretSyncAs :: forall d r. Members [Race, Embed IO] r => d -> InterpreterFor (Sync d) r
- withSync :: forall d res r. Member (ScopedSync res d) r => InterpreterFor (Sync d) r
- lock :: forall l r a. Members [Sync l, Resource] r => l -> Sem r a -> Sem r a
- interpretScopedSync :: forall d r. Members [Resource, Race, Embed IO] r => InterpreterFor (Scoped (SyncResources (MVar d)) (Sync d)) r
- interpretScopedSyncAs :: forall d r. Members [Resource, Race, Embed IO] r => d -> InterpreterFor (Scoped (SyncResources (MVar d)) (Sync d)) r
- syncRead :: forall d r. Member (Sync d) r => InterpreterFor (SyncRead d) r
- data Race :: Effect
- race :: forall a b r. Member Race r => Sem r a -> Sem r b -> Sem r (Either a b)
- race_ :: Member Race r => Sem r a -> Sem r a -> Sem r a
- timeout :: forall a b u r. TimeUnit u => Member Race r => Sem r a -> u -> Sem r b -> Sem r (Either a b)
- timeout_ :: TimeUnit u => Member Race r => Sem r a -> u -> Sem r a -> Sem r a
- timeoutAs :: TimeUnit u => Member Race r => a -> u -> Sem r b -> Sem r (Either a b)
- timeoutAs_ :: TimeUnit u => Member Race r => a -> u -> Sem r a -> Sem r a
- timeoutU :: TimeUnit u => Member Race r => u -> Sem r () -> Sem r ()
- timeoutMaybe :: TimeUnit u => Member Race r => u -> Sem r a -> Sem r (Maybe a)
- retrying :: forall e w u t d r a. TimeUnit w => TimeUnit u => Members [Race, Time t d] r => w -> u -> Sem r (Either e a) -> Sem r (Maybe a)
- retryingWithError :: forall e w u t d r a. TimeUnit w => TimeUnit u => Members [Race, Time t d, Embed IO] r => w -> u -> Sem r (Either e a) -> Sem r (Maybe (Either e a))
- interpretRace :: Member (Final IO) r => InterpreterFor Race r
- data Interrupt :: Effect
- interpretInterrupt :: Members [Critical, Race, Async, Embed IO] r => InterpreterFor Interrupt r
- interpretInterruptOnce :: Members [Critical, Race, Async, Embed IO] r => InterpreterFor Interrupt r
- interpretInterruptNull :: InterpreterFor Interrupt r
- data Events (resource :: Type) (e :: Type) :: Effect
- data Consume (e :: Type) :: Effect
- publish :: forall e resource r. Member (Events resource e) r => e -> Sem r ()
- consume :: forall e r. Member (Consume e) r => Sem r e
- subscribe :: forall e resource r. Member (Scoped (EventResource resource) (Consume e)) r => InterpreterFor (Consume e) r
- subscribeWhile :: forall e token r. Member (EventConsumer token e) r => (e -> Sem r Bool) -> Sem r ()
- subscribeLoop :: forall e token r. Member (EventConsumer token e) r => (e -> Sem r ()) -> Sem r ()
- data EventResource resource
- type EventChan e = EventResource (OutChan e)
- type ChanEvents e = Events (OutChan e) e
- type EventConsumer token e = Scoped (EventResource token) (Consume e)
- type ChanConsumer e = Scoped (EventChan e) (Consume e)
- interpretEventsChan :: forall e r. Members [Resource, Race, Async, Embed IO] r => InterpretersFor [Events (OutChan e) e, ChanConsumer e] r
- data Critical :: Effect
- interpretCritical :: Member (Final IO) r => InterpreterFor Critical r
- interpretCriticalNull :: InterpreterFor Critical r
- type Mask resource = Scoped (MaskResource resource) RestoreMask
- type UninterruptibleMask resource = Scoped (UninterruptibleMaskResource resource) RestoreMask
- mask :: forall resource r. Member (Mask resource) r => InterpreterFor RestoreMask r
- uninterruptibleMask :: forall resource r. Member (UninterruptibleMask resource) r => InterpreterFor RestoreMask r
- restore :: forall r a. Member RestoreMask r => Sem r a -> Sem r a
- data Restoration
- interpretMaskFinal :: Member (Final IO) r => InterpreterFor (Mask Restoration) r
- interpretUninterruptibleMaskFinal :: Member (Final IO) r => InterpreterFor (UninterruptibleMask Restoration) r
- data Scoped (resource :: Type) (effect :: Effect) :: Effect
- scoped :: forall resource effect r. Member (Scoped resource effect) r => InterpreterFor effect r
- runScoped :: forall resource effect r. (forall x. (resource -> Sem r x) -> Sem r x) -> (resource -> InterpreterFor effect r) -> InterpreterFor (Scoped resource effect) r
- runScopedAs :: forall resource effect r. Sem r resource -> (resource -> InterpreterFor effect r) -> InterpreterFor (Scoped resource effect) r
- interpretScoped :: forall resource effect r. (forall x. (resource -> Sem r x) -> Sem r x) -> (forall r0 x. resource -> effect (Sem r0) x -> Sem r x) -> InterpreterFor (Scoped resource effect) r
- interpretScopedH :: forall resource effect r. (forall x. (resource -> Sem r x) -> Sem r x) -> (forall r0 x. resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r x) -> InterpreterFor (Scoped resource effect) r
- interpretScopedAs :: forall resource effect r. Sem r resource -> (forall r0 x. resource -> effect (Sem r0) x -> Sem r x) -> InterpreterFor (Scoped resource effect) r
- interpretScopedResumable :: forall resource effect err r. (forall x. (resource -> Sem (Stop err ': r) x) -> Sem (Stop err ': r) x) -> (forall r0 x. resource -> effect (Sem r0) x -> Sem (Stop err ': r) x) -> InterpreterFor (Scoped resource effect !! err) r
- interpretScopedResumableH :: forall resource effect err r. (forall x. (resource -> Sem (Stop err ': r) x) -> Sem (Stop err ': r) x) -> (forall r0 x. resource -> effect (Sem r0) x -> Tactical effect (Sem r0) (Stop err ': r) x) -> InterpreterFor (Scoped resource effect !! err) r
- interpretScopedWith :: forall extra resource effect r r1. r1 ~ (extra ++ r) => InsertAtIndex 1 '[Scoped resource effect] r1 r (Scoped resource effect ': r1) extra => (forall x. (resource -> Sem r1 x) -> Sem r x) -> (forall m x. resource -> effect m x -> Sem r1 x) -> InterpreterFor (Scoped resource effect) r
- interpretScopedWithH :: forall extra resource effect r r1. r1 ~ (extra ++ r) => InsertAtIndex 1 '[Scoped resource effect] r1 r (Scoped resource effect ': r1) extra => (forall x. (resource -> Sem r1 x) -> Sem r x) -> (forall m x. resource -> effect m x -> Tactical effect m r1 x) -> InterpreterFor (Scoped resource effect) r
- interpretScopedWith_ :: forall extra effect r r1. r1 ~ (extra ++ r) => InsertAtIndex 1 '[Scoped () effect] r1 r (Scoped () effect ': r1) extra => (forall x. Sem r1 x -> Sem r x) -> (forall m x. effect m x -> Sem r1 x) -> InterpreterFor (Scoped () effect) r
- interpretScopedResumableWith :: forall extra resource effect err r r1. r1 ~ ((extra ++ '[Stop err]) ++ r) => InsertAtIndex 1 '[Scoped resource effect !! err] r1 r ((Scoped resource effect !! err) ': r1) (extra ++ '[Stop err]) => (forall x. (resource -> Sem r1 x) -> Sem (Stop err ': r) x) -> (forall r0 x. resource -> effect (Sem r0) x -> Sem r1 x) -> InterpreterFor (Scoped resource effect !! err) r
- interpretScopedResumableWithH :: forall extra resource effect err r r1. r1 ~ ((extra ++ '[Stop err]) ++ r) => InsertAtIndex 1 '[Scoped resource effect !! err] r1 r ((Scoped resource effect !! err) ': r1) (extra ++ '[Stop err]) => (forall x. (resource -> Sem r1 x) -> Sem (Stop err ': r) x) -> (forall r0 x. resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r1 x) -> InterpreterFor (Scoped resource effect !! err) r
- interpretScopedResumableWith_ :: forall extra effect err r r1. r1 ~ ((extra ++ '[Stop err]) ++ r) => InsertAtIndex 1 '[Scoped () effect !! err] r1 r ((Scoped () effect !! err) ': r1) (extra ++ '[Stop err]) => (forall x. Sem r1 x -> Sem (Stop err ': r) x) -> (forall r0 x. effect (Sem r0) x -> Sem r1 x) -> InterpreterFor (Scoped () effect !! err) r
- data Monitor (action :: Type) :: Effect
- monitor :: forall action r a. Member (Monitor action) r => Sem r a -> Sem r a
- withMonitor :: forall resource action r. Member (ScopedMonitor resource action) r => InterpreterFor (Monitor action) r
- restart :: forall resource r. Member (ScopedMonitor resource Restart) r => InterpreterFor (Monitor Restart) r
- data Restart
- type RestartingMonitor (resource :: Type) = ScopedMonitor resource Restart
- newtype MonitorResource a = MonitorResource a
- type ScopedMonitor (resource :: Type) (action :: Type) = Scoped (MonitorResource resource) (Monitor action)
- interpretMonitorRestart :: forall t d r. Members [Time t d, Resource, Async, Race, Final IO] r => MonitorCheck r -> InterpreterFor (RestartingMonitor CancelResource) r
- interpretMonitorPure :: InterpreterFor (ScopedMonitor () action) r
- monitorClockSkew :: forall t d diff r. Torsor t diff => TimeUnit diff => Members [AtomicState (Maybe t), Time t d, Embed IO] r => ClockSkewConfig -> MonitorCheck r
- data ClockSkewConfig = ClockSkewConfig NanoSeconds NanoSeconds
- clockSkewConfig :: TimeUnit u1 => TimeUnit u2 => u1 -> u2 -> ClockSkewConfig
- type ConcStack = [UninterruptibleMask Restoration, Mask Restoration, Race, Async, Resource, Embed IO, Final IO]
- runConc :: Sem ConcStack a -> IO a
- interpretAtomic :: forall a r. Member (Embed IO) r => a -> InterpreterFor (AtomicState a) r
- withAsyncBlock :: Members [Resource, Async] r => Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a
- withAsync :: Members [Resource, Race, Async] r => Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a
- withAsync_ :: Members [Resource, Race, Async] r => Sem r b -> Sem r a -> Sem r a
- scheduleAsync :: forall res b r a. Members [ScopedSync res (), Async, Race] r => Sem r b -> (Async (Maybe b) -> Sem (Sync () ': r) () -> Sem (Sync () ': r) a) -> Sem r a
- scheduleAsyncIO :: forall b r a. Members [Resource, Async, Race, Embed IO] r => Sem r b -> (Async (Maybe b) -> Sem (Sync () ': r) () -> Sem (Sync () ': r) a) -> Sem r a
Introduction
This library provides an assortment of tools for concurrency-related tasks:
Queues
data Queue d :: Effect Source #
Abstracts queues like TBQueue.
For documentation on the constructors, see the module Polysemy.Conc.Data.Queue.
import Polysemy.Conc (Queue, QueueResult)
import Polysemy.Conc.Effect.Queue as Queue
prog :: Member (Queue Int) r => Sem r (QueueResult Int)
prog = do
Queue.write 5
Queue.write 10
Queue.read >>= \case
QueueResult.Success i -> fmap (i +) <$> Queue.read
r -> pure r
Instances
| type DefiningModule Queue Source # | |
Defined in Polysemy.Conc.Effect.Queue | |
data QueueResult d Source #
Encodes failure reasons for queues.
For documentation on the constructors, see the module Polysemy.Conc.Data.QueueResult.
import qualified Polysemy.Conc.Data.QueueResult as QueueResult
Instances
Interpreters
interpretQueueListReadOnlyAtomic :: forall d r. Member (Embed IO) r => [d] -> InterpreterFor (Queue d) r Source #
Variant of interpretQueueListReadOnlyAtomicWith that interprets the AtomicState.
interpretQueueListReadOnlyAtomicWith :: forall d r. Member (AtomicState [d]) r => InterpreterFor (Queue d) r Source #
Reinterpret Queue as AtomicState with a list that cannot be written to.
Useful for testing.
interpretQueueListReadOnlyState :: forall d r. [d] -> InterpreterFor (Queue d) r Source #
Variant of interpretQueueListReadOnlyAtomicWith that interprets the State.
interpretQueueListReadOnlyStateWith :: forall d r. Member (State [d]) r => InterpreterFor (Queue d) r Source #
Combinators
resultToMaybe :: QueueResult d -> Maybe d Source #
loop :: Member (Queue d) r => (d -> Sem r ()) -> Sem r () Source #
Read from a Queue repeatedly until it is closed.
When an element is received, call action and recurse.
MVars
data Sync d :: Effect Source #
Abstracts an MVar.
For documentation on the constructors, see the module Polysemy.Conc.Effect.Sync.
import Polysemy.Conc (Sync) import qualified Polysemy.Conc.Effect.Sync as Sync prog :: Member (Sync Int) r => Sem r Int prog = do Sync.putTry 5 Sync.takeBlock
Instances
| type DefiningModule Sync Source # | |
Defined in Polysemy.Conc.Effect.Sync | |
data SyncRead (d :: Type) :: Effect Source #
An interface to a shared variable (MVar) that can only be read.
Instances
| type DefiningModule SyncRead Source # | |
Defined in Polysemy.Conc.Effect.SyncRead | |
type ScopedSync res a = Scoped (SyncResources res) (Sync a) Source #
Convenience alias.
Interpreters
interpretSync :: forall d r. Members [Race, Embed IO] r => InterpreterFor (Sync d) r Source #
interpretSyncAs :: forall d r. Members [Race, Embed IO] r => d -> InterpreterFor (Sync d) r Source #
withSync :: forall d res r. Member (ScopedSync res d) r => InterpreterFor (Sync d) r Source #
lock :: forall l r a. Members [Sync l, Resource] r => l -> Sem r a -> Sem r a Source #
Run the action ma with an exclusive lock (mutex).
When multiple threads call the action concurrently, only one is allowed to execute it at a time.
The value l is used to disambiguate the Sync from other uses of the combinator.
You can pass in something like Proxy "db-write"@.
Note: The Sync must be interpreted with an initially full MVar, e.g. using interpretSyncAs.
interpretScopedSync :: forall d r. Members [Resource, Race, Embed IO] r => InterpreterFor (Scoped (SyncResources (MVar d)) (Sync d)) r Source #
interpretScopedSyncAs :: forall d r. Members [Resource, Race, Embed IO] r => d -> InterpreterFor (Scoped (SyncResources (MVar d)) (Sync d)) r Source #
Racing
prog = Polysemy.Conc.race (httpRequest "hackage.haskell.org") (readFile "/path/to/file") >>= \case Left _ -> putStrLn "hackage was faster" Right _ -> putStrLn "file was faster"
When the first thunk finishes, the other will be killed.
Abstract the concept of running two programs concurrently, aborting the other when one terminates.
Timeout is a simpler variant, where one thread just sleeps for a given interval.
Instances
| type DefiningModule Race Source # | |
Defined in Polysemy.Conc.Effect.Race | |
race :: forall a b r. Member Race r => Sem r a -> Sem r b -> Sem r (Either a b) Source #
Run both programs concurrently, returning the result of the faster one.
timeout :: forall a b u r. TimeUnit u => Member Race r => Sem r a -> u -> Sem r b -> Sem r (Either a b) Source #
Run the fallback action if the given program doesn't finish within the specified interval.
timeoutAs :: TimeUnit u => Member Race r => a -> u -> Sem r b -> Sem r (Either a b) Source #
Version of timeout that takes a pure fallback value.
timeoutU :: TimeUnit u => Member Race r => u -> Sem r () -> Sem r () Source #
Specialization of timeout for unit actions.
Arguments
| :: forall e w u t d r a. TimeUnit w | |
| => TimeUnit u | |
| => Members [Race, Time t d] r | |
| => w | The timeout after which the attempt is abandoned. |
| -> u | The waiting interval between two tries. |
| -> Sem r (Either e a) | |
| -> Sem r (Maybe a) |
Run an action repeatedly until it returns Right or the timout has been exceeded.
Arguments
| :: forall e w u t d r a. TimeUnit w | |
| => TimeUnit u | |
| => Members [Race, Time t d, Embed IO] r | |
| => w | The timeout after which the attempt is abandoned. |
| -> u | The waiting interval between two tries. |
| -> Sem r (Either e a) | |
| -> Sem r (Maybe (Either e a)) |
Run an action repeatedly until it returns Right or the timout has been exceeded.
If the action failed at least once, the last error will be returned in case of timeout.
Interpreters
interpretRace :: Member (Final IO) r => InterpreterFor Race r Source #
Signal Handling
data Interrupt :: Effect Source #
The interrupt handler effect allows three kinds of interaction for interrupt signals:
- Execute a callback when a signal is received
- Block a thread until a signal is received
- Kill a thread when a signal is received
For documentation on the constructors, see the module Polysemy.Conc.Effect.Interrupt.
import qualified Polysemy.Conc.Effect.Interrupt as Interrupt prog = do Interrupt.register "task 1" (putStrLn "interrupted") Interrupt.killOnQuit $ forever do doSomeWork
Instances
| type DefiningModule Interrupt Source # | |
Defined in Polysemy.Conc.Effect.Interrupt | |
Interpreters
interpretInterrupt :: Members [Critical, Race, Async, Embed IO] r => InterpreterFor Interrupt r Source #
Interpret Interrupt by installing a signal handler.
Catches repeat invocations of SIGINT.
interpretInterruptOnce :: Members [Critical, Race, Async, Embed IO] r => InterpreterFor Interrupt r Source #
Interpret Interrupt by installing a signal handler.
Catches only the first invocation of SIGINT.
interpretInterruptNull :: InterpreterFor Interrupt r Source #
Eliminate Interrupt without interpreting.
Event Channels
data Events (resource :: Type) (e :: Type) :: Effect Source #
An event publisher that can be consumed from multiple threads.
Instances
| type DefiningModule Events Source # | |
Defined in Polysemy.Conc.Effect.Events | |
data Consume (e :: Type) :: Effect Source #
Consume events emitted by Events.
Instances
| type DefiningModule Consume Source # | |
Defined in Polysemy.Conc.Effect.Events | |
publish :: forall e resource r. Member (Events resource e) r => e -> Sem r () Source #
Publish one event.
subscribe :: forall e resource r. Member (Scoped (EventResource resource) (Consume e)) r => InterpreterFor (Consume e) r Source #
Create a new scope for Events, causing the nested program to get its own copy of the event stream.
To be used with interpretEventsChan.
subscribeWhile :: forall e token r. Member (EventConsumer token e) r => (e -> Sem r Bool) -> Sem r () Source #
Pull repeatedly from the Events channel, passing the event to the supplied callback.
Stop when the action returns False.
subscribeLoop :: forall e token r. Member (EventConsumer token e) r => (e -> Sem r ()) -> Sem r () Source #
Pull repeatedly from the Events channel, passing the event to the supplied callback.
data EventResource resource Source #
Instances
| Generic (EventResource resource) Source # | |
Defined in Polysemy.Conc.Effect.Events Associated Types type Rep (EventResource resource) :: Type -> Type # Methods from :: EventResource resource -> Rep (EventResource resource) x # to :: Rep (EventResource resource) x -> EventResource resource # | |
| Show resource => Show (EventResource resource) Source # | |
Defined in Polysemy.Conc.Effect.Events Methods showsPrec :: Int -> EventResource resource -> ShowS # show :: EventResource resource -> String # showList :: [EventResource resource] -> ShowS # | |
| Eq resource => Eq (EventResource resource) Source # | |
Defined in Polysemy.Conc.Effect.Events Methods (==) :: EventResource resource -> EventResource resource -> Bool # (/=) :: EventResource resource -> EventResource resource -> Bool # | |
| type Rep (EventResource resource) Source # | |
Defined in Polysemy.Conc.Effect.Events type Rep (EventResource resource) = D1 ('MetaData "EventResource" "Polysemy.Conc.Effect.Events" "polysemy-conc-0.9.0.0-EjHtQDNYbxi3AMXC1n8iKe" 'True) (C1 ('MetaCons "EventResource" 'PrefixI 'True) (S1 ('MetaSel ('Just "unEventToken") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 resource))) | |
type EventChan e = EventResource (OutChan e) Source #
Convenience alias for the default EventResource that uses an OutChan.
type ChanEvents e = Events (OutChan e) e Source #
type EventConsumer token e = Scoped (EventResource token) (Consume e) Source #
Convenience alias for the consumer effect.
type ChanConsumer e = Scoped (EventChan e) (Consume e) Source #
Convenience alias for the consumer effect using the default implementation.
Interpreters
interpretEventsChan :: forall e r. Members [Resource, Race, Async, Embed IO] r => InterpretersFor [Events (OutChan e) e, ChanConsumer e] r Source #
Interpret Events and Consume together by connecting them to the two ends of an unagi channel.
Consume is only interpreted in a Scoped manner, ensuring that a new duplicate of the channel is created so that
all consumers see all events (from the moment they are connected).
This should be used in conjunction with subscribe:
interpretEventsChan do
async $ subscribe do
putStrLn =<< consume
publish "hello"
Whenever subscribe creates a new scope, this interpreter calls dupChan and passes the
duplicate to interpretConsumeChan.
Exceptions
data Critical :: Effect Source #
An effect that catches exceptions.
Provides the exact functionality of fromExceptionSem, but pushes the dependency on Final IO to the
interpreter, and makes it optional.
Instances
| type DefiningModule Critical Source # | |
Defined in Polysemy.Conc.Effect.Critical | |
Interpreters
interpretCritical :: Member (Final IO) r => InterpreterFor Critical r Source #
interpretCriticalNull :: InterpreterFor Critical r Source #
Interpret Critical by doing nothing.
Masking
type Mask resource = Scoped (MaskResource resource) RestoreMask Source #
The scoped masking effect.
type UninterruptibleMask resource = Scoped (UninterruptibleMaskResource resource) RestoreMask Source #
The scoped uninterruptible masking effect.
mask :: forall resource r. Member (Mask resource) r => InterpreterFor RestoreMask r Source #
Mark a region as masked.
Uses the Scoped pattern.
uninterruptibleMask :: forall resource r. Member (UninterruptibleMask resource) r => InterpreterFor RestoreMask r Source #
Mark a region as uninterruptibly masked.
Uses the Scoped pattern.
restore :: forall r a. Member RestoreMask r => Sem r a -> Sem r a Source #
Restore the previous masking state.
Can only be called inside of an action passed to mask or uninterruptibleMask.
data Restoration Source #
Interpreters
interpretMaskFinal :: Member (Final IO) r => InterpreterFor (Mask Restoration) r Source #
interpretUninterruptibleMaskFinal :: Member (Final IO) r => InterpreterFor (UninterruptibleMask Restoration) r Source #
Interpret UninterruptibleMask in IO.
Scoped Effects
data Scoped (resource :: Type) (effect :: Effect) :: Effect Source #
Scoped transforms a program so that effect is associated with a resource within that program.
This requires the interpreter for effect to be parameterized by resource and constructed for every program using
Scoped separately.
An application for this is Events, in which each program using the effect Consume is
interpreted with its own copy of the event channel; or a database transaction, in which a transaction handle is
created for the wrapped program and passed to the interpreter for the database effect.
Resource creation is performed by the function passed to runScoped.
The constructors are not intended to be used directly; the smart constructor scoped is used like a local
interpreter for effect.
scoped :: forall resource effect r. Member (Scoped resource effect) r => InterpreterFor effect r Source #
Constructor for Scoped, taking a nested program and transforming all instances of effect to
Scoped resource effect.
Interpreters
runScoped :: forall resource effect r. (forall x. (resource -> Sem r x) -> Sem r x) -> (resource -> InterpreterFor effect r) -> InterpreterFor (Scoped resource effect) r Source #
Interpreter for Scoped, taking a resource allocation function and a parameterized interpreter for the plain
effect.
withResource is a callback function, allowing the user to acquire the resource for each program from other effects.
scopedInterpreter is a regular interpreter that is called with the resource argument produced by scope.
Note: This function will be called for each action in the program, so if the interpreter allocates any resources,
they will be scoped to a single action. Move them to withResource instead.
runScopedAs :: forall resource effect r. Sem r resource -> (resource -> InterpreterFor effect r) -> InterpreterFor (Scoped resource effect) r Source #
Variant of runScoped in which the resource allocator is a plain action.
interpretScoped :: forall resource effect r. (forall x. (resource -> Sem r x) -> Sem r x) -> (forall r0 x. resource -> effect (Sem r0) x -> Sem r x) -> InterpreterFor (Scoped resource effect) r Source #
Variant of runScoped that takes a handler instead of an interpreter.
interpretScopedH :: forall resource effect r. (forall x. (resource -> Sem r x) -> Sem r x) -> (forall r0 x. resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r x) -> InterpreterFor (Scoped resource effect) r Source #
Variant of runScoped that takes a higher-order handler instead of an interpreter.
interpretScopedAs :: forall resource effect r. Sem r resource -> (forall r0 x. resource -> effect (Sem r0) x -> Sem r x) -> InterpreterFor (Scoped resource effect) r Source #
Variant of interpretScoped in which the resource allocator is a plain action.
interpretScopedResumable :: forall resource effect err r. (forall x. (resource -> Sem (Stop err ': r) x) -> Sem (Stop err ': r) x) -> (forall r0 x. resource -> effect (Sem r0) x -> Sem (Stop err ': r) x) -> InterpreterFor (Scoped resource effect !! err) r Source #
interpretScopedResumableH :: forall resource effect err r. (forall x. (resource -> Sem (Stop err ': r) x) -> Sem (Stop err ': r) x) -> (forall r0 x. resource -> effect (Sem r0) x -> Tactical effect (Sem r0) (Stop err ': r) x) -> InterpreterFor (Scoped resource effect !! err) r Source #
interpretScopedWith :: forall extra resource effect r r1. r1 ~ (extra ++ r) => InsertAtIndex 1 '[Scoped resource effect] r1 r (Scoped resource effect ': r1) extra => (forall x. (resource -> Sem r1 x) -> Sem r x) -> (forall m x. resource -> effect m x -> Sem r1 x) -> InterpreterFor (Scoped resource effect) r Source #
interpretScopedWithH :: forall extra resource effect r r1. r1 ~ (extra ++ r) => InsertAtIndex 1 '[Scoped resource effect] r1 r (Scoped resource effect ': r1) extra => (forall x. (resource -> Sem r1 x) -> Sem r x) -> (forall m x. resource -> effect m x -> Tactical effect m r1 x) -> InterpreterFor (Scoped resource effect) r Source #
interpretScopedWith_ :: forall extra effect r r1. r1 ~ (extra ++ r) => InsertAtIndex 1 '[Scoped () effect] r1 r (Scoped () effect ': r1) extra => (forall x. Sem r1 x -> Sem r x) -> (forall m x. effect m x -> Sem r1 x) -> InterpreterFor (Scoped () effect) r Source #
interpretScopedResumableWith :: forall extra resource effect err r r1. r1 ~ ((extra ++ '[Stop err]) ++ r) => InsertAtIndex 1 '[Scoped resource effect !! err] r1 r ((Scoped resource effect !! err) ': r1) (extra ++ '[Stop err]) => (forall x. (resource -> Sem r1 x) -> Sem (Stop err ': r) x) -> (forall r0 x. resource -> effect (Sem r0) x -> Sem r1 x) -> InterpreterFor (Scoped resource effect !! err) r Source #
interpretScopedResumableWithH :: forall extra resource effect err r r1. r1 ~ ((extra ++ '[Stop err]) ++ r) => InsertAtIndex 1 '[Scoped resource effect !! err] r1 r ((Scoped resource effect !! err) ': r1) (extra ++ '[Stop err]) => (forall x. (resource -> Sem r1 x) -> Sem (Stop err ': r) x) -> (forall r0 x. resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r1 x) -> InterpreterFor (Scoped resource effect !! err) r Source #
interpretScopedResumableWith_ :: forall extra effect err r r1. r1 ~ ((extra ++ '[Stop err]) ++ r) => InsertAtIndex 1 '[Scoped () effect !! err] r1 r ((Scoped () effect !! err) ': r1) (extra ++ '[Stop err]) => (forall x. Sem r1 x -> Sem (Stop err ': r) x) -> (forall r0 x. effect (Sem r0) x -> Sem r1 x) -> InterpreterFor (Scoped () effect !! err) r Source #
Monitoring
data Monitor (action :: Type) :: Effect Source #
Mark a region as being subject to intervention by a monitoring program.
This can mean that a thread is repeatedly checking a condition and cancelling this region when it is unmet.
A use case could be checking whether a remote service is available, or whether the system was suspended and resumed.
This should be used in a Scoped context, like withMonitor.
Instances
| type DefiningModule Monitor Source # | |
Defined in Polysemy.Conc.Effect.Monitor | |
monitor :: forall action r a. Member (Monitor action) r => Sem r a -> Sem r a Source #
Mark a region as being subject to intervention by a monitoring program.
withMonitor :: forall resource action r. Member (ScopedMonitor resource action) r => InterpreterFor (Monitor action) r Source #
Start a region that can contain monitor-intervention regions.
restart :: forall resource r. Member (ScopedMonitor resource Restart) r => InterpreterFor (Monitor Restart) r Source #
Variant of withMonitor that uses the Restart strategy.
Marker type for the restarting action for Monitor.
type RestartingMonitor (resource :: Type) = ScopedMonitor resource Restart Source #
type ScopedMonitor (resource :: Type) (action :: Type) = Scoped (MonitorResource resource) (Monitor action) Source #
Interpreters
interpretMonitorRestart :: forall t d r. Members [Time t d, Resource, Async, Race, Final IO] r => MonitorCheck r -> InterpreterFor (RestartingMonitor CancelResource) r Source #
Interpret with the Scoped MonitorRestart strategy.
This takes a check action that may put an MVar when the scoped region should be restarted.
The check is executed in a loop, with an interval given in MonitorCheck.
interpretMonitorPure :: InterpreterFor (ScopedMonitor () action) r Source #
Run Monitor as a no-op.
monitorClockSkew :: forall t d diff r. Torsor t diff => TimeUnit diff => Members [AtomicState (Maybe t), Time t d, Embed IO] r => ClockSkewConfig -> MonitorCheck r Source #
Check for Monitor that checks every interval whether the difference between the current
time and the time at the last check is larger than interval + tolerance.
Can be used to detect that the operating system suspended and resumed.
data ClockSkewConfig Source #
Config for monitorClockSkew.
Constructors
| ClockSkewConfig NanoSeconds NanoSeconds |
Instances
| Show ClockSkewConfig Source # | |
Defined in Polysemy.Conc.Monitor Methods showsPrec :: Int -> ClockSkewConfig -> ShowS # show :: ClockSkewConfig -> String # showList :: [ClockSkewConfig] -> ShowS # | |
| Default ClockSkewConfig Source # | |
Defined in Polysemy.Conc.Monitor Methods def :: ClockSkewConfig # | |
| Eq ClockSkewConfig Source # | |
Defined in Polysemy.Conc.Monitor Methods (==) :: ClockSkewConfig -> ClockSkewConfig -> Bool # (/=) :: ClockSkewConfig -> ClockSkewConfig -> Bool # | |
clockSkewConfig :: TimeUnit u1 => TimeUnit u2 => u1 -> u2 -> ClockSkewConfig Source #
Smart constructor for ClockSkewConfig that takes arbitrary TimeUnits.
Other Combinators
type ConcStack = [UninterruptibleMask Restoration, Mask Restoration, Race, Async, Resource, Embed IO, Final IO] Source #
A default basic stack with Final for _polysemy-conc_.
runConc :: Sem ConcStack a -> IO a Source #
Interprets UninterruptibleMask, Mask and Race in terms of and runs the entire rest of the
stack.Final IO
interpretAtomic :: forall a r. Member (Embed IO) r => a -> InterpreterFor (AtomicState a) r Source #
Convenience wrapper around runAtomicStateTVar that creates a new TVar.
withAsyncBlock :: Members [Resource, Async] r => Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a Source #
Run the first action asynchronously while the second action executes, then cancel the first action. Passes the handle into the action to allow it to await its result.
When cancelling, this variant will wait indefinitely for the thread to be gone.
withAsync :: Members [Resource, Race, Async] r => Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a Source #
Run the first action asynchronously while the second action executes, then cancel the first action. Passes the handle into the action to allow it to await its result.
When cancelling, this variant will wait for 500ms for the thread to be gone.
withAsync_ :: Members [Resource, Race, Async] r => Sem r b -> Sem r a -> Sem r a Source #
Run the first action asynchronously while the second action executes, then cancel the first action. Discards the handle, expecting the async action to either terminate or be cancelled.
When cancelling, this variant will wait for 500ms for the thread to be gone.
scheduleAsync :: forall res b r a. Members [ScopedSync res (), Async, Race] r => Sem r b -> (Async (Maybe b) -> Sem (Sync () ': r) () -> Sem (Sync () ': r) a) -> Sem r a Source #
Run an action with async, but don't start it right away, so the thread handle can be processed before the action
executes.
Takes a callback function that is invoked after spawning the thread.
The callback receives the Async handle and a unit action that starts the computation.
This is helpful if the Async has to be stored in state and the same state is written when the action finishes.
In that case, the race condition causes the handle to be written over the finished state.
makeRequest = put Nothing main = scheduleAsync makeRequest handle start -> do put (Just handle) start -- now makeRequest is executed