@colyseus/tools 0.16.8 → 0.16.9

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/system-boot.js +31 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/tools",
3
- "version": "0.16.8",
3
+ "version": "0.16.9",
4
4
  "description": "Simplify the development and production settings for your Colyseus project.",
5
5
  "input": "./src/index.ts",
6
6
  "main": "./build/index.js",
@@ -50,7 +50,7 @@
50
50
  "@types/cors": "^2.8.10",
51
51
  "@types/dotenv": "^8.2.0",
52
52
  "uwebsockets-express": "^1.1.10",
53
- "@colyseus/core": "^0.16.17",
53
+ "@colyseus/core": "^0.16.18",
54
54
  "@colyseus/ws-transport": "^0.16.5",
55
55
  "@colyseus/uwebsockets-transport": "^0.16.9"
56
56
  },
package/system-boot.js CHANGED
@@ -6,57 +6,63 @@ const { execSync } = require('child_process');
6
6
 
7
7
  const NGINX_LIMITS_CONFIG_FILE = '/etc/nginx/colyseus_limits.conf';
8
8
  const LIMITS_CONF_FILE = '/etc/security/limits.d/colyseus.conf';
9
- const SYSCTL_FILE = '/etc/sysctl.d/local.conf'
9
+ const SYSCTL_FILE = '/etc/sysctl.d/local.conf';
10
10
 
11
- const MAX_CCU_PER_CPU = 8000;
11
+ const MAX_CCU_PER_CPU = 5000;
12
12
 
13
13
  /**
14
14
  * System-wide limits configuration for high-CCU environments
15
15
  * @param {Object} options
16
16
  * @param {number} options.maxCCUPerCPU - Maximum concurrent users per CPU core
17
- * @param {number} options.connectionsMultiplier - Multiplier for worker connections (default: 3)
18
- * @param {number} options.fileDescriptorMultiplier - Multiplier for max file descriptors (default: 4)
17
+ * @param {number} options.connectionsMultiplier - Multiplier for worker connections (default: 2)
18
+ * @param {number} options.fileDescriptorMultiplier - Multiplier for max file descriptors (default: 6)
19
19
  */
20
20
  function configureSystemLimits(options = {}) {
21
21
  const {
22
- connectionsMultiplier = 3,
23
- fileDescriptorMultiplier = 4
22
+ connectionsMultiplier = 2,
23
+ fileDescriptorMultiplier = 6
24
24
  } = options;
25
25
 
26
26
  const numCPU = os.cpus().length;
27
27
  const maxCCU = numCPU * MAX_CCU_PER_CPU;
28
28
 
29
29
  // Calculate limits
30
- const workerConnections = maxCCU * connectionsMultiplier;
31
- const maxFileDescriptors = maxCCU * fileDescriptorMultiplier;
30
+ const workerConnections = Math.min(maxCCU * connectionsMultiplier, 65535); // Cap at max_socket_backlog (65535)
31
+ let maxFileDescriptors = maxCCU * fileDescriptorMultiplier;
32
+ const workerRlimitNofile = Math.ceil(workerConnections * 3.5); // Assume 3.5 file descriptors per connection for safety
32
33
 
33
- // Nginx-specific calculations
34
- const workerRlimitNofile = (workerConnections / numCPU) * 2;
34
+ // Calculate total file descriptors needed
35
+ const totalFileDescriptors = workerRlimitNofile * numCPU;
35
36
 
36
- // Validation checks
37
- if (workerConnections > 65535) {
38
- console.warn(`Warning: worker_connections (${workerConnections}) exceeds typical max_socket_backlog (65535)`);
37
+ if (totalFileDescriptors > maxFileDescriptors) {
38
+ console.warn(`Warning: Total file descriptors (${totalFileDescriptors}) exceeds maxFileDescriptors (${maxFileDescriptors}). Increasing maxFileDescriptors.`);
39
+ maxFileDescriptors = totalFileDescriptors * 1.5; // 50% safety margin
39
40
  }
40
41
 
41
- if (maxFileDescriptors > 1048576) {
42
- console.warn(`Warning: Very high file descriptor limit (${maxFileDescriptors}). Verify system capabilities.`);
42
+ // Check memory (rough estimate: 150 KB per connection)
43
+ const estimatedMemoryMB = (workerConnections * numCPU * 150) / 1024;
44
+ const totalMemoryMB = Number(execSync('free -m | awk \'/Mem:/ {print $2}\'').toString().trim());
45
+ if (estimatedMemoryMB > totalMemoryMB * 0.8) {
46
+ console.warn(`Warning: Estimated memory usage (${estimatedMemoryMB} MB) exceeds 80% of available memory (${totalMemoryMB} MB). Consider reducing MAX_CCU_PER_CPU.`);
43
47
  }
44
48
 
45
49
  if (process.argv.includes('--dry-run')) {
46
50
  console.log({
51
+ numCPU,
47
52
  maxCCU,
48
53
  workerConnections,
49
54
  maxFileDescriptors,
50
- workerRlimitNofile
51
- })
55
+ workerRlimitNofile,
56
+ totalFileDescriptors,
57
+ estimatedMemoryMB
58
+ });
52
59
  process.exit();
53
60
  }
54
61
 
55
62
  // Configuration updates
56
63
  try {
57
64
  // Update Nginx limits
58
- if (fs.existsSync(NGINX_LIMITS_CONFIG_FILE)) {
59
- fs.writeFileSync(NGINX_LIMITS_CONFIG_FILE, `
65
+ fs.writeFileSync(NGINX_LIMITS_CONFIG_FILE, `
60
66
  worker_rlimit_nofile ${workerRlimitNofile};
61
67
  events {
62
68
  use epoll;
@@ -64,7 +70,6 @@ events {
64
70
  multi_accept on;
65
71
  }
66
72
  `);
67
- }
68
73
 
69
74
  // Update system-wide limits
70
75
  fs.writeFileSync(LIMITS_CONF_FILE, `
@@ -74,7 +79,7 @@ nginx soft nofile ${maxFileDescriptors}
74
79
  nginx hard nofile ${maxFileDescriptors}
75
80
  `);
76
81
 
77
- // Update sysctl with doubled file-max for safety margin
82
+ // Update sysctl
78
83
  fs.writeFileSync(SYSCTL_FILE, `
79
84
  # System-wide file descriptor limit
80
85
  fs.file-max = ${maxFileDescriptors * 2}
@@ -99,7 +104,11 @@ net.ipv4.tcp_keepalive_probes = 3
99
104
  `);
100
105
 
101
106
  // Apply sysctl changes
102
- execSync("sysctl -p", { stdio: 'inherit' });
107
+ execSync('sysctl -p', { stdio: 'inherit' });
108
+
109
+ // Reload systemd
110
+ execSync('systemctl daemon-reload', { stdio: 'inherit' });
111
+
103
112
  console.log(`System limits configured successfully for ${maxCCU} CCU (${MAX_CCU_PER_CPU}/CPU)`);
104
113
 
105
114
  } catch (error) {