{"id":431,"date":"2026-05-24T09:00:00","date_gmt":"2026-05-24T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/"},"modified":"2026-05-24T09:00:00","modified_gmt":"2026-05-24T03:30:00","slug":"go-rate-limiting","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/","title":{"rendered":"Go Rate Limiting with the Token Bucket Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go rate limiting protects your API by limiting how many requests each client can send in a given time. In this post, you will learn what a token bucket is and why it works so well. Then we will build a per-client limiter as middleware, and we will test it with real output.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This lesson uses <a href=\"https:\/\/smitabharat.com\/blog\/go-middleware-examples\/\">Go middleware examples<\/a>, so read that post if middleware is new. We also use a mutex from <a href=\"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/\">Go sync package with WaitGroup and Mutex<\/a>. In addition, the handler style comes from <a href=\"https:\/\/smitabharat.com\/blog\/go-http-server-beginners\/\">Go HTTP server basics<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is Go Rate Limiting?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In short, rate limiting means you allow only a fixed number of requests in a period of time. If a client sends too many, the server answers with status 429, which means &#8220;Too Many Requests&#8221;. Then the client has to wait.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a water tap with a small bucket under it. The tap adds one drop per second. Each request takes one drop from the bucket. When the bucket is empty, the next request must wait for a new drop. Meanwhile, the bucket can hold only a few drops, so a long quiet time does not give unlimited credit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This idea is called a token bucket. It is simple, fast and fair. As a result, most real systems use it, including many cloud APIs. For example, a free plan might allow 60 calls per minute, while a paid plan allows 600. Both plans can use the same Go rate limiting code with different numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Rate Limiting Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Without limits, one noisy client can slow down everyone else. For example, a buggy script that retries in a tight loop can send thousands of calls per second. So here is what Go rate limiting gives you.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Protection from overload<\/strong>: a burst of traffic cannot crush your database.<\/li>\n\n\n\n<li><strong>Fair sharing<\/strong>: every client gets a similar share of your capacity.<\/li>\n\n\n\n<li><strong>Defence against abuse<\/strong>: password guessing and scraping become much slower.<\/li>\n\n\n\n<li><strong>Predictable costs<\/strong>: calls to paid services stay under control.<\/li>\n\n\n\n<li><strong>Better uptime<\/strong>: your API keeps working during traffic spikes.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Because of this, rate limiting is a basic part of any public API. In fact, security reviews often ask about it first. However, it also helps internal services, because one broken service can otherwise flood another.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Rate Limiting Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Go rate limiting, the token bucket has two numbers. First, the capacity is the largest burst the bucket allows. Second, the refill rate is how many tokens are added each second. When a request arrives, the code first adds the tokens earned since the last check. Then it tries to take one token. If a token is available, the request goes through. Otherwise, the request is rejected.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-1-1024x640.png\" alt=\"Go Rate Limiting diagram showing a token bucket that refills and rejects the fourth request with 429\" class=\"wp-image-412\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">A token bucket refills at a steady rate. Each request takes one token, and requests that find an empty bucket receive a 429 response.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Notice that nothing needs a background timer. Instead, the bucket calculates the new tokens from the time that has passed. As a result, it uses almost no CPU, even with thousands of clients. That is why this design is popular in Go rate limiting libraries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Burst and Rate<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">These two settings control how the limiter feels to a user. The table below explains them with examples.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Setting<\/th><th>Meaning<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td>Capacity (burst)<\/td><td>Most requests allowed at once<\/td><td>3<\/td><\/tr><tr><td>Rate<\/td><td>Tokens added per second<\/td><td>1<\/td><\/tr><tr><td>Result<\/td><td>Fast start, then a steady pace<\/td><td>3 quick calls, then 1 per second<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So a burst of 3 and a rate of 1 means a user can send three quick requests. After that, they get one new request every second. Choose the burst for how spiky normal users are, and choose the rate for how much load you can carry.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Where to Keep the Buckets<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A fair limiter needs one bucket for each client, usually keyed by IP address or API key. Therefore, keep a map from key to bucket, and protect the map with a mutex. Otherwise, many requests could write to the map at the same time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go Rate Limiting: Token Bucket vs Fixed Window<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">There are two popular ways to count requests. The table below compares them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Token bucket<\/th><th>Fixed window<\/th><\/tr><\/thead><tbody><tr><td>Idea<\/td><td>Tokens refill steadily<\/td><td>A counter resets every minute<\/td><\/tr><tr><td>Allows bursts<\/td><td>Yes, up to the capacity<\/td><td>Yes, and double bursts at window edges<\/td><\/tr><tr><td>Memory per client<\/td><td>Two numbers<\/td><td>One number<\/td><\/tr><tr><td>Smoothness<\/td><td>Smooth<\/td><td>Can feel jumpy<\/td><\/tr><tr><td>Best for<\/td><td>Most public APIs<\/td><td>Very simple daily or hourly quotas<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-2-1024x640.png\" alt=\"Go Rate Limiting diagram comparing a token bucket with a fixed window\" class=\"wp-image-413\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">A token bucket spreads requests evenly over time, while a fixed window can allow a burst at the end of one minute and another at the start of the next.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So when should you use which? Use a token bucket for most APIs, because it is smooth and fair. On the other hand, a fixed window is fine for simple quotas, such as &#8220;1000 calls per day&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go Rate Limiting Hands-On: Per-IP Middleware<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s build a limiter with only the standard library. It allows a burst of 3 requests and refills 1 token per second for every IP address.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: The Token Bucket<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, here is the bucket. It stores the tokens, the capacity, the refill rate and the time of the last check.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Bucket struct {\n\tmu       sync.Mutex\n\ttokens   float64\n\tcapacity float64\n\trate     float64 \/\/ tokens added per second\n\tlast     time.Time\n}\n\nfunc NewBucket(capacity, ratePerSec float64) *Bucket {\n\treturn &amp;Bucket{tokens: capacity, capacity: capacity, rate: ratePerSec, last: time.Now()}\n}\n\nfunc (b *Bucket) Allow() bool {\n\tb.mu.Lock()\n\tdefer b.mu.Unlock()\n\n\tnow := time.Now()\n\tb.tokens += now.Sub(b.last).Seconds() * b.rate\n\tif b.tokens &gt; b.capacity {\n\t\tb.tokens = b.capacity\n\t}\n\tb.last = now\n\n\tif b.tokens &lt; 1 {\n\t\treturn false\n\t}\n\tb.tokens--\n\treturn true\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s walk through <code>Allow<\/code>. First, it adds the tokens earned since the last call. Then it caps the total at the capacity, so a long pause cannot save up unlimited tokens. Finally, it takes one token if there is at least one, and otherwise it returns false. Also, the mutex inside the bucket makes the method safe when many goroutines call it at once.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: One Bucket per Client<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, the <code>Limiter<\/code> creates a bucket the first time it sees an IP address.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Limiter struct {\n\tmu       sync.Mutex\n\tbuckets  map[string]*Bucket\n\tcapacity float64\n\trate     float64\n}\n\nfunc NewLimiter(capacity, rate float64) *Limiter {\n\treturn &amp;Limiter{buckets: map[string]*Bucket{}, capacity: capacity, rate: rate}\n}\n\nfunc (l *Limiter) get(ip string) *Bucket {\n\tl.mu.Lock()\n\tdefer l.mu.Unlock()\n\tb, ok := l.buckets[ip]\n\tif !ok {\n\t\tb = NewBucket(l.capacity, l.rate)\n\t\tl.buckets[ip] = b\n\t}\n\treturn b\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: The Middleware<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Then the middleware reads the client IP, asks the bucket and either calls the next handler or returns 429.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (l *Limiter) Middleware(next http.Handler) http.Handler {\n\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tip, _, err := net.SplitHostPort(r.RemoteAddr)\n\t\tif err != nil {\n\t\t\tip = r.RemoteAddr\n\t\t}\n\t\tif !l.get(ip).Allow() {\n\t\t\tw.Header().Set(\"Retry-After\", \"1\")\n\t\t\thttp.Error(w, \"too many requests\", http.StatusTooManyRequests)\n\t\t\treturn\n\t\t}\n\t\tnext.ServeHTTP(w, r)\n\t})\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>Retry-After<\/code> header tells well-behaved clients how many seconds to wait. Also, <code>net.SplitHostPort<\/code> removes the port, so one user is not counted as many. Without that step, each new connection would get its own bucket, and the limit would never trigger.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Try It<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, we send five requests from one IP, one from a second IP and one more after waiting. Here is the real output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>request 1 from 10.0.0.1: 200\nrequest 2 from 10.0.0.1: 200\nrequest 3 from 10.0.0.1: 200\nrequest 4 from 10.0.0.1: 429\nrequest 5 from 10.0.0.1: 429\nrequest 1 from 10.0.0.2: 200\nafter 1.1s from 10.0.0.1: 200<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first three requests use the burst. After that, requests 4 and 5 get a 429. Meanwhile, the second IP has its own full bucket. After waiting 1.1 seconds, the first IP has earned one new token, so it succeeds again.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Use the Official Package<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In production, many teams use the <code>golang.org\/x\/time\/rate<\/code> package instead of writing their own bucket. First, install it with <code>go get golang.org\/x\/time\/rate<\/code>. Then the same limit looks like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>limiter := rate.NewLimiter(rate.Limit(1), 3) \/\/ 1 token per second, burst of 3\n\nif !limiter.Allow() {\n\thttp.Error(w, \"too many requests\", http.StatusTooManyRequests)\n\treturn\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This package also has <code>Wait<\/code> and <code>Reserve<\/code>, which let a request sleep until a token is ready. It is well tested, so it is a good default choice. Even so, writing the bucket yourself once is a great way to understand how Go rate limiting really works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Go Rate Limiting<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>One global limit for everyone<\/strong>: a single noisy client then blocks all others. So keep one bucket per client.<\/li>\n\n\n\n<li>**Trusting <code>X-Forwarded-For<\/code> blindly**: attackers can fake it. Therefore, read it only behind a proxy you control.<\/li>\n\n\n\n<li><strong>Never removing old buckets<\/strong>: the map then grows forever. Instead, delete buckets that have been idle for a while.<\/li>\n\n\n\n<li><strong>Using a map without a lock<\/strong>: concurrent writes can crash the program. So protect it with a mutex.<\/li>\n\n\n\n<li><strong>Limits in memory only<\/strong>: with many servers, each one counts alone. In that case, use a shared store such as Redis.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Rate Limiting<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>**Return 429 with <code>Retry-After<\/code>**, because clients can then back off politely.<\/li>\n\n\n\n<li><strong>Use different limits for different routes<\/strong>, since login needs stricter limits than reads. A login route might allow 5 tries per minute.<\/li>\n\n\n\n<li><strong>Clean up idle buckets<\/strong>, so memory stays flat.<\/li>\n\n\n\n<li><strong>Log rejected requests<\/strong>, because spikes often show an attack or a buggy client.<\/li>\n\n\n\n<li><strong>Test with a fake clock<\/strong>, since sleeping in tests is slow.<\/li>\n\n\n\n<li><strong>Document your limits<\/strong>, so API users know what to expect.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To practise Go rate limiting, add a cleanup goroutine to the <code>Limiter<\/code>. Every minute it should remove buckets that were not used in the last five minutes. Then test it with a short time, such as one second, so you do not wait.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hint: store a <code>lastSeen<\/code> time in each bucket. After that, loop over the map with the lock held and delete old entries. Finally, stop the goroutine with a context.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These pages cover the rate package and the HTTP status code used in this lesson.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/golang.org\/x\/time\/rate\">golang.org\/x\/time\/rate package documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Status\/429\">MDN: 429 Too Many Requests<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/net\/http\">net\/http package documentation<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go rate limiting caps how many requests a client can send.<\/li>\n\n\n\n<li>A token bucket has a capacity for bursts and a refill rate.<\/li>\n\n\n\n<li>The bucket works out new tokens from elapsed time, so it needs no timer.<\/li>\n\n\n\n<li>Keep one bucket per client and guard the map with a mutex.<\/li>\n\n\n\n<li>Return 429 with a <code>Retry-After<\/code> header when the bucket is empty.<\/li>\n\n\n\n<li>Use <code>golang.org\/x\/time\/rate<\/code> in production and Redis for many servers.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By now, you can protect an API from overload. Next, we will switch to the browser side and learn how to render safe HTML pages with Go templates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next up: <a href=\"https:\/\/smitabharat.com\/blog\/go-html-templates\/\">Go HTML templates for web pages<\/a><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Go rate limiting protects your API by limiting how many requests each client can send in a given time. In this post, you will&hellip;<\/p>\n","protected":false},"author":1,"featured_media":411,"comment_status":"","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[15],"tags":[44,11,63,77],"swapz_series":[],"class_list":["post-431","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-lang","tag-go-http-server","tag-go-lang","tag-go-middleware","tag-intermediate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Rate Limiting with the Token Bucket Explained<\/title>\n<meta name=\"description\" content=\"Learn Go rate limiting step by step: how a token bucket works, build per-IP middleware, return 429 and use the official rate package.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Rate Limiting with the Token Bucket Explained\" \/>\n<meta property=\"og:description\" content=\"Learn Go rate limiting step by step: how a token bucket works, build per-IP middleware, return 429 and use the official rate package.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-05-24T03:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Rate Limiting with the Token Bucket Explained\",\"datePublished\":\"2026-05-24T03:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/\"},\"wordCount\":1528,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-rate-limiting-featured.png\",\"keywords\":[\"Go HTTP Server\",\"Go Lang\",\"Go Middleware\",\"Intermediate\"],\"articleSection\":[\"Go Lang\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/\",\"name\":\"Go Rate Limiting with the Token Bucket Explained\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-rate-limiting-featured.png\",\"datePublished\":\"2026-05-24T03:30:00+00:00\",\"description\":\"Learn Go rate limiting step by step: how a token bucket works, build per-IP middleware, return 429 and use the official rate package.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-rate-limiting-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-rate-limiting-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Rate Limiting featured image: 200 within the burst, 429 when the bucket is empty\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-rate-limiting\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Rate Limiting with the Token Bucket Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Rate Limiting with the Token Bucket Explained","description":"Learn Go rate limiting step by step: how a token bucket works, build per-IP middleware, return 429 and use the official rate package.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/","og_locale":"en_US","og_type":"article","og_title":"Go Rate Limiting with the Token Bucket Explained","og_description":"Learn Go rate limiting step by step: how a token bucket works, build per-IP middleware, return 429 and use the official rate package.","og_url":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-05-24T03:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Rate Limiting with the Token Bucket Explained","datePublished":"2026-05-24T03:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/"},"wordCount":1528,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-featured.png","keywords":["Go HTTP Server","Go Lang","Go Middleware","Intermediate"],"articleSection":["Go Lang"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/","url":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/","name":"Go Rate Limiting with the Token Bucket Explained","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-featured.png","datePublished":"2026-05-24T03:30:00+00:00","description":"Learn Go rate limiting step by step: how a token bucket works, build per-IP middleware, return 429 and use the official rate package.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-rate-limiting\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-rate-limiting-featured.png","width":2400,"height":1260,"caption":"Go Rate Limiting featured image: 200 within the burst, 429 when the bucket is empty"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Rate Limiting with the Token Bucket Explained"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=431"}],"version-history":[{"count":0,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/431\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/411"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=431"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}