A relay that serves one job rarely wants to accept everything. A nostrino rendezvous relay keeps one stored, gateway-signed presence record and forwards two ephemeral frame kinds; a market relay stores signed notes from anybody; a private relay serves a handful of keys. Those are the same four questions with different answers:
Set TORLY_POLICY_FILE to a JSON document and the relay answers them. Without the variable the
relay accepts every kind from every author, exactly as before.
{
"version": 1,
"default": "deny",
"rules": [
{
"kinds": [30333],
"require_auth": true,
"authors": ["<64 hex>"]
},
{
"kinds": [23196, 23197],
"rate": { "per_second": 400, "burst": 800 }
}
]
}
version must be 1.default is allow or deny for kinds no rule matches. It defaults to allow, so a policythat only restricts a few kinds does not silently become an allowlist.
rules are checked in order and the first match decides. Put a specific rule before a catch-all; a rule with no kinds matches every kind.
kinds is a list of NIP-01 kinds. An empty or absent list matches every kind.require_auth demands NIP-42 authentication and that the authenticated key is the event'sauthor. A signature proves who wrote an event; authentication proves who is speaking, and a restricted kind wants both.
authors restricts the matched kinds to those public keys (hex, case-insensitive). It works on the event's author whether or not authentication is required; combine it with require_auth
when the connection's identity matters too.
tags narrows a rule to events carrying named tags: every name must be present with one of thelisted values. It is what separates a protocol's notes from every other note of the same kind, so a gateway-only rule can cover a marketplace's listings while a different rule covers the rest of kind 1.
rate is a token bucket per connection and per rule: burst events at once, then per_second sustained. burst defaults to per_second, and may be smaller (less than a
second of buffering). bytes_per_second/bytes_burst add the same budget measured in bytes,
which a count alone cannot express: without it, a connection may push per_second events of
the maximum size every second. One client cannot spend another client's budget.
Unknown fields are refused, so a typo is a startup error rather than a rule that quietly does something else. A file that cannot be read or parsed stops the relay: running without the policy would accept what the operator believes is refused.
A refused write is answered with OK false and a NIP-01 machine-readable prefix, so a client can
tell the cases apart without reading prose:
| Prefix | Meaning |
|---|---|
blocked: | the kind is not accepted, or the author is not registered for it |
auth-required: | the rule requires NIP-42, the connection is not authenticated, or the authenticated key is not the author |
rate-limited: | this connection spent its budget for the rule |
invalid: | the event itself is malformed, expired, too large or too far in the future |
The policy is evaluated before the signature is verified, so a refused write costs a table lookup rather than a verification slot. AUTH events (kind 22242) are never subject to it: a policy that required authentication for them could not be satisfied.
deploy/policy/rendezvous.json is the nostrino case:
30333 (the gateway presence record) — require_auth plus the gateway's key, stored andaddressable, so a client that connects later still finds it;
23196/23197 (client→gateway and gateway→client frames) — open to any wallet, becauseframes are encrypted for the gateway and the client is not known in advance, with a per connection rate;
"default": "deny", so nothing else is stored or forwarded.deploy/torly-rendezvous.env is the matching environment: Blossom off, a 128 KiB event ceiling
(the tunnel's 32 KiB frames are NIP-44 ciphertext, about 4/3 of the frame), and the query limits a
rendezvous relay needs rather than a general one.
deploy/policy/market.json is the other shape: the standing offers (kind 1 tagged
blake-swap-market) are the gateway's alone, while takes, cancels and matches are what
participants publish, each at its own rate, and deletions need authentication. Everybody can read
the offers, and TORLY_QUERY_PER_SEC stops a client from asking for them in a loop. The market's own
expiry rides NIP-40, which the relay already honours (TORLY_EXPIRY_INTERVAL), so an offer
disappears when its expiration tag says.
The policy governs inbound writes only. Delivery to subscribers — what a wallet actually downloads — is not rate-limited at all: a relay sends each matching event to every matching subscription as fast as the sockets take it, and a client that cannot keep up loses its connection rather than slowing the relay down. That asymmetry is deliberate, and the rendezvous policy uses it: the gateway's frames (kind 23197, gateway to wallet, the bulk of a sync) are gateway-only and carry no rate, while a wallet's frames (kind 23196, requests and acks) are open but rate- and byte-limited per connection.
Two more limits are direction- and request-aware:
TORLY_OUTBOUND_BYTES_PER_SEC paces live delivery to each connection, in bytes per second.Zero, the default, delivers at whatever the socket takes. A value queues the excess in order and releases it as the budget allows, so a paced stream is slowed, never broken: the nostrino tunnel would otherwise see a gap in its sequence numbers and ask for the frame again. A connection that queues more than 8 MiB is closed as a slow consumer.
TORLY_QUERY_PER_SEC and TORLY_QUERY_BURST bound subscription requests per connection. A REQ or COUNT over the budget is not answered at all, which is what stops a client from
polling a marketplace flat; one that keeps asking is closed after TORLY_ABUSE_CLOSE_AFTER
requests.
Two limits sit above the policy:
TORLY_INBOUND_BYTES_PER_SEC is a relay-wide ceiling on inbound bytes, checked on the raw message before the JSON is parsed. It is the line: set it to what the relay can actually serve.
A message over the ceiling is dropped with no reply (answering a flood would cost a write per
dropped frame), and a connection that keeps exceeding it is closed after
TORLY_ABUSE_CLOSE_AFTER messages. This is what stops a flood spread across connections that
each stay inside their own budget.
TORLY_MAX_EVENT_BYTES refuses one oversized message; TORLY_MAX_CONNECTIONS bounds how manyconnections there are to share the ceiling.
TORLY_AUTH_REQUIRED is the global gate: when true, every write needs NIP-42 regardless of thepolicy. The policy is how a relay asks for authentication on some kinds and not others.
TORLY_MAX_EVENT_BYTES, TORLY_MAX_FILTERS, TORLY_MAX_SUBS_PER_CONN, TORLY_MAX_LIMIT and TORLY_QUERY_LIMIT bound a single event or subscription. The policy decides what may be
published at all; the limits decide how large and how many.
TORLY_EXPIRY_INTERVAL prunes events whose NIP-40 expiration has passed. A policy that allowsa kind does not keep it forever if its events carry expiration.
The announcements on a relay already name every relay that back end talks to, so a client that discovers a gateway on one relay learns the others and can fail over between them.
Before any of that, the relay has to say where to ask. Set
TORLY_CONTACT_EMAIL=keys@your.relay
and it is published as the NIP-11 contact (`curl -H 'Accept: application/nostr+json'
https://your.relay/`), which is where a back-end operator looks. A relay that keeps an allowlist
without publishing one gives the other side no way to join. TORLY_CONTACT remains the general
contact and is used only when no email is set.
A second relay operator who wants the same back ends adds their public keys to the gateway-only
rules. nostrino-discover prints exactly that list from any relay that already holds the
announcements:
nostrino-discover -relay wss://swaprelay.smesh.lol -all -authors
"authors": ["<pubkey>", "<pubkey>"]
The back end then adds the new relay to its own -relay flags; its listing, which already names
every relay it uses, is published there as well, so the two relays describe the same back ends and
clients can discover them from either one. The loop closes with the addresses: the relay publishes
its TORLY_CONTACT_EMAIL, the back end writes to it with its public key, and both sides know who to
talk to before and after they are connected.