@colyseus/tools 0.15.0 → 0.15.1

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/post-deploy.js +55 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/tools",
3
- "version": "0.15.0",
3
+ "version": "0.15.1",
4
4
  "description": "Colyseus Tools for Production",
5
5
  "input": "./src/index.ts",
6
6
  "main": "./build/index.js",
@@ -47,5 +47,5 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  },
50
- "gitHead": "0ec5c17379936d74f4fa18ba68d16cbb7ed2c298"
50
+ "gitHead": "a64293e4e0cba0c7e21f62a9d4b18284a1fd45ea"
51
51
  }
package/post-deploy.js CHANGED
@@ -2,11 +2,13 @@
2
2
  const pm2 = require('pm2');
3
3
  const os = require('os');
4
4
  const fs = require('fs');
5
+ const { exec } = require('child_process');
5
6
 
6
7
  const opts = { env: process.env.NODE_ENV || "production" };
7
8
  const maxCPU = os.cpus().length;
8
9
 
9
- const NGINX_CONFIG_FILE = '/etc/nginx/colyseus_servers.conf';
10
+ const NGINX_SERVERS_CONFIG_FILE = '/etc/nginx/colyseus_servers.conf';
11
+ const NGINX_LIMITS_CONFIG_FILE = '/etc/nginx/colyseus_limits.conf';
10
12
 
11
13
  pm2.list(function(err, apps) {
12
14
  bailOnErr(err);
@@ -34,24 +36,30 @@ pm2.list(function(err, apps) {
34
36
  });
35
37
 
36
38
  function updateAndReloadNginx() {
39
+
37
40
  pm2.list(function(err, apps) {
38
41
  bailOnErr(err);
39
42
 
40
- const port = 2567;
41
- const addresses = [];
43
+ // update file descriptor limits systemwide + nginx worker connections
44
+ updateNOFileConfig(function(err) {
45
+ bailOnErr(err);
42
46
 
43
- apps.forEach(function(app) {
44
- addresses.push(`127.0.0.1:${ port + app.pm2_env.NODE_APP_INSTANCE }`);
45
- });
47
+ const port = 2567;
48
+ const addresses = [];
46
49
 
47
- fs.writeFileSync(NGINX_CONFIG_FILE, addresses.map(address => `server ${address};`).join("\n"));
50
+ apps.forEach(function(app) {
51
+ addresses.push(`127.0.0.1:${ port + app.pm2_env.NODE_APP_INSTANCE }`);
52
+ });
48
53
 
49
- // "pm2 save"
50
- pm2.dump(function(err, ret) {
51
- bailOnErr(err);
54
+ fs.writeFileSync(NGINX_SERVERS_CONFIG_FILE, addresses.map(address => `server ${address};`).join("\n"), bailOnErr);
55
+
56
+ // "pm2 save"
57
+ pm2.dump(function(err, ret) {
58
+ bailOnErr(err);
52
59
 
53
- // exit with success!
54
- process.exit();
60
+ // exit with success!
61
+ process.exit();
62
+ });
55
63
  });
56
64
  });
57
65
 
@@ -76,3 +84,38 @@ function bailOnErr(err) {
76
84
  process.exit(1);
77
85
  }
78
86
  }
87
+
88
+
89
+ function updateNOFileConfig(cb) {
90
+ // const numCPU = os.cpus().length;
91
+ const totalmemMB = os.totalmem() / 1024 / 1024;
92
+ const estimatedCCUPerGB = 4000;
93
+
94
+ const maxCCU = (totalmemMB / 1024) * estimatedCCUPerGB;
95
+ const systemMaxNOFileLimit = maxCCU * 4;
96
+ const nginxMaxNOFileLimit = maxCCU * 3; // 3x because of nginx -> proxy_pass -> node:port
97
+
98
+ // immediatelly apply new nofile limit
99
+ exec(`ulimit -n ${systemMaxNOFileLimit}`, bailOnErr);
100
+
101
+ // update "/etc/security/limits.conf" file.
102
+ fs.writeFileSync("/etc/security/limits.conf", `
103
+ * - nofile $NOFILE_LIMIT
104
+ `, bailOnErr);
105
+
106
+ if (fs.existsSync(NGINX_LIMITS_CONFIG_FILE)) {
107
+ fs.writeFileSync(NGINX_LIMITS_CONFIG_FILE, `
108
+ worker_rlimit_nofile ${nginxMaxNOFileLimit};
109
+
110
+ events {
111
+ worker_connections ${maxCCU};
112
+ # multi_accept on;
113
+ }
114
+ `, cb);
115
+ console.log("new nofile limit:", { maxCCU, systemMaxNOFileLimit, nginxMaxNOFileLimit });
116
+
117
+ } else {
118
+ console.warn(NGINX_LIMITS_CONFIG_FILE, "not found.");
119
+ }
120
+ }
121
+