File: rate-limiting.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; ansic: 288; makefile: 9; sh: 6
file content (61 lines) | stat: -rwxr-xr-x 1,412 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env ruby

$: << File.expand_path('../../../lib', __FILE__)
require 'concurrent-edge'
require 'time'

Channel = Concurrent::Channel

## Go by Example: Rate Limiting
# https://gobyexample.com/rate-limiting

requests = Channel.new(buffer: :buffered, capacity: 5)
(1..5).each do |i|
  requests << i
end
requests.close

limiter = Channel.ticker(0.2)
requests.each do |req|
  print "request #{req} #{Channel::Tick.new}\n" if ~limiter
end
print "\n"

bursty_limiter = Channel.new(buffer: :buffered, capacity: 3)
(1..3).each do
  bursty_limiter << Channel::Tick.new
end

ticker = Channel.ticker(0.2)
Channel.go do
  ticker.each do |t|
    bursty_limiter << t
  end
end

bursty_requests = Channel.new(buffer: :buffered, capacity: 5)
(1..5).each do |i|
  bursty_requests << i
end
bursty_requests.close

bursty_requests.each do |req|
  ~bursty_limiter
  print "request #{req} #{Channel::Tick.new}\n"
end

limiter.close
ticker.close

__END__
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

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