A throttler is a concurrent pipeline stage that spaces messages before forwarding them. It protects a target server from overload and supports more accurate performance measurements. section 6.8
A throttler typically:
- Receives requests from a producer through an input channel. section 6.8.1
- Waits for a fixed interval before forwarding each request. section 6.8.1
- Passes requests to dispatcher goroutines through an output channel. section 6.8
- Activates only when configuration requests a positive requests-per-second rate. section 6.8.2
Go throttler stage
func throttle(in <-chan *http.Request, delay time.Duration) <-chan *http.Request { out := make(chan *http.Request) go func() { defer close(out) t := time.NewTicker(delay) for r := range in { <-t.C out <- r } }() return out } Integrate it after the producer with throttle(requests, time.Second/time.Duration(opts.RPS)) when opts.RPS > 0. section 6.8.1 section 6.8.2
Figure 6.5 The throttler stage slows the flow of messages in the pipeline.
Use the pipeline diagram to see the throttler between the producer and dispatcher and observe where message flow is slowed. section 6.8
