Skip to content
This repository was archived by the owner on Apr 14, 2025. It is now read-only.

Commit afc8414

Browse files
authored
Ajvb/v0.1.8 (#24)
* reputation cache improvements * Up default cache ttl to 60 seconds * Up default cache size to 5000 items * Add metric for cache hits * Only set reputation in cache when it's new * bump version * Set HTTP keepalive header * Added iprepd.cache_hit to README * add separate error cache ttl
1 parent 69a2131 commit afc8414

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ violations for your environment.
8181
-- Optional parameters:
8282
-- url - The base URL to iprepd (defaults to "http://localhost:8080/")
8383
-- timeout - The timeout for making requests to iprepd in milliseconds (defaults to 10)
84-
-- cache_ttl - The iprepd response cache ttl in seconds (defaults to 30)
85-
-- cache_buffer_count - Max number of entries allowed in the cache. (defaults to 200)
84+
-- cache_ttl - The iprepd response cache ttl in seconds (defaults to 60)
85+
-- cache_buffer_count - Max number of entries allowed in the cache. (defaults to 5000)
8686
-- cache_errors - Enables (1) or disables (0) caching errors. Caching errors is a good
8787
-- idea in production, as it can reduce the average additional latency
8888
-- caused by this module if anything goes wrong with the underlying
8989
-- infrastructure. (defaults to disabled)
90+
-- cache_errors_ttl - The iprepd response cache ttl for error responses (not 200 or 404) in seconds (defaults to 10)
9091
-- statsd_host - Host of statsd collector. Setting this will enable statsd metrics collection
9192
-- statsd_port - Port of statsd collector. (defaults to 8125)
9293
-- statsd_max_buffer_count - Max number of metrics in buffer before metrics should be submitted
@@ -107,6 +108,7 @@ client = require("resty.iprepd").new({
107108
cache_ttl = 30,
108109
cache_buffer_count = 1000,
109110
cache_errors = 1,
111+
cache_errors_ttl = 10,
110112
statsd_host = "127.0.0.1",
111113
statsd_port = 8125,
112114
statsd_max_buffer_count = 100,
@@ -127,6 +129,7 @@ client = require("resty.iprepd").new({
127129
| iprepd.status.rejected | count | The request was blocked (won’t be sent if `blocking_mode` is disabled). |
128130
| iprepd.status.accepted | count | The request was accepted. The reputation can still be below the threshold if `blocking_mode` is disabled.
129131
| iprepd.get_reputation | count | Request to iprepd |
132+
| iprepd.cache_hit | count | Got reputation from internal cache |
130133
| iprepd.err.timeout | count | Request to iprepd timed out |
131134
| iprepd.err.500 | count | Got a 500 response from iprepd |
132135
| iprepd.err.401 | count | Got a 401 response from iprepd, usually means the API key in use is invalid or being sent incorrectly by nginx. |

dist.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = iprepd-nginx
22
abstract = iprepd openresty module
33
author = AJ Bahnken (ajvb)
4-
version = 0.1.7
4+
version = 0.1.8
55
is_original = yes
66
license = mozilla2
77
lib_dir = lib

lib/resty/iprepd.lua

+13-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function _M.new(options)
1919
iprepd_url = iprepd_url:sub(1, -2)
2020
end
2121

22-
local cache_buffer_count = options.cache_buffer_count or 200
22+
local cache_buffer_count = options.cache_buffer_count or 5000
2323

2424
local iprepd_threshold = options.threshold or fatal_error('Need to pass in a threshold')
2525
local iprepd_api_key = options.api_key or fatal_error('Need to pass in an api_key')
@@ -44,12 +44,14 @@ function _M.new(options)
4444
url = iprepd_url,
4545
timeout = options.timeout or 10,
4646
threshold = iprepd_threshold,
47-
api_key_hdr = {
47+
iprepd_hdrs = {
4848
['Authorization'] = string.format('APIKey %s', iprepd_api_key),
49+
['Connection'] = 'keep-alive',
4950
},
5051
cache = cache,
51-
cache_ttl = options.cache_ttl or 30,
52+
cache_ttl = options.cache_ttl or 60,
5253
cache_errors = options.cache_errors or 0,
54+
cache_errors_ttl = options.cache_errors_ttl or 10,
5355
statsd = statsd_client,
5456
statsd_host = options.statsd_host,
5557
statsd_port = options.statsd_port or 8125,
@@ -112,7 +114,7 @@ function _M.get_reputation(self, ip)
112114
httpc:set_timeout(self.timeout)
113115
local resp, err = httpc:request_uri(string.format("%s/%s", self.url, ip), {
114116
method = "GET",
115-
headers = self.api_key_hdr,
117+
headers = self.iprepd_hdrs,
116118
})
117119
self.statsd.incr("iprepd.get_reputation")
118120
if err then
@@ -147,12 +149,16 @@ function _M.get_reputation(self, ip)
147149
if self.cache_errors == 1 then
148150
reputation = 100
149151
self:debug_log(string.format("cache_errors is enabled, setting reputation of %s to 100 within the cache", ip))
152+
self.cache:set(ip, reputation, self.cache_errors_ttl)
153+
return reputation
150154
end
151155
end
152-
end
153156

154-
if reputation and reputation >= 0 and reputation <= 100 then
155-
self.cache:set(ip, reputation, self.cache_ttl)
157+
if reputation and reputation >= 0 and reputation <= 100 then
158+
self.cache:set(ip, reputation, self.cache_ttl)
159+
end
160+
else
161+
self.statsd.incr("iprepd.cache_hit")
156162
end
157163

158164
return reputation

0 commit comments

Comments
 (0)