otplike.gen-server

gen-server behaviour and related functions.

call

(call server request)(call server request timeout-ms)

The same as call! but returns async value.

call!

macro

(call! server request)(call! server request timeout-ms)

Makes a synchronous call to a server by sending a request and waiting until a reply arrives or a time-out occurs. The handle-call callback of the gen-server is called to handle the request.

server can be a pid or a registered name.

request is any form that is passed as the request arguments to handle-call.

timeout-ms is an integer greater than zero that specifies how many milliseconds to wait for a reply, or the keyword :infinity to wait indefinitely. Defaults to 5000. If no reply is received within the specified time, the function call fails. If the caller catches the failure and continues running, and the server is just late with the reply, it can arrive at any time later into the message queue of the caller. The caller must in this case be prepared for this and discard any such garbage messages that are two element tuples with a reference as the first element.

The return value is defined in the return value of handle-call.

The call can fail for many reasons, including time-out and the called gen-server process dying before or during the call.

cast

(cast server request)

Sends an asynchronous request to the server and returns immediately, ignoring if the server process does not exist. The handle-cast callback of the gen-server is called to handle the request.

request is any form that is passed as the request argument to handle-cast.

IGenServer

protocol

members

handle-call

(handle-call _ request from state)

handle-cast

(handle-cast _ request state)

handle-info

(handle-info _ request state)

init

(init _ args)

terminate

(terminate _ reason state)

reply

(reply [mref pid :as _from] response)

This function can be used to explicitly send a reply to a client that called call* or, when the reply cannot be defined in the return value of handle-call. This allows processing call* requests asynchronously.

Client must be the from argument provided to the handle-call callback. reply is given back to the client as the return value of call*.

The return value is not further defined, and is always to be ignored.

start

(start server)(start server args)(start server args options)(start reg-name server args options)

The same as start! but returns async value.

start!

macro

(start! server)(start! server args)(start! server args options)(start! reg-name server args options)

Starts the server, passing args to server’s init function.

Arguments: server-impl - IGenServer implementation, or map, or namespace. args - any form that is passed as the argument to init function.

Options: :timeout - time in milliseconds gen-server is allowed to spend initializing or it is terminated and the start function returns [:error :timeout]. :spawn-opt - options used to spawn the gen-server process (see process/spawn-opt)

Returns: [:ok pid] if server started successfully, [:error :no-init] if server implementation doesn’t provide init function, [:error [:bad-return-value value]] if init returns a bad value, [:error reason] otherwise.

Throws on invalid arguments, or when the name is already registered.

start-link!

macro

(start-link! server)(start-link! server args)(start-link! server args options)(start-link! reg-name server args options)

The same as start! but atomically links caller to started process.

start-ns

macro

(start-ns)(start-ns args)(start-ns args options)(start-ns reg-name args options)

The same as start-ns! but returns async value.

start-ns!

macro

(start-ns!)(start-ns! args)(start-ns! args options)(start-ns! reg-name args options)

Starts the server, taking current ns as an implementation source. See start! for more info.