Wire client
BrainClient over the BRN0 protocol — connect, ClientConfig, the two auth methods, act_as, multiplexing, the pool, and the id helpers.
Connecting
BrainClient.connect(host: str, port: int, auth: Auth) -> BrainClient
BrainClient.connect_with(host: str, port: int, config: ClientConfig) -> BrainClientconnect takes just a credential and uses the default transport settings;
connect_with takes a full ClientConfig. The handshake — HELLO, version
negotiation, AUTH — runs inside both.
ClientConfig
authAuthrequiredThe credential. Mandatory, and the only field without a default: the server resolves the connection's identity from it, so a config cannot be built without one.
client_idstroptionalIdentifies the client to the server. Defaults to
"brain-db-sdk-python".
supported_versionslist[int]optionalWire versions this client accepts. Defaults to [1] — the only version
Brain speaks today.
capabilitiesHelloCapabilitiesoptionalDefaults to streaming on, zstd compression off, server push off.
connect_timeoutfloat | NoneoptionalDeadline for the connect and handshake, in seconds. Defaults to 10.0.
request_timeoutfloat | NoneoptionalDeadline for an ordinary request. Defaults to 30.0.
derived_write_timeoutfloat | NoneoptionalDeadline for an encode under the DERIVED wait mode only. Defaults to
120.0 — it exists because the 30 s request timeout would fire before the
derivation pipeline finished.
Auth
Auth.token(token: bytes)AuthrequiredA shared bearer token, as bytes — encode it yourself.
Auth.mtls(claim: MtlsClaim)AuthoptionalMutual TLS: the client certificate's fingerprint plus the subject it claims. The server checks the assertion against the presented cert.
import os
from brain_db_sdk import BrainClient, Auth, ClientConfig
client = BrainClient.connect_with(
"127.0.0.1",
9090,
ClientConfig(
auth=Auth.token(os.environ["BRAIN_API_KEY"].encode()),
request_timeout=15.0,
),
)
try:
...
finally:
client.close() # sends BYE, then closesConnection facts
client.connection is a frozen ConnectionInfo carrying what the handshake
established, with no round trip: space_id, server_id, chosen_version,
connection_id, bound_shard_id, permissions, namespace and
server_features. client.namespace, client.space_id and
client.is_closed are shorthands.
bound_shard_id matters: the connection is pinned to one shard, and a request
for data on another is WrongShard, not a transparent redirect.
Multiplexing
One connection carries many concurrent requests, demultiplexed by stream_id
on a background reader thread — so a single client serves requests from
several threads at once. You do not manage streams.
Streaming verbs — anything with a *_frames form, plus
subscribe — each occupy one stream for their
duration. cancel_stream ends one early.
act_as
Every tenant-scoped wire request carries an optional act_as selector —
namespace, space, and an optional grant. It is what lets one
connection serve many spaces: the credential fixes the namespace, and
act_as selects the space per request. The builders expose
.act_as(namespace, space); hand-built requests set the field directly.
act_as is a grant, not a claim
A principal without the act-as grant — or one naming a namespace outside its
allowlist — raises ActAsDenied, a ServerError subclass you can catch by
type. The two optional grant bits, schema upload and admin, are requested
per call and must also be held.
This is the mechanism behind per-tenant isolation at scale: one pooled connection, a storage-key prefix per end user, and no credential per user. See Namespaces.
Pool
Pool.connect(host, port, size, auth)PoolrequiredOpens size connections, each running its own handshake.
Pool.connect_with(host, port, size, config)PooloptionalThe same, with a full config.
pool.get()BrainClientrequiredThe next connection, round-robin. No liveness check.
pool.get_healthy()BrainClientrequiredThe next connection, reconnected first if its socket died. Concurrent callers landing on the same dead slot share one reconnect.
pool.size()intoptionalpool.close()NoneoptionalPool is a context manager, so with Pool.connect(...) as pool: closes it for
you.
A pooled member outlives any single request, so a server restart leaves every
borrower holding the same dead socket until the process is bounced. get()
will hand it to you.
Id helpers
new_id()bytesrequiredA fresh random 16-byte id, for request_id and txn_id on hand-built
requests. The builders call it for you.
derive_space_id(namespace, space)bytesoptionalThe space id the server derives at ingress:
UUIDv5(UUIDv5(ROOT, namespace), space). Folding the namespace in is what
makes equal space strings under different namespaces diverge. The seed is
frozen and pinned by a golden test on both sides.
Do not let the derived space id leave Brain
Clients only ever send the space string. derive_space_id exists for
the rare local case — naming the effective space by its resolved id in a
subscription filter. It is Brain's internal identifier, not your control
plane's; using it as an external key couples two id spaces that are
deliberately separate.
Was this page helpful?