Go receiver
One file, standard library only. It binds your UDP port, prints the verification token, counts shreds and reads each shred's common header with encoding/binary.
Build and run#
Save the program as receiver.go, then build it and run it with the UDP port you sent us. It needs Go 1.18 or later, for net/netip, and no modules.
go build -o receiver receiver.go./receiver 9000
A second argument, a source address, is optional. Add it only once delivery is active and support has given you the address the stream is sent from; datagrams from any other sender are then ignored. While your destination is being verified, leave it off, since support has not given you the address yet. A datagram that is all printable text, such as the verification token, is printed as a text from line and not parsed.
1// receiver.go: go run receiver.go 9000 [source-ip]2package main34import (5"encoding/binary"6"errors"7"fmt"8"net"9"net/netip"10"os"11"strconv"12"strings"13"time"14)1516const (17maxDatagram = 122818commonHeader = 8319wantRcvbuf = 8 << 2020)2122type header struct {23slot uint6424kind string25index uint3226fec uint3227version uint1628length int29}3031func isText(b []byte) bool {32if len(b) == 0 {33return false34}35for _, c := range b {36if c != '\t' && c != '\n' && c != '\r' && (c < 0x20 || c > 0x7e) {37return false38}39}40return true41}4243func kind(variant byte) string {44switch variant {45case 0xa5:46return "data" // legacy data47case 0x5a:48return "code" // legacy coding49}50switch variant & 0xf0 {51case 0x80, 0x90, 0xb0:52return "data" // Merkle data53case 0x40, 0x60, 0x70:54return "code" // Merkle coding55}56return ""57}5859func fail(err error) {60fmt.Fprintln(os.Stderr, err)61os.Exit(1)62}6364func main() {65port := 900066if len(os.Args) > 1 {67p, err := strconv.Atoi(os.Args[1])68if err != nil {69fail(err)70}71port = p72}73var source netip.Addr // zero value: accept every sender74if len(os.Args) > 2 {75a, err := netip.ParseAddr(os.Args[2])76if err != nil {77fail(err)78}79source = a80}8182conn, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4zero, Port: port})83if err != nil {84fail(err)85}86defer conn.Close()87if err := conn.SetReadBuffer(wantRcvbuf); err != nil {88fail(err)89}90fmt.Printf("listening on udp://0.0.0.0:%d\n", port)9192buf := make([]byte, maxDatagram)93var data, code, short, unknown int94var last *header95next := time.Now().Add(time.Second)96conn.SetReadDeadline(next)9798for {99n, from, err := conn.ReadFromUDPAddrPort(buf)100if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {101fail(err)102}103ip := from.Addr().Unmap()104switch {105case err != nil, source.IsValid() && ip != source:106// deadline reached, or a sender we were told to ignore107case isText(buf[:n]):108fmt.Printf("text from %s: %s\n", ip, strings.TrimSpace(string(buf[:n])))109case n < commonHeader:110short++111default:112k := kind(buf[64])113if k == "" {114unknown++115break116}117if k == "data" {118data++119} else {120code++121}122last = &header{123slot: binary.LittleEndian.Uint64(buf[65:73]),124kind: k,125index: binary.LittleEndian.Uint32(buf[73:77]),126version: binary.LittleEndian.Uint16(buf[77:79]),127fec: binary.LittleEndian.Uint32(buf[79:83]),128length: n,129}130}131132if now := time.Now(); !now.Before(next) {133line := fmt.Sprintf("data=%d code=%d short=%d unknown=%d", data, code, short, unknown)134if last != nil {135line += fmt.Sprintf(" last: slot=%d type=%s index=%d fec=%d version=%d len=%d",136last.slot, last.kind, last.index, last.fec, last.version, last.length)137}138fmt.Println(line)139data, code, short, unknown = 0, 0, 0, 0140next = now.Add(time.Second)141conn.SetReadDeadline(next)142}143}144}
What does it print?#
listening on udp://0.0.0.0:9000text from 203.0.113.10: verify token=abc123data=3 code=1 short=1 unknown=1 last: slot=312345680 type=data index=18 fec=0 version=50093 len=1200
The token line comes first: send the token back to support. After that, one line a second counts data shreds, coding shreds, datagrams too short for a header (short) and datagrams with a variant byte it does not recognise (unknown), then shows the most recent shred's slot, type, index, FEC set index, shred version and length. The output matches the Python receiver line for line, so you can compare the two on one server.
How does it read the header?#
The first 64 bytes of a shred are the leader's signature. Byte 64 is the variant, and kind maps it to data or coding the same way the Rust receiver does. The next 18 bytes are read with binary.LittleEndian: slot from 65 to 72, index from 73 to 76, shred version from 77 to 78 and FEC set index from 79 to 82. The headers after these 83 bytes differ between data and coding shreds; parsing the shred header goes through them.
ReadFromUDPAddrPort returns the sender as a netip.AddrPort. Its address is a comparable value, so the source check is a plain !=. On a socket bound to an unspecified address, the Go docs say the address can come back IPv4-mapped, which is why the program calls Unmap before comparing.
Why a read deadline?#
The receive call blocks until a datagram arrives, so on an idle port the program would never print its summary. A deadline set to the next report time makes the read return os.ErrDeadlineExceeded instead, the summary prints, and the deadline moves on a second. The deadline is set once a second, not once per packet.
Is SetReadBuffer enough?#
SetReadBuffer sets SO_RCVBUF on the socket. Linux quietly caps the value at net.core.rmem_max and returns no error, so the call succeeding does not mean you got 8 MiB. Check what the kernel granted with ss; the rb field is the receive buffer in bytes:
ss -ulmn 'sport = :9000'
UNCONN 0 0 0.0.0.0:9000 0.0.0.0:*skmem:(r0,rb425984,t0,tb212992,f0,w0,o0,bl0,d0)
An rb of 425984 is a cap of 212992 bytes, doubled by the kernel. Raise net.core.rmem_max, restart the receiver and check again; Linux sockets and buffers has the commands. The d field in the same line counts datagrams the socket dropped.
Towards production#
- Keep the receive goroutine to receiving. Copy each datagram into a buffer from a pool and pass it on a buffered channel to workers, because the next read overwrites
buf. - Decide what happens when workers fall behind. If the channel is full, either drop and count in your own code, or let the kernel buffer fill and drop there. Either way, make the drops visible.
- The standard library reads one datagram per system call. For batches,
golang.org/x/net/ipv4hasPacketConn.ReadBatch, which its docs say is optimized on Linux. That module is outside the standard library, so this page does not use it. - Watch for missing shreds by slot and index, as in detecting gaps and duplicates.
For the rest of the setup, start from the docs overview.
Sources#
- Go standard library: package net (ListenUDP, UDPConn.SetReadBuffer, ReadFromUDPAddrPort) (checked 2026-11-17)
- Go standard library: package encoding/binary (checked 2026-11-17)
- Go standard library: package net/netip (checked 2026-11-17)
- golang.org/x/net/ipv4: PacketConn.ReadBatch (checked 2026-11-17)
- socket(7): SO_RCVBUF and net.core.rmem_max (checked 2026-11-17)
- ss(8): skmem fields (checked 2026-11-17)