Generic server behavior implementation.
Provides a standard way to implement servers with synchronous calls, asynchronous casts, and message handling.
Generic server behavior implementation. Provides a standard way to implement servers with synchronous calls, asynchronous casts, and message handling.
(call server request)(call server request timeout-ms)Make synchronous call to server. Returns response or throws on timeout/error.
Make synchronous call to server. Returns response or throws on timeout/error.
(cast server request)Send asynchronous request to server. Returns :ok.
Send asynchronous request to server. Returns :ok.
Protocol for gen_server callbacks.
Protocol for gen_server callbacks.
(handle-call this request from state)Handle synchronous call. Returns: [:reply response new-state] | [:reply response new-state timeout] | [:noreply new-state] | [:noreply new-state timeout] | [:stop reason response new-state] | [:stop reason new-state]
Handle synchronous call.
Returns: [:reply response new-state]
| [:reply response new-state timeout]
| [:noreply new-state]
| [:noreply new-state timeout]
| [:stop reason response new-state]
| [:stop reason new-state](handle-cast this request state)Handle asynchronous cast. Returns: [:noreply new-state] | [:noreply new-state timeout] | [:stop reason new-state]
Handle asynchronous cast.
Returns: [:noreply new-state]
| [:noreply new-state timeout]
| [:stop reason new-state](handle-info this message state)Handle other messages. Returns: same as handle-cast
Handle other messages. Returns: same as handle-cast
(init this args)Initialize server state. Returns: [:ok state] | [:ok state timeout] | [:stop reason]
Initialize server state. Returns: [:ok state] | [:ok state timeout] | [:stop reason]
(terminate this reason state)Called when server is terminating. Return value ignored.
Called when server is terminating. Return value ignored.
(ns->gen-server ns-sym)Create an IGenServer implementation from a namespace.
The namespace should define functions matching the IGenServer protocol:
Missing functions use default implementations.
Usage: (ns my-server) (defn init [args] [:ok {:count 0}]) (defn handle-call [req from state] [:reply (:count state) state])
;; Then start with: (gen-server/start (gen-server/ns->gen-server 'my-server))
Create an IGenServer implementation from a namespace.
The namespace should define functions matching the IGenServer protocol:
- init [args] -> [:ok state] | [:ok state timeout] | [:stop reason]
- handle-call [request from state] -> [:reply response new-state] | ...
- handle-cast [request state] -> [:noreply new-state] | ...
- handle-info [message state] -> [:noreply new-state] | ...
- terminate [reason state] -> ignored
Missing functions use default implementations.
Usage:
(ns my-server)
(defn init [args] [:ok {:count 0}])
(defn handle-call [req from state] [:reply (:count state) state])
;; Then start with:
(gen-server/start (gen-server/ns->gen-server 'my-server))(reply [ref caller-pid] response)Send reply to a call. Used when handle-call returns :noreply. from is [ref caller-pid].
Send reply to a call. Used when handle-call returns :noreply. from is [ref caller-pid].
(start impl)(start impl args)(start impl args opts)Start a gen_server process. Returns {:ok pid} or {:error reason}.
Start a gen_server process.
Returns {:ok pid} or {:error reason}.(start-link impl)(start-link impl args)(start-link impl args opts)Start a gen_server process linked to current process. Returns {:ok pid} or {:error reason}.
Start a gen_server process linked to current process.
Returns {:ok pid} or {:error reason}.(stop server)(stop server reason)(stop server reason timeout)Stop the server.
Stop the server.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |