Implementing Rate Limiting in Go | Challenge

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

The challenge demonstrates how to implement rate limiting in Go using goroutines, channels, and tickers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/ConcurrencyGroup(["`Concurrency`"]) go/ConcurrencyGroup -.-> go/rate_limiting("`Rate Limiting`") subgraph Lab Skills go/rate_limiting -.-> lab-15418{{"`Implementing Rate Limiting in Go | Challenge`"}} end

Rate Limiting

The problem is to limit the handling of incoming requests to maintain quality of service and control resource utilization.

Requirements

  • Go programming language
  • Basic understanding of goroutines, channels, and tickers

Example

## Running our program we see the first batch of requests
## handled once every ~200 milliseconds as desired.
$ go run rate-limiting.go
request 1 2012-10-19 00:38:18.687438 +0000 UTC
request 2 2012-10-19 00:38:18.887471 +0000 UTC
request 3 2012-10-19 00:38:19.087238 +0000 UTC
request 4 2012-10-19 00:38:19.287338 +0000 UTC
request 5 2012-10-19 00:38:19.487331 +0000 UTC

## For the second batch of requests we serve the first
## 3 immediately because of the burstable rate limiting,
## then serve the remaining 2 with ~200ms delays each.
request 1 2012-10-19 00:38:20.487578 +0000 UTC
request 2 2012-10-19 00:38:20.487645 +0000 UTC
request 3 2012-10-19 00:38:20.487676 +0000 UTC
request 4 2012-10-19 00:38:20.687483 +0000 UTC
request 5 2012-10-19 00:38:20.887542 +0000 UTC

Summary

The challenge demonstrates how to implement rate limiting in Go using goroutines, channels, and tickers to control resource utilization and maintain quality of service.

Other Go Tutorials you may like