Limit tcp download speed in Golang

Viewed 1173

I would like to be able to limit bandwidth of tcp connection in my code. This question has some clues, but not exactly what I want. I have tried to hack src code of go standard lib (net/tcp package), using similar approach, but failed. My main idea is to somehow write data from syscall to internal buffer slowly, thus making OS to drop tcp packets.

1 Answers

As said in the comment, the easiest way is to do it at the OS level with wondershaper tool. If you look at 'wondershaper' script, it uses '/sbin/tc' command from the 'iproute2' package. I understand that it is not exactly what you would like to do, but you can go at a lower level by executing 'tc' commands in your source code with os.exec(). Implementing your own "traffic control" algorithm is maybe too much work and not necessary for your goal.

Related