3

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.

Aleksey Kanaev
  • 293
  • 7
  • 13
  • 1
    Even if you read slowly from the local TCP buffer, the kernel can still fetch up to `/proc/sys/net/ipv4/tcp_rmem` (third number is max buffer size in bytes) at a time. For a long-lived connection you can average it over time (buffer fills in a burst, consume slowly, buffer re-fills, etc). To really limit bandwidth you need to do it at the OS layer, not in Go. See https://unix.stackexchange.com/a/28203/40168 . – Graham King Nov 14 '17 at 18:31

1 Answers1

0

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.

Joseph Paul
  • 346
  • 1
  • 7