move cache to lua to minimise number of requests

This commit is contained in:
Karol Wypchlo 2021-10-26 13:01:15 +02:00
parent 9737f22fed
commit 81a1338f00
No known key found for this signature in database
GPG Key ID: C92C016317A964D0
5 changed files with 28 additions and 38 deletions

View File

@ -1,3 +1,5 @@
lua_shared_dict dnslink 10m;
server { server {
listen 80 default_server; listen 80 default_server;
listen [::]:80 default_server; listen [::]:80 default_server;

View File

@ -5,6 +5,10 @@ location / {
set $path $uri; set $path $uri;
rewrite_by_lua_block { rewrite_by_lua_block {
local cache = ngx.shared.dnslink
local cache_value = cache:get(ngx.var.host)
if cache_value == nil then
local httpc = require("resty.http").new() local httpc = require("resty.http").new()
-- 10.10.10.55 points to dnslink-api service (alias not available when using resty-http) -- 10.10.10.55 points to dnslink-api service (alias not available when using resty-http)
@ -27,6 +31,12 @@ location / {
ngx.var.skylink = res.body ngx.var.skylink = res.body
end end
local cache_ttl = 300 -- 5 minutes cache expire time
cache:set(ngx.var.host, ngx.var.skylink, cache_ttl)
else
ngx.var.skylink = cache_value
end
ngx.var.skylink_v1 = ngx.var.skylink ngx.var.skylink_v1 = ngx.var.skylink
ngx.var.skylink_v2 = ngx.var.skylink ngx.var.skylink_v2 = ngx.var.skylink
} }

View File

@ -5,8 +5,7 @@
"license": "SEE LICENSE IN LICENSE.md", "license": "SEE LICENSE IN LICENSE.md",
"dependencies": { "dependencies": {
"express": "^4.17.1", "express": "^4.17.1",
"is-valid-domain": "^0.1.2", "is-valid-domain": "^0.1.2"
"node-cache": "^5.1.2"
}, },
"devDependencies": { "devDependencies": {
"prettier": "^2.4.1" "prettier": "^2.4.1"

View File

@ -1,14 +1,11 @@
const dns = require("dns"); const dns = require("dns");
const express = require("express"); const express = require("express");
const NodeCache = require("node-cache");
const isValidDomain = require("is-valid-domain"); const isValidDomain = require("is-valid-domain");
const host = process.env.DNSLINK_API_HOSTNAME || "0.0.0.0"; const host = process.env.DNSLINK_API_HOSTNAME || "0.0.0.0";
const port = Number(process.env.DNSLINK_API_PORT) || 3100; const port = Number(process.env.DNSLINK_API_PORT) || 3100;
const cacheTTL = Number(process.env.DNSLINK_API_CACHE_TTL) || 300; // default to 5 minutes
const server = express(); const server = express();
const cache = new NodeCache({ stdTTL: cacheTTL });
const dnslinkNamespace = "skynet-ns"; const dnslinkNamespace = "skynet-ns";
const dnslinkRegExp = new RegExp(`^dnslink=/${dnslinkNamespace}/.+$`); const dnslinkRegExp = new RegExp(`^dnslink=/${dnslinkNamespace}/.+$`);
@ -23,10 +20,6 @@ server.get("/dnslink/:name", async (req, res) => {
return failure(`"${req.params.name}" is not a valid domain`); return failure(`"${req.params.name}" is not a valid domain`);
} }
if (cache.has(req.params.name)) {
return success(cache.get(req.params.name));
}
const lookup = `_dnslink.${req.params.name}`; const lookup = `_dnslink.${req.params.name}`;
dns.resolveTxt(lookup, (error, records) => { dns.resolveTxt(lookup, (error, records) => {
@ -65,8 +58,6 @@ server.get("/dnslink/:name", async (req, res) => {
const skylink = matchSkylink[1]; const skylink = matchSkylink[1];
cache.set(req.params.name, skylink);
console.log(`${req.params.name} => ${skylink}`); console.log(`${req.params.name} => ${skylink}`);
return success(skylink); return success(skylink);

View File

@ -36,11 +36,6 @@ bytes@3.1.0:
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6" resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6"
integrity sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg== integrity sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==
clone@2.x:
version "2.1.2"
resolved "https://registry.yarnpkg.com/clone/-/clone-2.1.2.tgz#1b7f4b9f591f1e8f83670401600345a02887435f"
integrity sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18=
content-disposition@0.5.3: content-disposition@0.5.3:
version "0.5.3" version "0.5.3"
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.3.tgz#e130caf7e7279087c5616c2007d0485698984fbd" resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.3.tgz#e130caf7e7279087c5616c2007d0485698984fbd"
@ -257,13 +252,6 @@ negotiator@0.6.2:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.2.tgz#feacf7ccf525a77ae9634436a64883ffeca346fb" resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.2.tgz#feacf7ccf525a77ae9634436a64883ffeca346fb"
integrity sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw== integrity sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==
node-cache@^5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/node-cache/-/node-cache-5.1.2.tgz#f264dc2ccad0a780e76253a694e9fd0ed19c398d"
integrity sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==
dependencies:
clone "2.x"
on-finished@~2.3.0: on-finished@~2.3.0:
version "2.3.0" version "2.3.0"
resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.3.0.tgz#20f1336481b083cd75337992a16971aa2d906947" resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.3.0.tgz#20f1336481b083cd75337992a16971aa2d906947"