@colyseus/tools 0.15.1 → 0.15.3

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.1",
3
+ "version": "0.15.3",
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": "a64293e4e0cba0c7e21f62a9d4b18284a1fd45ea"
51
+ "gitHead": "3d22fe71a30b78158450f79d091a24ef80494d2c"
51
52
  }
package/post-deploy.js CHANGED
@@ -2,13 +2,11 @@
2
2
  const pm2 = require('pm2');
3
3
  const os = require('os');
4
4
  const fs = require('fs');
5
- const { exec } = require('child_process');
6
5
 
7
6
  const opts = { env: process.env.NODE_ENV || "production" };
8
7
  const maxCPU = os.cpus().length;
9
8
 
10
9
  const NGINX_SERVERS_CONFIG_FILE = '/etc/nginx/colyseus_servers.conf';
11
- const NGINX_LIMITS_CONFIG_FILE = '/etc/nginx/colyseus_limits.conf';
12
10
 
13
11
  pm2.list(function(err, apps) {
14
12
  bailOnErr(err);
@@ -36,30 +34,24 @@ pm2.list(function(err, apps) {
36
34
  });
37
35
 
38
36
  function updateAndReloadNginx() {
39
-
40
37
  pm2.list(function(err, apps) {
41
38
  bailOnErr(err);
42
39
 
43
- // update file descriptor limits systemwide + nginx worker connections
44
- updateNOFileConfig(function(err) {
45
- bailOnErr(err);
46
-
47
- const port = 2567;
48
- const addresses = [];
40
+ const port = 2567;
41
+ const addresses = [];
49
42
 
50
- apps.forEach(function(app) {
51
- addresses.push(`127.0.0.1:${ port + app.pm2_env.NODE_APP_INSTANCE }`);
52
- });
43
+ apps.forEach(function(app) {
44
+ addresses.push(`127.0.0.1:${ port + app.pm2_env.NODE_APP_INSTANCE }`);
45
+ });
53
46
 
54
- fs.writeFileSync(NGINX_SERVERS_CONFIG_FILE, addresses.map(address => `server ${address};`).join("\n"), bailOnErr);
47
+ fs.writeFileSync(NGINX_SERVERS_CONFIG_FILE, addresses.map(address => `server ${address};`).join("\n"), bailOnErr);
55
48
 
56
- // "pm2 save"
57
- pm2.dump(function(err, ret) {
58
- bailOnErr(err);
49
+ // "pm2 save"
50
+ pm2.dump(function(err, ret) {
51
+ bailOnErr(err);
59
52
 
60
- // exit with success!
61
- process.exit();
62
- });
53
+ // exit with success!
54
+ process.exit();
63
55
  });
64
56
  });
65
57
 
@@ -85,37 +77,3 @@ function bailOnErr(err) {
85
77
  }
86
78
  }
87
79
 
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
-
package/system-boot.js ADDED
@@ -0,0 +1,56 @@
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 = Math.floor((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
+ // (apparently this has no effect)
32
+ exec(`ulimit -n ${systemMaxNOFileLimit}`, bailOnErr);
33
+
34
+ // update "/etc/security/limits.conf" file.
35
+ fs.writeFileSync(LIMITS_CONF_FILE, `
36
+ * - nofile ${systemMaxNOFileLimit}
37
+ `, bailOnErr);
38
+
39
+ if (fs.existsSync(NGINX_LIMITS_CONFIG_FILE)) {
40
+ fs.writeFileSync(NGINX_LIMITS_CONFIG_FILE, `
41
+ worker_rlimit_nofile ${nginxMaxNOFileLimit};
42
+
43
+ events {
44
+ worker_connections ${maxCCU};
45
+ # multi_accept on;
46
+ }
47
+ `, cb);
48
+ console.log("new nofile limit:", { maxCCU, systemMaxNOFileLimit, nginxMaxNOFileLimit });
49
+
50
+ } else {
51
+ console.warn(NGINX_LIMITS_CONFIG_FILE, "not found.");
52
+ }
53
+ }
54
+
55
+
56
+ updateNOFileConfig(bailOnErr);