skeg_client/connection
A RESP3 connection to a skeg server.
let assert Ok(conn) =
connection.new("127.0.0.1", 6379)
|> connection.credentials(username: "alice", password: "secret")
|> connection.connect
let assert Ok(_) = connection.command(conn, [<<"SET">>, <<"k">>, <<"v">>])
connection.close(conn)
connect returns only after the server has accepted the HELLO 3
handshake, credentials included, so every command runs authenticated.
The socket is owned by a process started by connect and linked to the
caller. Commands from any number of processes are sent one at a time, in
arrival order, and each caller receives its own reply.
Every command has one deadline for the whole exchange: waiting behind other commands, sending, and reading the full reply. When the outcome of a command is unknown (timeout, broken stream, peer closed) the connection is closed, so a late reply can never be read as the answer to a later command. Commands are never retried automatically.
Types
How to reach and authenticate with a server. Build it with new.
pub opaque type Config
pub opaque type Connection
Values
pub fn close(connection: Connection) -> Nil
Closes the connection. Commands still waiting are not sent. Closing a closed connection does nothing.
pub fn command(
connection: Connection,
arguments: List(BitArray),
) -> Result(resp_codec.Value, error.Error)
Sends a command, given as its arguments, and returns the reply. A
top-level error reply becomes Error(ServerError(..)); errors nested in
an aggregate, such as per-element batch results, stay values.
pub fn command_within(
connection: Connection,
arguments: List(BitArray),
milliseconds: Int,
) -> Result(resp_codec.Value, error.Error)
Like command, with a deadline of milliseconds from now instead of the
connection’s default.
pub fn connect(config: Config) -> Result(Connection, error.Error)
Opens the connection and performs the handshake. On any error the socket is closed before returning.
pub fn connect_timeout(
config: Config,
milliseconds: Int,
) -> Config
Time allowed to open the TCP connection.
pub fn credentials(
config: Config,
username username: String,
password password: String,
) -> Config
Authenticates as username during the handshake.
pub fn max_reply_bytes(config: Config, bytes: Int) -> Config
Largest reply accepted. A larger one closes the connection with
OutcomeUnknown(ReplyTooLarge).
pub fn new(host: String, port: Int) -> Config
Default settings: no credentials, 5 s to connect, 5 s per command, and replies up to 256 MiB.