@colyseus/tools 0.15.0 → 0.15.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/tools",
3
- "version": "0.15.0",
3
+ "version": "0.15.2",
4
4
  "description": "Colyseus Tools for Production",
5
5
  "input": "./src/index.ts",
6
6
  "main": "./build/index.js",
@@ -10,6 +10,7 @@
10
10
  "start": "ts-node-dev example/app.ts"
11
11
  },
12
12
  "bin": {
13
+ "colyseus-system-boot": "system-boot.js",
13
14
  "colyseus-post-deploy": "post-deploy.js"
14
15
  },
15
16
  "repository": {
@@ -47,5 +48,5 @@
47
48
  "publishConfig": {
48
49
  "access": "public"
49
50
  },
50
- "gitHead": "0ec5c17379936d74f4fa18ba68d16cbb7ed2c298"
51
+ "gitHead": "810b5a4f8f849efb4d06226e54e8cad7aa72927a"
51
52
  }
package/post-deploy.js CHANGED
@@ -6,7 +6,7 @@ const fs = require('fs');
6
6
  const opts = { env: process.env.NODE_ENV || "production" };
7
7
  const maxCPU = os.cpus().length;
8
8
 
9
- const NGINX_CONFIG_FILE = '/etc/nginx/colyseus_servers.conf';
9
+ const NGINX_SERVERS_CONFIG_FILE = '/etc/nginx/colyseus_servers.conf';
10
10
 
11
11
  pm2.list(function(err, apps) {
12
12
  bailOnErr(err);
@@ -44,7 +44,7 @@ function updateAndReloadNginx() {
44
44
  addresses.push(`127.0.0.1:${ port + app.pm2_env.NODE_APP_INSTANCE }`);
45
45
  });
46
46
 
47
- fs.writeFileSync(NGINX_CONFIG_FILE, addresses.map(address => `server ${address};`).join("\n"));
47
+ fs.writeFileSync(NGINX_SERVERS_CONFIG_FILE, addresses.map(address => `server ${address};`).join("\n"), bailOnErr);
48
48
 
49
49
  // "pm2 save"
50
50
  pm2.dump(function(err, ret) {
@@ -76,3 +76,4 @@ function bailOnErr(err) {
76
76
  process.exit(1);
77
77
  }
78
78
  }
79
+
package/system-boot.js ADDED
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env node
2
+
3
+ const os = require('os');
4
+ const fs = require('fs');
5
+ const { exec } = require('child_process');
6
+
7
+ const NGINX_LIMITS_CONFIG_FILE = '/etc/nginx/colyseus_limits.conf';
8
+ const LIMITS_CONF_FILE = "/etc/security/limits.conf";
9
+
10
+ // update file descriptor limits systemwide + nginx worker connections
11
+
12
+ function bailOnErr(err) {
13
+ if (err) {
14
+ console.error(err);
15
+
16
+ // exit with error!
17
+ process.exit(1);
18
+ }
19
+ }
20
+
21
+ function updateNOFileConfig(cb) {
22
+ // const numCPU = os.cpus().length;
23
+ const totalmemMB = os.totalmem() / 1024 / 1024;
24
+ const estimatedCCUPerGB = 4000;
25
+
26
+ const maxCCU = (totalmemMB / 1024) * estimatedCCUPerGB;
27
+ const systemMaxNOFileLimit = maxCCU * 4;
28
+ const nginxMaxNOFileLimit = maxCCU * 3; // 3x because of nginx -> proxy_pass -> node:port
29
+
30
+ // immediatelly apply new nofile limit
31
+ exec(`ulimit -n ${systemMaxNOFileLimit}`, bailOnErr);
32
+
33
+ // update "/etc/security/limits.conf" file.
34
+ fs.writeFileSync(LIMITS_CONF_FILE, `
35
+ * - nofile $NOFILE_LIMIT
36
+ `, bailOnErr);
37
+
38
+ if (fs.existsSync(NGINX_LIMITS_CONFIG_FILE)) {
39
+ fs.writeFileSync(NGINX_LIMITS_CONFIG_FILE, `
40
+ worker_rlimit_nofile ${nginxMaxNOFileLimit};
41
+
42
+ events {
43
+ worker_connections ${maxCCU};
44
+ # multi_accept on;
45
+ }
46
+ `, cb);
47
+ console.log("new nofile limit:", { maxCCU, systemMaxNOFileLimit, nginxMaxNOFileLimit });
48
+
49
+ } else {
50
+ console.warn(NGINX_LIMITS_CONFIG_FILE, "not found.");
51
+ }
52
+ }
53
+
54
+
55
+ updateNOFileConfig(bailOnErr);