Going further
Swarms
This page shows how to run many bots in one process: Swarm schedules them on a few worker
threads, a join delay spaces out their logins, and an optional SharedChunks store lets bots in
the same area hold one copy of the chunks they all see. (For a single bot, keep the
current_thread runtime from First bot.)
A swarm is a small scheduler and nothing more: each bot is still an independent Client
session, so bots in one swarm can join different servers with different versions.
Spawning bots
Swarm::builder() starts the worker threads, each with its own current-thread runtime and
LocalSet. spawn sends your closure to the least-loaded worker (ties go round-robin), and the
bot stays there for its whole life. The closure runs on the worker thread, so its future does
not need to be Send.
use Duration;
use ;
let swarm = builder
.workers
.join_delay
.build?;
let mut bots = Vecnew;
for i in 0..100
The closure receives a BotContext: id() is the bot's number in spawn order, worker() its
thread, and stopped() a future that completes when the bot is asked to stop. Pass it to
Client::run_until and the bot logs out cleanly. You can call stopped() again on every
reconnect.
Workers. The default is one worker, and it is the cheapest layout until that thread is close
to a full core: a busy thread handles many bots per wake-up, while bots spread thinly over many
threads make every thread wake for each small batch of packets. Add workers with .workers(n)
only when one thread is no longer enough; watch the CPU of the ghast-swarm-<n> threads.
swarm.worker_loads() gives the live bots per worker.
Join delay. Servers often throttle logins per address. join_delay is the minimum time
between two bot starts across the swarm. spawn still returns at once; each bot gets a start
slot in spawn order and waits on its worker until then. A busy worker may start it a little
later, never earlier.
Stopping and joining
A BotHandle stops one bot and waits for its result:
stop()asks that bot to stop and returns at once.join().awaitwaits from any runtime;join_blocking()waits from synchronous code (it panics inside an async runtime).- Dropping a handle does not stop the bot. Handles keep working after the swarm is gone.
swarm.stop_all() fires every bot's stopped() without waiting; bots spawned afterwards do not
start. swarm.shutdown(grace) does that, waits up to grace, aborts the rest and joins the
threads. It blocks, so from async code call stop_all(), await the handles, then drop the
swarm. Dropping a swarm does the same with a 5 s grace (drop_grace).
let report = swarm.shutdown;
println!;
for bot in bots
The outer Result is the swarm's: BotError::Panicked carries the panic message, NotStarted
means the bot was stopped before its start slot, Aborted that it was still running when the
swarm gave up. Each bot runs as its own task, so a bot that panics or fails ends only itself.
ShutdownReport::stuck_workers counts workers left blocked by a bot that never awaits.
Sharing chunks
Bots in the same area receive the same chunks, and at view distance 10 their block storage is
most of a bot's memory. Give every client one world::SharedChunks store and each identical
16x16x16 section is held once for all of them:
use SharedChunks;
let cache = new; // one store for the whole process
let client = new.shared_chunks;
- Copy on write, per section. A block change, from the server or from local prediction, never writes to storage another bot reads. The changing bot moves to the section with the new content, which bots receiving the same update then share again. No bot sees another bot's packets.
- Decoded once. A section's key is a 128-bit keyed hash of its exact chunk-packet bytes; a section already in the store is not decoded.
- Reads cost the same, with no lock. A block change costs more (a store lookup instead of a write in place), which matters only where blocks change constantly.
- Memory is freed with the last bot holding a section. Biomes and block entities stay per bot.
It is opt-in and used by 26.1, 26.2 and 26.3 only; the 1.8.9, 1.12.2 and 1.16.5 worlds ignore
it. The handle is Send + Sync, so bots on different workers can share it. cache.sections()
and cache.heap_bytes() report what it holds.
Memory per bot
The README's measured figures are for one idle AFK bot per process, x86-64, release profile, before the 0.4.4 memory work:
| build | RSS | measured |
|---|---|---|
| every version, on 26.1 / on 26.2 | 8.6 MB / 9.0 MB | 2026-11-13 (0.4.2) |
| 26.1 only / 26.2 only | 6.7 MB / 6.5 MB | 2026-11-13 (0.4.2) |
| 1.8.9 only | 5.0 MB | 2026-11-12 |
Most of that is not private: private dirty memory was about 1.6 to 1.9 MB for the
single-version builds. No per-bot figure is published for bots inside one Swarm. Since 0.4.4
the zlib inflater (about 47 KB) is per thread instead of per connection, and 26.x registry
data, tags and recipe bytes are stored once per process for clients that received the same
content.
When one thread per bot is still fine
You do not need a swarm. The session future is Send for Send behaviours, so many bots can
share one multi-thread tokio runtime. Or run each bot in its own process on a current_thread
runtime: the strongest isolation, and it needs nothing from the library. For that layout the
README recommends:
- Build only the versions you use (
default-features = falseplus the version features). - Build without PIE (
RUSTFLAGS="-C relocation-model=static"with an explicit--target), so the generated data tables (about 0.9 MB) stay shared between processes. Idle AFK bot, x86-64, 2026-11-12: private dirty memory 2.3 to 1.4 MB.
The swarm example runs N wandering bots in one process, and takes every option of the other
examples, including those from Accounts and proxies: