
This bugfix implements correct way to calculate source sha1 hash, before it will changed, by propagation of unchanged sources. This commit will also include regenerated static files and *.hash files.
269 lines
12 KiB
Plaintext
Generated
269 lines
12 KiB
Plaintext
Generated
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Go by Example: Rate Limiting</title>
|
|
<link rel=stylesheet href="site.css">
|
|
</head>
|
|
<script>
|
|
onkeydown = (e) => {
|
|
|
|
if (e.key == "ArrowLeft") {
|
|
window.location.href = 'waitgroups';
|
|
}
|
|
|
|
|
|
if (e.key == "ArrowRight") {
|
|
window.location.href = 'atomic-counters';
|
|
}
|
|
|
|
}
|
|
</script>
|
|
<body>
|
|
<div class="example" id="rate-limiting">
|
|
<h2><a href="./">Go by Example</a>: Rate Limiting</h2>
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p><em><a href="http://en.wikipedia.org/wiki/Rate_limiting">Rate limiting</a></em>
|
|
is an important mechanism for controlling resource
|
|
utilization and maintaining quality of service. Go
|
|
elegantly supports rate limiting with goroutines,
|
|
channels, and <a href="tickers">tickers</a>.</p>
|
|
|
|
</td>
|
|
<td class="code empty leading">
|
|
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
<a href="http://play.golang.org/p/20c_m1AtOEI"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
|
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
|
<span class="s">"fmt"</span>
|
|
<span class="s">"time"</span>
|
|
<span class="p">)</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>First we’ll look at basic rate limiting. Suppose
|
|
we want to limit our handling of incoming requests.
|
|
We’ll serve these requests off a channel of the
|
|
same name.</p>
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre> <span class="nx">requests</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="kt">int</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
|
|
<span class="k">for</span> <span class="nx">i</span> <span class="o">:=</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><=</span> <span class="mi">5</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span>
|
|
<span class="nx">requests</span> <span class="o"><-</span> <span class="nx">i</span>
|
|
<span class="p">}</span>
|
|
<span class="nb">close</span><span class="p">(</span><span class="nx">requests</span><span class="p">)</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>This <code>limiter</code> channel will receive a value
|
|
every 200 milliseconds. This is the regulator in
|
|
our rate limiting scheme.</p>
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre> <span class="nx">limiter</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Tick</span><span class="p">(</span><span class="mi">200</span> <span class="o">*</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Millisecond</span><span class="p">)</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>By blocking on a receive from the <code>limiter</code> channel
|
|
before serving each request, we limit ourselves to
|
|
1 request every 200 milliseconds.</p>
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre> <span class="k">for</span> <span class="nx">req</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">requests</span> <span class="p">{</span>
|
|
<span class="o"><-</span><span class="nx">limiter</span>
|
|
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"request"</span><span class="p">,</span> <span class="nx">req</span><span class="p">,</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Now</span><span class="p">())</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>We may want to allow short bursts of requests in
|
|
our rate limiting scheme while preserving the
|
|
overall rate limit. We can accomplish this by
|
|
buffering our limiter channel. This <code>burstyLimiter</code>
|
|
channel will allow bursts of up to 3 events.</p>
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre> <span class="nx">burstyLimiter</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Time</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>Fill up the channel to represent allowed bursting.</p>
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre> <span class="k">for</span> <span class="nx">i</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="p"><</span> <span class="mi">3</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span>
|
|
<span class="nx">burstyLimiter</span> <span class="o"><-</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Now</span><span class="p">()</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>Every 200 milliseconds we’ll try to add a new
|
|
value to <code>burstyLimiter</code>, up to its limit of 3.</p>
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre> <span class="k">go</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
|
|
<span class="k">for</span> <span class="nx">t</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Tick</span><span class="p">(</span><span class="mi">200</span> <span class="o">*</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Millisecond</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="nx">burstyLimiter</span> <span class="o"><-</span> <span class="nx">t</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}()</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>Now simulate 5 more incoming requests. The first
|
|
3 of these will benefit from the burst capability
|
|
of <code>burstyLimiter</code>.</p>
|
|
|
|
</td>
|
|
<td class="code">
|
|
|
|
<div class="highlight"><pre> <span class="nx">burstyRequests</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="kt">int</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
|
|
<span class="k">for</span> <span class="nx">i</span> <span class="o">:=</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><=</span> <span class="mi">5</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span>
|
|
<span class="nx">burstyRequests</span> <span class="o"><-</span> <span class="nx">i</span>
|
|
<span class="p">}</span>
|
|
<span class="nb">close</span><span class="p">(</span><span class="nx">burstyRequests</span><span class="p">)</span>
|
|
<span class="k">for</span> <span class="nx">req</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">burstyRequests</span> <span class="p">{</span>
|
|
<span class="o"><-</span><span class="nx">burstyLimiter</span>
|
|
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"request"</span><span class="p">,</span> <span class="nx">req</span><span class="p">,</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Now</span><span class="p">())</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>Running our program we see the first batch of requests
|
|
handled once every ~200 milliseconds as desired.</p>
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre><span class="gp">$</span> go run rate-limiting.go
|
|
<span class="go">request 1 2012-10-19 00:38:18.687438 +0000 UTC</span>
|
|
<span class="go">request 2 2012-10-19 00:38:18.887471 +0000 UTC</span>
|
|
<span class="go">request 3 2012-10-19 00:38:19.087238 +0000 UTC</span>
|
|
<span class="go">request 4 2012-10-19 00:38:19.287338 +0000 UTC</span>
|
|
<span class="go">request 5 2012-10-19 00:38:19.487331 +0000 UTC</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>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.</p>
|
|
|
|
</td>
|
|
<td class="code">
|
|
|
|
<div class="highlight"><pre><span class="go">request 1 2012-10-19 00:38:20.487578 +0000 UTC</span>
|
|
<span class="go">request 2 2012-10-19 00:38:20.487645 +0000 UTC</span>
|
|
<span class="go">request 3 2012-10-19 00:38:20.487676 +0000 UTC</span>
|
|
<span class="go">request 4 2012-10-19 00:38:20.687483 +0000 UTC</span>
|
|
<span class="go">request 5 2012-10-19 00:38:20.887542 +0000 UTC</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
|
<p class="next">
|
|
Next example: <a href="atomic-counters">Atomic Counters</a>.
|
|
</p>
|
|
|
|
<p class="footer">
|
|
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/rate-limiting">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
|
|
</p>
|
|
</div>
|
|
<script>
|
|
var codeLines = [];
|
|
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' requests := make(chan int, 5)\u000A for i := 1; i \x3C= 5; i++ {\u000A requests \x3C- i\u000A }\u000A close(requests)\u000A');codeLines.push(' limiter := time.Tick(200 * time.Millisecond)\u000A');codeLines.push(' for req := range requests {\u000A \x3C-limiter\u000A fmt.Println(\"request\", req, time.Now())\u000A }\u000A');codeLines.push(' burstyLimiter := make(chan time.Time, 3)\u000A');codeLines.push(' for i := 0; i \x3C 3; i++ {\u000A burstyLimiter \x3C- time.Now()\u000A }\u000A');codeLines.push(' go func() {\u000A for t := range time.Tick(200 * time.Millisecond) {\u000A burstyLimiter \x3C- t\u000A }\u000A }()\u000A');codeLines.push(' burstyRequests := make(chan int, 5)\u000A for i := 1; i \x3C= 5; i++ {\u000A burstyRequests \x3C- i\u000A }\u000A close(burstyRequests)\u000A for req := range burstyRequests {\u000A \x3C-burstyLimiter\u000A fmt.Println(\"request\", req, time.Now())\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
|
|
</script>
|
|
<script src="site.js" async></script>
|
|
</body>
|
|
</html>
|