Skeg (skeg v1.0.0)
Copy MarkdownA client for the skeg server over RESP3.
{:ok, conn} = Skeg.start_link(host: "127.0.0.1", port: 6379, auth: {"alice", "secret"})
{:ok, "OK"} = Skeg.command(conn, ["SET", "k", "v"])
Skeg.close(conn)Under a supervisor:
children = [{Skeg, name: MyApp.Skeg, port: 6379, auth: {"alice", fn -> fetch_password() end}}]Connection
start_link/1 returns only after the server has accepted the HELLO 3
handshake, credentials included, so every command runs authenticated. A
connection holds one identity; use one connection per tenant.
The connection is a process owning one socket. Commands from any number of processes are sent one at a time, in arrival order, and each caller gets its own reply. Requests wait in the process mailbox, which has no size limit; a request whose deadline passes there is discarded, never sent late.
Every request has one deadline for the whole exchange: waiting behind
other requests, sending, and reading the full reply. When the outcome of a
request is unknown (timeout, broken stream, peer closed) the socket is
closed, so a late reply can never be read as the answer to a later
request. See Skeg.Error for what each error proves about the request.
Requests are never retried. With reconnect: true, the request after a
lost connection opens a new one and authenticates again first. That
includes a connection the server closed while idle: the loss is found
before sending, so the same request goes on the new connection. Without
:reconnect, requests after a loss fail with :not_sent.
Options
:host- default"127.0.0.1":port- default6379:auth-{username, password}. The password may be a zero-arity function that returns it, which keeps it out of supervisor child specs. Printing the connection's state does not show the password, but it is not erased from memory: without:reconnectthe connection drops it after the handshake; with:reconnectit keeps it to authenticate again, recoverable by anyone who can inspect the process. To keep only a reference, pass a function that fetches the password from a secret store.:timeout- deadline in ms for the handshake, default5000:connect_timeout- ms to open the TCP connection, default5000:max_reply_bytes- largest reply, or sum of replies of a pipeline, default 256 MiB. A larger one closes the connection.:max_request_bytes- largest encoded request, default 64 MiB. A larger one is refused before sending.:max_pipeline- most commands in one pipeline, default1000:reconnect- defaultfalse:name- registers the process
Summary
Functions
Closes the connection. Closing a closed connection does nothing.
Sends one command, given as its binary arguments, and returns the reply.
Sends several commands at once and returns their replies in order. Error
replies are %Skeg.Error{} elements of the list. This is not a
transaction: when the outcome is unknown, any prefix of the commands may
have been applied. Options: :timeout in ms for the whole pipeline.
Starts a connection. See the module documentation for options.
Types
@type conn() :: GenServer.server()
Functions
@spec close(conn()) :: :ok
Closes the connection. Closing a closed connection does nothing.
@spec command(conn(), [binary()], keyword()) :: {:ok, Skeg.RESP.value()} | {:error, Skeg.Error.t()}
Sends one command, given as its binary arguments, and returns the reply.
A top-level error reply becomes {:error, %Skeg.Error{kind: :server}};
errors nested in an aggregate, such as per-element batch results, stay
values. Options: :timeout in ms, default 5000.
@spec pipeline(conn(), [[binary()]], keyword()) :: {:ok, [Skeg.RESP.value()]} | {:error, Skeg.Error.t()}
Sends several commands at once and returns their replies in order. Error
replies are %Skeg.Error{} elements of the list. This is not a
transaction: when the outcome is unknown, any prefix of the commands may
have been applied. Options: :timeout in ms for the whole pipeline.
@spec start_link(keyword()) :: {:ok, pid()} | {:error, Skeg.Error.t()}
Starts a connection. See the module documentation for options.