network chunk v10
This commit is contained in:
Generated
+8
@@ -21,6 +21,7 @@
|
||||
"@types/better-sqlite3": "^7.6.11",
|
||||
"@types/lokijs": "^1.5.14",
|
||||
"@types/node": "^22.6.1",
|
||||
"@types/ping": "^0.4.4",
|
||||
"@types/sqlite3": "^3.1.11",
|
||||
"@types/uuid": "^10.0.0",
|
||||
"concurrently": "^8.2.2",
|
||||
@@ -179,6 +180,13 @@
|
||||
"undici-types": "~6.19.2"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/ping": {
|
||||
"version": "0.4.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/ping/-/ping-0.4.4.tgz",
|
||||
"integrity": "sha512-ifvo6w2f5eJYlXm+HiVx67iJe8WZp87sfa683nlqED5Vnt9Z93onkokNoWqOG21EaE8fMxyKPobE+mkPEyxsdw==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/sqlite3": {
|
||||
"version": "3.1.11",
|
||||
"resolved": "https://registry.npmjs.org/@types/sqlite3/-/sqlite3-3.1.11.tgz",
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
"@types/better-sqlite3": "^7.6.11",
|
||||
"@types/lokijs": "^1.5.14",
|
||||
"@types/node": "^22.6.1",
|
||||
"@types/ping": "^0.4.4",
|
||||
"@types/sqlite3": "^3.1.11",
|
||||
"@types/uuid": "^10.0.0",
|
||||
"concurrently": "^8.2.2",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ParsedMessage } from '../message_handler';
|
||||
import { OperationHandler } from '../operations_base/operation_handler';
|
||||
import { exec } from 'child_process';
|
||||
import ping from "ping";
|
||||
|
||||
export abstract class SocketCommunicatorBase {
|
||||
protected readonly ip: string;
|
||||
@@ -23,34 +23,35 @@ export abstract class SocketCommunicatorBase {
|
||||
|
||||
protected async scanNetworkLatency(): Promise<number> {
|
||||
const targetIp = this.ip; // Use the IP from the superclass
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(`ping -c 1 ${targetIp}`, (error, stdout) => {
|
||||
if (error) {
|
||||
console.error(`Ping error: ${error}`);
|
||||
return reject(error);
|
||||
|
||||
try {
|
||||
const response = await ping.promise.probe(targetIp);
|
||||
|
||||
if (!response.alive || response.time === "unknown") {
|
||||
console.warn(`Ping failed to reach ${targetIp}. Using default network speed.`);
|
||||
return 200; // Default latency in ms if ping fails
|
||||
}
|
||||
|
||||
const match = stdout.match(/time=([\d.]+) ms/);
|
||||
if (match && match[1]) {
|
||||
const latency = parseFloat(match[1]);
|
||||
console.log(`Network latency to ${targetIp} is approximately ${latency} ms`);
|
||||
resolve(latency);
|
||||
} else {
|
||||
reject(new Error('Unable to determine latency from ping output.'));
|
||||
return response.time; // Latency in ms from ping response
|
||||
} catch (error: any) {
|
||||
console.error(`Ping error: ${error.message}. Using default network speed.`);
|
||||
return 200; // Default latency in ms if an error occurs
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Calculate optimal chunk size based on network latency, with fallback if necessary
|
||||
protected async calculateOptimalChunkSize(messageLength: number): Promise<number> {
|
||||
this.networkSpeed = await this.scanNetworkLatency();
|
||||
const latency = await this.scanNetworkLatency();
|
||||
this.networkSpeed = latency > 0 ? 1000 / latency : 1; // Speed in bytes/ms based on latency
|
||||
|
||||
// Calculate initial chunk size based on latency (bounded between 512 and 1024 bytes)
|
||||
let chunkSize = Math.min(Math.max(512, Math.floor(5000 / this.networkSpeed)), 1024);
|
||||
|
||||
// Ensure chunk size aligns with base64 encoding (multiple of 4)
|
||||
// Adjust chunk size for base64 alignment (multiple of 4)
|
||||
while (messageLength % chunkSize !== 0 && chunkSize > 0) {
|
||||
chunkSize -= 4;
|
||||
}
|
||||
|
||||
return chunkSize || 1024;
|
||||
return chunkSize || 1024; // Fallback to 1024 if alignment adjustment results in 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ParsedMessage } from '../message_handler';
|
||||
import { OperationHandler } from '../operations_base/operation_handler';
|
||||
import { exec } from 'child_process';
|
||||
import ping from "ping";
|
||||
|
||||
export abstract class SocketCommunicatorBase {
|
||||
protected readonly ip: string;
|
||||
@@ -23,34 +23,35 @@ export abstract class SocketCommunicatorBase {
|
||||
|
||||
protected async scanNetworkLatency(): Promise<number> {
|
||||
const targetIp = this.ip; // Use the IP from the superclass
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(`ping -c 1 ${targetIp}`, (error, stdout) => {
|
||||
if (error) {
|
||||
console.error(`Ping error: ${error}`);
|
||||
return reject(error);
|
||||
|
||||
try {
|
||||
const response = await ping.promise.probe(targetIp);
|
||||
|
||||
if (!response.alive || response.time === "unknown") {
|
||||
console.warn(`Ping failed to reach ${targetIp}. Using default network speed.`);
|
||||
return 200; // Default latency in ms if ping fails
|
||||
}
|
||||
|
||||
const match = stdout.match(/time=([\d.]+) ms/);
|
||||
if (match && match[1]) {
|
||||
const latency = parseFloat(match[1]);
|
||||
console.log(`Network latency to ${targetIp} is approximately ${latency} ms`);
|
||||
resolve(latency);
|
||||
} else {
|
||||
reject(new Error('Unable to determine latency from ping output.'));
|
||||
return response.time; // Latency in ms from ping response
|
||||
} catch (error: any) {
|
||||
console.error(`Ping error: ${error.message}. Using default network speed.`);
|
||||
return 200; // Default latency in ms if an error occurs
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Calculate optimal chunk size based on network latency, with fallback if necessary
|
||||
protected async calculateOptimalChunkSize(messageLength: number): Promise<number> {
|
||||
this.networkSpeed = await this.scanNetworkLatency();
|
||||
const latency = await this.scanNetworkLatency();
|
||||
this.networkSpeed = latency > 0 ? 1000 / latency : 1; // Speed in bytes/ms based on latency
|
||||
|
||||
// Calculate initial chunk size based on latency (bounded between 512 and 1024 bytes)
|
||||
let chunkSize = Math.min(Math.max(512, Math.floor(5000 / this.networkSpeed)), 1024);
|
||||
|
||||
// Ensure chunk size aligns with base64 encoding (multiple of 4)
|
||||
// Adjust chunk size for base64 alignment (multiple of 4)
|
||||
while (messageLength % chunkSize !== 0 && chunkSize > 0) {
|
||||
chunkSize -= 4;
|
||||
}
|
||||
|
||||
return chunkSize || 1024;
|
||||
return chunkSize || 1024; // Fallback to 1024 if alignment adjustment results in 0
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user