Search code examples
multithreadinggorate

How to implement simple traffic shaping in go


I am trying as an exercise to implement simple traffic shaper in go.

The API is: push(int): puts an int in the shaper out(): outputs 1 or more int(s) from the shaper.

push is called by the clients and the rate can't be controlled. out is called roughly every 1ms and can output 1 or more ints and is trying to maintain a constant out put rate of r ints per 1s but can output more if the intrnal buffer of the shaper is in danger of getting filled up. However the output should be as uniform as possible. For example: Out: 1 1 2 2 2 1 is better than Out: 1 1 5 1 1

since the second example is bursty (there's an output of 5 ints).

I have an idea of how to do this using leaky bucket algorithm.

My question: How to implement in Go that output is called semi-regularly roughly at 1ms ticks?


Solution

  • How to implement in Go that output is called semi-regularly roughly at 1ms ticks?

    Use the standard time.Ticker, configured to flush output every 1 millisecond.