@abtnode/router-provider 1.8.65-beta-81d3340c → 1.8.65-beta-f7af64a4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ proxy_cache_key $scheme$proxy_host$request_uri;
2
+ proxy_cache_bypass $cookie_nocache $arg_nocache;
3
+ proxy_cache_lock on;
4
+ proxy_cache_revalidate on;
5
+
6
+ add_header X-Cache-Status $upstream_cache_status;
@@ -18,7 +18,9 @@ const {
18
18
  DEFAULT_ADMIN_PATH,
19
19
  SLOT_FOR_IP_DNS_SITE,
20
20
  WELLKNOWN_SERVICE_PATH_PREFIX,
21
+ USER_AVATAR_PATH_PREFIX,
21
22
  LOG_RETAIN_IN_DAYS,
23
+ ROUTER_CACHE_GROUPS,
22
24
  } = require('@abtnode/constant');
23
25
  const md5 = require('@abtnode/util/lib/md5');
24
26
 
@@ -101,6 +103,7 @@ class NginxProvider extends BaseProvider {
101
103
  this.errorLog = path.join(this.logDir, 'error.log');
102
104
  this.tmpDir = path.join(this.configDir, 'tmp');
103
105
  this.certDir = path.join(this.configDir, 'certs');
106
+ this.cacheDir = path.join(this.configDir, 'cache');
104
107
  this.includesDir = path.join(this.configDir, 'includes');
105
108
  this.wwwDir = path.join(this.configDir, 'www');
106
109
 
@@ -120,7 +123,7 @@ class NginxProvider extends BaseProvider {
120
123
  });
121
124
 
122
125
  // ensure directories
123
- [this.configDir, this.logDir, this.tmpDir, this.certDir].forEach((dir) => {
126
+ [this.configDir, this.logDir, this.cacheDir, this.tmpDir, this.certDir].forEach((dir) => {
124
127
  if (!fs.existsSync(dir)) {
125
128
  fs.mkdirSync(dir);
126
129
  }
@@ -143,6 +146,7 @@ class NginxProvider extends BaseProvider {
143
146
  return getMainTemplate({
144
147
  logDir: this.getRelativeConfigDir(formatBackSlash(this.logDir)),
145
148
  tmpDir: this.getRelativeConfigDir(formatBackSlash(this.tmpDir)),
149
+ cacheDir: this.getRelativeConfigDir(formatBackSlash(this.cacheDir)),
146
150
  workerProcess: this.getWorkerProcess(),
147
151
  nginxLoadModules: getNginxLoadModuleDirectives(REQUIRED_MODULES, this.readNginxConfigParams()).join(os.EOL),
148
152
  });
@@ -195,8 +199,6 @@ class NginxProvider extends BaseProvider {
195
199
  this.addGlobalReqLimit(conf.nginx.http, requestLimit);
196
200
  }
197
201
 
198
- logger.debug('routing sites:', sites);
199
-
200
202
  const allRules = sites.reduce((acc, site) => {
201
203
  acc.push(...(site.rules || []));
202
204
  return acc;
@@ -451,6 +453,7 @@ class NginxProvider extends BaseProvider {
451
453
  ruleId,
452
454
  type,
453
455
  commonHeaders,
456
+ cacheGroup,
454
457
  }) {
455
458
  server._add('location', concatPath(prefix, suffix));
456
459
 
@@ -469,18 +472,22 @@ class NginxProvider extends BaseProvider {
469
472
  location._add('proxy_set_header', `X-Blocklet-Component-Id "${componentId}"`);
470
473
  }
471
474
 
475
+ location._add('include', 'includes/proxy');
476
+
472
477
  // kill cache
473
478
  // TODO: 这个放在管理端的 Gateway 配置中
474
479
  if (this.cacheDisabled) {
475
480
  location._add('expires', '-1');
481
+ } else if (ROUTER_CACHE_GROUPS[cacheGroup]) {
482
+ location._add('proxy_cache', cacheGroup);
483
+ location._add('proxy_cache_valid', `200 ${ROUTER_CACHE_GROUPS[cacheGroup].period}`);
484
+ location._add('include', 'includes/cache');
476
485
  }
477
486
 
478
- location._add('include', 'includes/proxy');
479
-
480
487
  // Redirect blocklet traffic
481
488
  if (type === ROUTING_RULE_TYPES.BLOCKLET) {
482
- // FIXME logic related to server gateway should not in provider
483
- const rewritePathPrefix = prefix.replace(WELLKNOWN_SERVICE_PATH_PREFIX, '') || '/';
489
+ // FIXME: logic related to server gateway should not in provider
490
+ const rewritePathPrefix = prefix.replace(WELLKNOWN_SERVICE_PATH_PREFIX, '').replace(USER_AVATAR_PATH_PREFIX, '');
484
491
 
485
492
  // Add header
486
493
 
package/lib/nginx/util.js CHANGED
@@ -8,7 +8,7 @@ const { NginxConfFile } = require('nginx-conf');
8
8
  const shelljs = require('shelljs');
9
9
  const findProcess = require('find-process');
10
10
  const moment = require('moment');
11
- const { MAX_UPLOAD_FILE_SIZE, MAX_NGINX_WORKER_CONNECTIONS } = require('@abtnode/constant');
11
+ const { MAX_UPLOAD_FILE_SIZE, MAX_NGINX_WORKER_CONNECTIONS, ROUTER_CACHE_GROUPS } = require('@abtnode/constant');
12
12
  const formatBackSlash = require('@abtnode/util/lib/format-back-slash');
13
13
  const { deleteOldLogfiles } = require('@abtnode/logger');
14
14
 
@@ -150,6 +150,7 @@ const getWorkerConnectionCount = (maxWorkerConnections, workerProcess) => {
150
150
  const getMainTemplate = ({
151
151
  logDir,
152
152
  tmpDir,
153
+ cacheDir,
153
154
  nginxLoadModules = '',
154
155
  workerProcess,
155
156
  maxBodySize = CLIENT_MAX_BODY_SIZE,
@@ -189,8 +190,22 @@ http {
189
190
  include includes/params;
190
191
  include includes/mime.types;
191
192
  include includes/compression;
193
+
194
+ ${Object.keys(ROUTER_CACHE_GROUPS)
195
+ .map((x) => getCachePathDirective(cacheDir, x))
196
+ .join('\n')}
192
197
  }
193
198
  `;
199
+
200
+ const getCachePathDirective = (cacheDir, group) => {
201
+ if (!ROUTER_CACHE_GROUPS[group]) {
202
+ return '';
203
+ }
204
+
205
+ const config = ROUTER_CACHE_GROUPS[group];
206
+ return `proxy_cache_path ${cacheDir}/${group} levels=1:2 keys_zone=${group}:${config.minSize} inactive=${config.period} max_size=${config.maxSize};`;
207
+ };
208
+
194
209
  const getNginxStatus = async (configPath) => {
195
210
  const list = await findProcess('name', /nginx:/);
196
211
  const result = {
@@ -299,6 +314,7 @@ module.exports = {
299
314
  rotateNginxLogFile,
300
315
  getUserGroup,
301
316
  getWorkerConnectionCount,
317
+ getCachePathDirective,
302
318
  getUpstreamName,
303
319
  joinNginxPath,
304
320
  };
package/lib/util.js CHANGED
@@ -86,6 +86,7 @@ const formatRoutingTable = (routingTable, onRule) => {
86
86
  } else {
87
87
  rule.port = +x.to.port;
88
88
  rule.did = x.to.did;
89
+ rule.cacheGroup = x.to.cacheGroup;
89
90
  rule.componentId = x.to.componentId;
90
91
  rule.target = trimEndSlash(normalizePathPrefix(x.to.target || '/'));
91
92
  rule.services = x.services || [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abtnode/router-provider",
3
- "version": "1.8.65-beta-81d3340c",
3
+ "version": "1.8.65-beta-f7af64a4",
4
4
  "description": "Routing engine implementations for abt node",
5
5
  "author": "polunzh <polunzh@gmail.com>",
6
6
  "homepage": "https://github.com/ArcBlock/blocklet-server#readme",
@@ -32,10 +32,10 @@
32
32
  "url": "https://github.com/ArcBlock/blocklet-server/issues"
33
33
  },
34
34
  "dependencies": {
35
- "@abtnode/constant": "1.8.65-beta-81d3340c",
36
- "@abtnode/logger": "1.8.65-beta-81d3340c",
37
- "@abtnode/router-templates": "1.8.65-beta-81d3340c",
38
- "@abtnode/util": "1.8.65-beta-81d3340c",
35
+ "@abtnode/constant": "1.8.65-beta-f7af64a4",
36
+ "@abtnode/logger": "1.8.65-beta-f7af64a4",
37
+ "@abtnode/router-templates": "1.8.65-beta-f7af64a4",
38
+ "@abtnode/util": "1.8.65-beta-f7af64a4",
39
39
  "axios": "^0.27.2",
40
40
  "debug": "^4.3.4",
41
41
  "find-process": "^1.4.7",
@@ -62,5 +62,5 @@
62
62
  "fs-extra": "^10.1.0",
63
63
  "needle": "^3.1.0"
64
64
  },
65
- "gitHead": "5a904b9192febd1d09a759d929ca4de639a50cd1"
65
+ "gitHead": "97607d6e12bf8508ac29ab6546110c4ae3b31a82"
66
66
  }