Skip to content

Instantly share code, notes, and snippets.

@djdisodo
Last active January 2, 2026 14:25
Show Gist options
  • Select an option

  • Save djdisodo/0222270ad0de085fda8dedc0188e9468 to your computer and use it in GitHub Desktop.

Select an option

Save djdisodo/0222270ad0de085fda8dedc0188e9468 to your computer and use it in GitHub Desktop.
running torrent on esp32

Running Torrent Client on esp32

here's cool avif image image the red led is connected to spi, so it turns on when data is being written

notes

i tried this project since i found it challenging and cool,
if you want something that's useful, it's better to use any old smartphones with chroot

this project uses esp-idf, so it's more like a programming for constrained resource than bare-metal programming
and the code is very generic and non platform-dependant, based on tokio

this project uses rust

before trying this i ported https://github.com/ikatson/rqbit but crashed out of OOM

backstory

i bought esp32 thinking to do a lot more things, and thought, why not riscv32 and wifi6?
and gone with esp32c6 later to notice it was likely a mistake

unlike s2 or s3, c6 doesn't support PSRAM and only has 500KB of SRAM
and i found this to be a huge issue later

also while it has two usb port, it can't use usb host mode
so i can't use usb storage, had to go with sdcard
but again, c6 doesn't supports sdmmc, i'm stuck with 80MHz SPI sdspi
(as you can see above i even bought sdcard module that supports sdmmc mode, without knowing it won't work)

it would be cool if something that looks like a phone charger head and it can function as NAS when you connect storage to that usb port APLA1385-pimid2745-lg

Challenges

Memory usage

first i started by reducing tcp window size to 6K and RECVMBOX to 10K
(numbers are pulled out of my ass)

these are allocated per-socket so these size limits amount of peers I can connect to at a time

maybe these can be less of a problem if I implement UDP connection

there's 4K buffer for copying data from socket to file
there's 4K buffer for copying data from file to socket

and also per peer there's 2K buffer to read data back from file when we need to calculate sha1 hash

these may not sound like a lot, but memory fragmentation is also an issue

it would be ideal if i can customize the allocator and return preallocated space only when the request size matches them
but i couldn't find any way to do so

subobtimal would be to implement buffer pool, but this doesn't help when i want to keep my code looking generic and also increases complexity

anyway we're here with maximum of 4 peers

also i always program with memory consumption in mind, but never know how much does it even reduce the usage

fragmentation would be less of a problem if I had more memory

Processing power

this in fact, isn't a challenge, but here it is to tell why it isn't a challenge

it's common sense that downloading stuffs barely uses processing power
(some cases like Steam, uses on decompressing files)

however in torrent, some processing power is used to verify pieces of data, by hashing the piece and comparing with hash table

esp32c6 is quite powerful with riscv32 160MHz processor
but is it enough to run torrent?

here's benchmark of xtensa 240MHz esp32, not same processor but anyway source image

2.8MB/s it's not bad, even if we half them

but as you can see in the table, esp32 is equiped with hardware sha1 accelerator,

i wouldn't complain if it can do 30MB/s

More memory usage problem

hashing should be done in order, but there's no guarantee requested data will arrive in order

since piece is not stored in memory to reduce memory usage,
if data doesn't arrive in order it will have to read data back from file and do hashing

and storage here is very very slow, so it's ideal to avoid this

i implemented an algorithm that sends request with interval, and increase interval when data doesn't arrive in requested order

every task, like saving status, announcing and connecting peer uses memory

so i want to avoid multiple task running at the same time, so i made them one at a time using select! macro

Bottlenecks

Peer count

one peer are unlikely to upload at high speed, so it is must to connect to multiple peers to saturate bandwidth

but this can only connect to limited amount of peers

Storage

sdspi is absolute garbage and i only saw 200KB/s maximum speed

Todo

http file managment

this will allow me to delete file remotely and add torrent remotely, stream content from esp

implement udp

hopefully this will allow it to connect with more peers

implement DHT

upgrade the gear

even esp32s3 can do a lot better with 8MB of ram and sdmmc support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment