Show HN: I built a concurrent BitTorrent engine in Go to master P2P protocols
3 points
1 hour ago
| 1 comment
| HN
I’ve always used BitTorrent, but I never understood the complexity of peer-to-peer orchestration until I tried to build it from scratch. I wanted to move beyond simple "Hello World" projects and tackle something that involved real-world constraints: network latency, data poisoning, and the "Slow Peer Problem."

Key Technical Challenges I Solved:

Non-Blocking Concurrency: Used a worker pool where each peer gets its own Goroutine. I implemented a "Stateless Worker" logic where if a peer fails a SHA-1 hash check or drops the connection, the piece is automatically re-queued into a thread-safe channel for other peers to pick up.

Request Pipelining: To fight network RTT, I implemented a pipeline depth of 5. The client dispatches multiple 16KB block requests without waiting for the previous one to return, ensuring the bandwidth is fully saturated.

The Binary Boundary: Dealing with Big-Endian logic and the 68-byte binary handshake taught me more about encoding/binary and byte-alignment than any textbook could.

Zero-Trust Data Integrity: Every 256KB piece is verified against a "Golden Hash" using crypto/sha1 before being written to disk. If a single bit is off, the data is purged.

The Specification: I’ve documented the full spec in the README, covering:

Reflection-based Bencode Parsing.

Compact Tracker Discovery (BEP-0023).

The Choke/Unchoke Protocol State Machine.

Data Granularity (Pieces vs. Blocks).

Repo: https://github.com/Jyotishmoy12/Bittorrent-Client-in-Go

I’d love to get feedback from the community on my concurrency model and how I handled the peer lifecycle.

sinisterMage
1 hour ago
[-]
this seems very impressive! however i would like to ask, why the language choice of go? and not something like elixir for fault tolerance and its actor model, i am asking not because i dont like go, i am simply curious
reply
Jyotishmoy
1 hour ago
[-]
Thank you so much :). Oh, I will be very honest. I used Go only because I know Go but don't know Elixir, and another reason is because of how Go handles concurrency. I find it super easy to implement.
reply
sinisterMage
1 hour ago
[-]
oh alright! very understandable :) good luck with this project!
reply
Jyotishmoy
1 hour ago
[-]
Thank you so much.
reply