-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresty_din_rate_limit.lua
94 lines (72 loc) · 2.62 KB
/
resty_din_rate_limit.lua
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
local cjson = require "cjson"
local limit_traffic = require "resty.limit.count"
local config_file_path = "/usr/local/openresty/lua/rate_limit.json"
local shared_config_cache = ngx.shared.config_cache
-- Mem zones map
local mem_zone_map = {
[1] = "api_limit_zone_1", -- Rate limit 1 requests per second
[5] = "api_limit_zone_5", -- Rate limit 5 requests per second
[10] = "api_limit_zone_10", -- Rate limit 10 requests per second
}
-- Function to read theconfiguration file
local function read_config_file(path)
local file = io.open(path, "r")
if not file then
ngx.log(ngx.ERR, "Error: Unable to open configuration file: ", path)
return nil
end
local content = file:read("*all")
file:close()
local config = cjson.decode(content)
return config
end
-- Function to load configuration into memory cache
local function load_config()
local cached_config = shared_config_cache:get("rate_limits")
if cached_config then
return cjson.decode(cached_config)
end
local config = read_config_file(config_file_path)
if not config then
ngx.log(ngx.ERR, "Error reading the config file.")
return nil
end
-- Store in cache by 1 hour
shared_config_cache:set("rate_limits", cjson.encode(config), 3600)
return config
end
local config = load_config()
if not config then
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
return
end
-- Get API key from request header
local api_key = ngx.req.get_headers()["Authorization"]
if not api_key then
api_key = "DEFAULT"
end
-- Get the rate limit for the API key
local rate_limit = config[api_key] or config["DEFAULT"] -- Default rate limit if key is not in the config file
-- Get mem zone
local mem_zone_name = mem_zone_map[rate_limit] or "api_limit_zone_1"
ngx.log(ngx.DEBUG, "Creating limiter zone " .. mem_zone_name .. " for rate limit " .. rate_limit .. ".")
-- Create a limiter instance using the rate limit
local lim, err = limit_traffic.new(mem_zone_name, rate_limit, 1) -- 1 second window
if not lim then
ngx.log(ngx.ERR, "Failed to create limiter for rate limit " .. rate_limit .. ": ", err)
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
return
end
local key = "api_key:" .. api_key
local current, err = lim:incoming(key, true)
if not current then
if err == "rejected" then
ngx.status = ngx.HTTP_TOO_MANY_REQUESTS
ngx.say("Rate limit exceeded.")
ngx.exit(ngx.HTTP_TOO_MANY_REQUESTS)
else
ngx.log(ngx.ERR, "Failed to check rate limit: ", err)
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
end
ngx.log(ngx.DEBUG,"Request allowed, API Key: " .. api_key)