Throttling bandwidth with Go
Imagine a service that is serving big log files over raw TCP, you decided to implement simple QoS for an existing server.
The goal of the task is to create small Go package that would allow for throttling bandwidth for TCP connections.
Requirements:
- keep it simple
- think of it as open-source software that builds upon existing solutions
- use only stdlib and/or supplementary repositories (golang.org/x/*)
- prefer reusing existing packages
- e.g. https://pkg.go.dev/golang.org/x/time/rate?tab=doc
- the package should allow for:
- setting one cumulative bandwidth limit for all connections* (aka global limit)
- setting single individual bandwidth limit for all connections** (aka connection limit)
- changing limits in runtime (applies to all existing connections)
- for a 30s transfer sample consumed bandwidth should be accurate +/- 5%
Reasonable deadline for submitting a solution is 8 days. Questions, clarifications – please drop us an e-mail!
Test
go test -v -timeout 300s
Example server usage
l, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
}
l.WithBandwith(10)
for {
conn, err := l.Accept()
if err != nil {
panic(err)
}
wrappedConn := limiter.WrapConn(conn)
wrappedConn.WithBandwith(5)
go handleConn(t, wrappedConn)
}