Safe Haskell | None |
---|---|
Language | Haskell2010 |
Attempt to reproduce Erlang pattern of resource owner process gen_server.
Original documentation: http://www.erlang.org/doc/man/gen_server.html
- class GenServerState req res s | s -> req, s -> res where
- handle_call :: req -> Unique -> GenServerM s req res (CallResult res)
- handle_cast :: req -> GenServerM s req res CastResult
- onTerminate :: s -> IO ()
- data GenServer req res
- type RequestId = Unique
- data StartStatus req res
- data ServerIsDead = ServerIsDead
- data CallResult res
- data CastResult
- start :: GenServerState req res s => Process (Request req res) s -> IO (StartStatus req res)
- call :: GenServer req res -> req -> IO res
- cast :: GenServer req res -> req -> IO ()
- replyWith :: RequestId -> res -> GenServerM s req res ()
- reply :: res -> CallResult res
- noreply :: HandlerResult a => a
- stop :: HandlerResult a => String -> a
- replyAndStop :: res -> String -> CallResult res
- callWithTimeout :: GenServer req res -> Maybe Int -> req -> IO (Maybe res)
Documentation
class GenServerState req res s | s -> req, s -> res where Source
Describe GenServer callbacks.
handle_call :: req -> Unique -> GenServerM s req res (CallResult res) Source
Triggered when somebody send request to GenServer by call
function.
Executed in State monad, and state of GenServer can be changed by implementation.
Callback accept request and Unique
id of request.
Return CallResult
(one of: reply
, noreply
, stop
, replyAndStop
).
If callback retrun noreply
caller process will be blocked until GenServer will explicity call replyWith
.
If callback return reply
or replyAndStop
caller will receive response and continue execution.
If callback retrun stop
server will be terminated, onTerminate
callback called.
Exception ProcessIsDead
will be raised in caller process.
handle_cast :: req -> GenServerM s req res CastResult Source
Triggered when somebody send asyncronous command to GenServer by cast
function.
Executed in State monad, and state of GenServer can be changed by implementation.
Callback accept request and return CallResult
(one of: noreply
, stop
).
Used for change server state without any response.
onTerminate :: s -> IO () Source
Termination callback executed on GenServer termination. This callback usefull for release resources allocated on server start.
data StartStatus req res Source
data ServerIsDead Source
data CallResult res Source
Returned by handle_call
callback.
data CastResult Source
Returned by handle_cast
callback.
start :: GenServerState req res s => Process (Request req res) s -> IO (StartStatus req res) Source
Start instance of GenServer.
Take action for initialization of state. And return StartStatus
.
If start
returns Fail, initialization was failed. In that case onTerminate callback will not be called.
call :: GenServer req res -> req -> IO res Source
Send synchronous request to GenServer.
call
block caller process until GenServer will reply.
If GenServer instance is not alive or will be terminated during call
ServerIsDead
exception will be raised.
cast :: GenServer req res -> req -> IO () Source
Send asynchronous request to GenServer.
cast
doesn't block caller thread.
replyWith :: RequestId -> res -> GenServerM s req res () Source
Reply to process, which waits response from server.
reply :: res -> CallResult res Source
replyAndStop :: res -> String -> CallResult res Source
callWithTimeout :: GenServer req res -> Maybe Int -> req -> IO (Maybe res) Source
Send synchronous request to GenServer with timeout.
call
block caller process until GenServer will reply or timeout will be expired.
If GenServer instance is not alive or will be terminated during call
ServerIsDead
exception will be raised.
If Timeout will be expired, Nothing
will be returned.