liveBook · Manning

Throttler

As explained in Go by Example

A throttler is a concurrency component that spaces messages or operations by delaying their forwarding or execution.

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.
A screenshot of a computer

AI-generated content may be incorrect.

Use the pipeline diagram to see the throttler between the producer and dispatcher and observe where message flow is slowed. section 6.8

How Go by Example covers it

How deep it goes
Defines the stage, implements it with channels and time.Ticker, and defers cancellation handling to later treatment.
Examples
Demonstrates a Go throttle function using time.Ticker and conditional integration through an RPS option.
What it assumes
Assumes basic Go concurrency knowledge, including goroutines and channels.
What you can do after
Build it from scratch as a channel-based Go pipeline stage and configure it with a requests-per-second rate.
How it connects
Connects producer stages, dispatcher stages, channels, goroutines, concurrent pipelines, and HTTP requests.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go by Example. The section links point to the parts of the book it draws on.