Skip to content

Commit 4defb6a

Browse files
committed
nginx: fix non-expiring cache
The shared memory based caching was being done without any form of expiration. As as iniital step, hard-code the default 5m MAXAGE that is supposed to be configurable in all ephemeral-npm variants.
1 parent 82ce02f commit 4defb6a

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

ephemeral-npm.lua

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ local cjson = require "cjson.safe"
22

33
local _M = {}
44

5+
local function parseDuration(str)
6+
-- TODO: parse inputs like "1h", "2d", "10m" and return them as seconds
7+
return 5 * 60
8+
end
9+
510
function _M.init()
611
local npmConfig = ngx.shared.npmConfig
7-
local registry = os.getenv("npm_config_registry"):gsub("/+$", "")
8-
local pattern = registry:gsub("%.", "%%."):gsub("%-", "%%-")
12+
_M.registry = os.getenv("npm_config_registry"):gsub("/+$", "")
13+
_M.hostPattern = _M.registry:gsub("%.", "%%."):gsub("%-", "%%-")
14+
_M.MAXAGE = parseDuration(os.getenv("MAXAGE") or "5m")
915
-- escape . and - which have special meaning in Lua patterns
10-
npmConfig:set('npm_config_registry', registry)
11-
npmConfig:set('npm_upstream_pattern', pattern)
16+
npmConfig:set('npm_config_registry', _M.registry)
17+
npmConfig:set('npm_upstream_pattern', _M.hostPattern)
18+
npmConfig:set('MAXAGE', _M.MAXAGE)
1219
end
1320

1421
function _M.getPackage()
@@ -17,6 +24,7 @@ function _M.getPackage()
1724
local body = meta:get(uri)
1825
-- yep, our own shared memory cache implementation :-/
1926
if body == nil then
27+
ngx.var.ephemeralCacheStatus = 'MISS'
2028
local res = ngx.location.capture('/-@-' .. uri,
2129
{ copy_all_vars = true })
2230
body = res.body
@@ -27,7 +35,9 @@ function _M.getPackage()
2735
-- next time
2836
return ngx.redirect(uri, ngx.HTTP_MOVED_TEMPORARILY)
2937
end
30-
meta:set(uri, body)
38+
meta:set(uri, body, _M.MAXAGE)
39+
else
40+
ngx.var.ephemeralCacheStatus = 'HIT'
3141
end
3242
ngx.header["Content-Length"] = #body
3343
ngx.print(body)

nginx.conf

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ http {
2222
proxy_cache_lock on;
2323
log_format upstreamlog '$remote_addr - $remote_user [$time_local] '
2424
'"$request" $status $body_bytes_sent '
25-
'"$http_referer" "$http_user_agent" "$upstream_cache_status"';
25+
'"$http_referer" "$http_user_agent" "$ephemeralCacheStatus"';
2626
access_log logs/access.log upstreamlog;
2727

2828
lua_shared_dict npmConfig 32k;
@@ -44,25 +44,26 @@ http {
4444
try_files $uri @fetch-tgz;
4545
}
4646
location / {
47+
set $ephemeralCacheStatus '-';
48+
# We can't do caching at the proxy level because we can't easily
49+
# parameterize cache lifetime for proxy_cache. Instead, we do
50+
# the caching ourselves in Lua using a shared dict.
4751
content_by_lua_block {
4852
local ephemeralNPM = require "ephemeral-npm"
4953
ephemeralNPM.getPackage()
5054
}
5155
}
56+
# This is just a straight though, uncaching proxy to the upstream
57+
# registry.
58+
# We have to use a path prefix instead of an internal @name because
59+
# we need to be able to make sub-requests using ngx.location.capture()
5260
location /-@- {
5361
internal;
5462
rewrite /-@-/(.+) /$1 break;
5563
resolver 127.0.0.1 ipv6=off;
5664
proxy_pass $npm_config_registry;
5765
proxy_buffers 32 1m;
58-
proxy_cache NPM;
59-
# need nginx-1.11.10 for this, which isn't in openresty yet:
60-
# proxy_cache_background_update on;
61-
proxy_cache_revalidate on;
62-
# let our metadata be 20 minutes old, at most
63-
proxy_cache_valid 200 20m;
64-
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
65-
# need to disable encoding in order to be able to filter the response body
66+
# need to disable encoding in order to be able to process it locally
6667
proxy_set_header Accept-Encoding "";
6768
# modifying the response body will change the length
6869
header_filter_by_lua_block {

0 commit comments

Comments
 (0)