• Stars
    star
    144
  • Rank 246,477 (Top 5 %)
  • Language
    Lua
  • License
    BSD 3-Clause "New...
  • Created over 5 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A lua library to provide distributed rate measurement using nginx + redis, you can use it to do a throttling system within many nodes.

Build Status license Coverage Status

Resty Redis Rate

A Lua library to provide rate measurement using nginx + Redis. This lib was inspired on Cloudflare's post How we built rate limiting capable of scaling to millions of domains.

You can found more about why and when this library was created here..

Use case: distributed throttling

Nginx has already a rate limiting feature but it is restricted by the local node. Once you have more than one server behind a load balancer this won't work as expected, so you can use redis as a distributed storage to keep the rating data.

local redis_client = redis_cluster:new(config)
-- let's say we'll use the ?token=<value> as the key to rate limit
local rate, err = redis_rate.measure(redis_client, ngx.var.arg_token)
if err then
    ngx.log(ngx.ERR, "err: ", err)
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end

-- once we hit more than 10 reqs/m we'll reply 403
if rate > 10 then
    ngx.exit(ngx.HTTP_FORBIDDEN)
end

ngx.say(rate)

Tests result

We ran three different experiments constrained by a rate limit of 10 req/minute:

  1. Experiment1: 1 reqs/second
  2. Experiment2: 1/6 reqs/second
  3. Experiment3: 1/5 reqs/second

nginx redis throttling exprimentes graph result

All the data points above the rate limit (the red line) resulted in forbidden responses.

You can run the throttling example locally, open up a terminal tab to run the servers.

Make sure you have docker and docker-compose installed.

make up

Open another terminal tab and perform the experiments:

# Experiment 1
for i in {1..120}; do curl "http://localhost:8080/lua_content?token=Experiment1" && sleep 1; done

# Experiment 2
for i in {1..20}; do curl "http://localhost:8080/lua_content?token=Experiment2" && sleep 6; done

# Experiment 3
for i in {1..24}; do curl "http://localhost:8080/lua_content?token=Experiment3" && sleep 5; done

Pipeline and hash tag

We're using the combination of pipeline and hash tag to perform all the commands in a single connection to redis cluster. You can see the tcpdump output showing the three-way handshake followed by the three commands requests $get, $inc and $expire and the redis response.

22:20:10.515457 IP (tos 0x0, ttl 64, id 38199, offset 0, flags [DF], proto TCP (6), length 60)
    172.31.0.3.49824 > 172.31.0.2.7000: Flags [S], cksum 0x5872 (incorrect -> 0xb9b9), seq 1010830934, win 29200, options [mss 1460,sackOK,TS val 170849 ecr 0,nop,wscale 7], length 0

22:20:10.515505 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 60)
    172.31.0.2.7000 > 172.31.0.3.49824: Flags [S.], cksum 0x5872 (incorrect -> 0xfcda), seq 1496303914, ack 1010830935, win 28960, options [mss 1460,sackOK,TS val 170849 ecr 170849,nop,wscale 7], length 0

22:20:10.515518 IP (tos 0x0, ttl 64, id 38200, offset 0, flags [DF], proto TCP (6), length 52)
    172.31.0.3.49824 > 172.31.0.2.7000: Flags [.], cksum 0x586a (incorrect -> 0x9be2), seq 1, ack 1, win 229, options [nop,nop,TS val 170849 ecr 170849], length 0

22:20:10.515648 IP (tos 0x0, ttl 64, id 38201, offset 0, flags [DF], proto TCP (6), length 212)
    172.31.0.3.49824 > 172.31.0.2.7000: Flags [P.], cksum 0x590a (incorrect -> 0x7954), seq 1:161, ack 1, win 229, options [nop,nop,TS val 170849 ecr 170849], length 160
	0x0000:  4500 00d4 9539 4000 4006 4ca7 ac1f 0003  E....9@[email protected].....
	0x0010:  ac1f 0002 c2a0 1b58 3c40 0e57 592f c92b  .......X<@.WY/.+
	0x0020:  8018 00e5 590a 0000 0101 080a 0002 9b61  ....Y..........a
	0x0030:  0002 9b61 2a32 0d0a 2433 0d0a 6765 740d  ...a*2..$3..get.
	0x0040:  0a24 3239 0d0a 6e67 785f 7261 7465 5f6d  .$29..ngx_rate_m
	0x0050:  6561 7375 7269 6e67 5f7b 6c75 6967 697d  easuring_{luigi}
	0x0060:  5f31 390d 0a2a 320d 0a24 340d 0a69 6e63  _19..*2..$4..inc
	0x0070:  720d 0a24 3239 0d0a 6e67 785f 7261 7465  r..$29..ngx_rate
	0x0080:  5f6d 6561 7375 7269 6e67 5f7b 6c75 6967  _measuring_{luig
	0x0090:  697d 5f32 300d 0a2a 330d 0a24 360d 0a65  i}_20..*3..$6..e
	0x00a0:  7870 6972 650d 0a24 3239 0d0a 6e67 785f  xpire..$29..ngx_
	0x00b0:  7261 7465 5f6d 6561 7375 7269 6e67 5f7b  rate_measuring_{
	0x00c0:  6c75 6967 697d 5f32 300d 0a24 330d 0a31  luigi}_20..$3..1
	0x00d0:  3230 0d0a                                20..
22:20:10.517337 IP (tos 0x0, ttl 64, id 21067, offset 0, flags [DF], proto TCP (6), length 65)
    172.31.0.2.7000 > 172.31.0.3.49824: Flags [P.], cksum 0x5877 (incorrect -> 0xc55e), seq 1:14, ack 161, win 235, options [nop,nop,TS val 170849 ecr 170849], length 13
	0x0000:  4500 0041 524b 4000 4006 9028 ac1f 0002  E..ARK@.@..(....
	0x0010:  ac1f 0003 1b58 c2a0 592f c92b 3c40 0ef7  .....X..Y/.+<@..
	0x0020:  8018 00eb 5877 0000 0101 080a 0002 9b61  ....Xw.........a
	0x0030:  0002 9b61 242d 310d 0a3a 310d 0a3a 310d  ...a$-1..:1..:1.
	0x0040:  0a                                       .

More Repositories

1

digital_video_introduction

A hands-on introduction to video technology: image, video, codec (av1, vp9, h265) and more (ffmpeg encoding). Translations: 🇺🇸 🇨🇳 🇯🇵 🇮🇹 🇰🇷 🇷🇺 🇧🇷 🇪🇸
Jupyter Notebook
15,022
star
2

ffmpeg-libav-tutorial

FFmpeg libav tutorial - learn how media works from basic to transmuxing, transcoding and more. Translations: 🇺🇸 🇨🇳 🇰🇷 🇪🇸 🇻🇳 🇧🇷
C
9,443
star
3

linux-network-performance-parameters

Learn where some of the network sysctl variables fit into the Linux/Kernel network flow. Translations: 🇷🇺
5,276
star
4

cdn-up-and-running

CDN Up and Running - Building a CDN from Scratch to Learn about CDN, Nginx, Lua, Prometheus, Grafana, Load balancing, and Containers.
Lua
3,033
star
5

redlock-rb

Redlock is a redis-based distributed lock implementation in Ruby. More than 20M downloads.
Ruby
655
star
6

live-stream-from-desktop

Provide guidance to test live streaming (mpeg-dash or hls) or vod from your desktop
Shell
169
star
7

http-video-streaming-troubleshooting

A collection of fixes / problem solutions to HTTP video streaming
77
star
8

nott

The New OTT Platform - an excuse to discuss and design a simple edge computing platform
Lua
50
star
9

video-containers-debugging-tools

A set of command lines to debug video streaming files like mp4 (MPEG-4 Part 14), ts (MPEG-2 Part 1), fmp4 in Dash, HLS, or MSS, with or without DRM.
45
star
10

python_chip16

A full implementation (tested) of chip16 virtual machine, or emulator as you wish, using python and rendering with opengl.
Python
34
star
11

scte-35-scte-104-scte-67

Documentation/references about Dynamic Ad Insertion (DAI) through SCTE-104, SCTE-35 to HLS, MPEG DASH, Smooth, RTMP
30
star
12

tls_certificate_generation

Use temporary Amazon EC2 / Digital Ocean cloud machines to get / renew letsencrypt certificates
Shell
28
star
13

docker-ffmpeg-vmaf

Docker FFmpeg VMAF usage example / tips / workflow
25
star
14

edge-computing-resty

a simple edge computing platform using nginx, lua and rails
Ruby
24
star
15

lua-resty-dynacode

A library to provide dynamic (via json/API) load of lua byte code into nginx/openresty.
Ruby
24
star
16

resty-bakery

An Nginx+Lua library to modify media manifests like HLS and MPEG Dash, acting like a proxy.
Lua
21
star
17

player-ffmpeg

Up to date tutorial of ffmpeg
C
19
star
18

fake-as-fuck

I'm a lonely, lonely lord. Pick another day to be restored. Just another fate that's going overboard.
Python
19
star
19

lua-resty-perf

A small ngx resty lua library to benchmark memory and throughput of a function.
Lua
16
star
20

kaltura-media-framework-docker-compose

A docker compose for https://github.com/kaltura/media-framework
Go
13
star
21

cassandra-lock

A ruby lib to achieve consensus with Cassandra
Ruby
12
star
22

dotfiles

dotfiles
Shell
8
star
23

JChip16BR

An java implementation of VM Chip16.
Assembly
8
star
24

clappr-pause-tab-visibility

A clappr container plugin to pause when user is in another tab and resume when the user is back.
JavaScript
6
star
25

py-chip8

introduction to low level thingies
Python
6
star
26

tc-linux-rate-delay-specific-ip-on-docker

a docker experiment to learn to apply tc (linux traffic control) to shaping and delaying network for a specific CIDR/network.
Shell
6
star
27

jgb

gameboy emulator written in JavaScript
JavaScript
4
star
28

ruby_meta_programming

Meta programming Ruby - Book practise
Ruby
4
star
29

dcpu16

JDCPU16BR
Java
4
star
30

nginx-throttling-leaderboard

POC to test local throttling and top leaderboard most requested token
Lua
3
star
31

js_learning

My javascript training files
JavaScript
3
star
32

jmf

jmf - java media framework example of server and player
Java
3
star
33

hackday-27-06-19

Globo.com hackday-27-06-19
Lua
2
star
34

clojure_playground

It is a simple repo to study clojure
Clojure
2
star
35

clappr-dash-dashjs

A dash playback (based on dashjs) for 🎬 Clappr
HTML
2
star
36

clint

An ordinary encoder.
JavaScript
2
star
37

clojure-playground

Silly repo -> While I'm reading and learning about clojure I will use this guy to keep track
Clojure
2
star
38

jchip8br

Automatically exported from code.google.com/p/jchip8br
Java
1
star
39

playground.sh

A place to practice the fine art of shell script
Shell
1
star
40

d3.playground

A place to play with d3js
1
star
41

rslimbr

Ruby Slim Protocol for Fitnesse compatible with Ruby 1.9.x
Ruby
1
star
42

minimal-js-dash-player

JavaScript
1
star
43

mediainfo

docker image for mediainfo
1
star
44

playground.coffe

just another playground for a 'new' language
1
star
45

angularjs-pet

Pet project to learn and apply angularjs for large project with tests and fine structure.
JavaScript
1
star
46

jschip16br

the chip16 virtual machine specification implemented using javascript
1
star
47

test_legacy

Improve your legacy project providing certain level of test automation
Ruby
1
star
48

rspec_book

the book in praticse
Ruby
1
star
49

cat

Container as a Teacher
Shell
1
star