This commit is contained in:
Alexander Zinchuk 2021-07-28 03:14:55 +03:00
parent 345dab4a7e
commit 5430c0229b
4 changed files with 26 additions and 17 deletions

View File

@ -25,6 +25,7 @@ const DEFAULT_DC_ID = 2;
const WEBDOCUMENT_DC_ID = 4;
const DEFAULT_IPV4_IP = 'zws2.web.telegram.org';
const DEFAULT_IPV6_IP = '[2001:67c:4e8:f002::a]';
const EXPORTED_SENDER_RECONNECT_TIMEOUT = 1000; // 1 sec
const EXPORTED_SENDER_RELEASE_TIMEOUT = 30000; // 30 sec
const WEBDOCUMENT_REQUEST_PART_SIZE = 131072; // 128kb
@ -374,8 +375,14 @@ class TelegramClient {
let sender;
try {
sender = await this._exportedSenderPromises[dcId];
if (!sender.isConnected()) {
return this._borrowExportedSender(dcId, true, sender);
if (sender.isConnecting) {
await Helpers.sleep(EXPORTED_SENDER_RECONNECT_TIMEOUT);
return this._borrowExportedSender(dcId, false, sender);
} else {
return this._borrowExportedSender(dcId, true, sender);
}
}
} catch (err) {
// eslint-disable-next-line no-console
@ -390,9 +397,6 @@ class TelegramClient {
}
this._exportedSenderReleaseTimeouts[dcId] = setTimeout(() => {
// eslint-disable-next-line no-console
console.warn(`Disconnecting from file socket #${dcId}...`);
this._exportedSenderReleaseTimeouts[dcId] = undefined;
sender.disconnect();
}, EXPORTED_SENDER_RELEASE_TIMEOUT);

View File

@ -104,7 +104,7 @@ export async function downloadFile(
progressCallback(progress);
}
// used to populate the sender
// Preload sender
await client.getSender(dcId);
// eslint-disable-next-line no-constant-condition
@ -120,7 +120,7 @@ export async function downloadFile(
await foreman.requestWorker();
if (hasEnded) {
await foreman.releaseWorker();
foreman.releaseWorker();
break;
}
@ -128,8 +128,9 @@ export async function downloadFile(
promises.push((async (offsetMemo: number) => {
// eslint-disable-next-line no-constant-condition
while (true) {
let sender;
try {
const sender = await client.getSender(dcId);
sender = await client.getSender(dcId);
const result = await sender.send(new Api.upload.GetFile({
location: inputLocation,
offset: offsetMemo,
@ -150,23 +151,22 @@ export async function downloadFile(
hasEnded = true;
}
foreman.releaseWorker();
return result.bytes;
} catch (err) {
if (err.message === 'Disconnect') {
if (sender && !sender.isConnected()) {
await sleep(DISCONNECT_SLEEP);
continue;
} else if (err instanceof errors.FloodWaitError) {
await sleep(err.seconds * 1000);
continue;
} else if (err.message !== 'USER_CANCELED') {
// eslint-disable-next-line no-console
console.error(err);
}
foreman.releaseWorker();
hasEnded = true;
throw err;
} finally {
foreman.releaseWorker();
}
}
})(offset));

View File

@ -66,9 +66,10 @@ export async function uploadFile(
// eslint-disable-next-line no-loop-func
sendingParts.push((async (jMemo: number, bytesMemo: Buffer) => {
while (true) {
let sender;
try {
// We always upload from the DC we are in
const sender = await client.getSender(client.session.dcId);
sender = await client.getSender(client.session.dcId);
await sender.send(
isLarge
? new Api.upload.SaveBigFilePart({
@ -84,7 +85,7 @@ export async function uploadFile(
}),
);
} catch (err) {
if (err.message === 'Disconnect') {
if (sender && !sender.isConnected()) {
await sleep(DISCONNECT_SLEEP);
continue;
} else if (err instanceof errors.FloodWaitError) {
@ -106,6 +107,7 @@ export async function uploadFile(
}
})(j, bytes));
}
await Promise.all(sendingParts);
}

View File

@ -93,8 +93,11 @@ class PromisedWebSockets {
};
this.client.onclose = (event) => {
const { code, reason, wasClean } = event;
// eslint-disable-next-line no-console
console.error(`Socket ${ip} closed. Code: ${code}, reason: ${reason}, was clean: ${wasClean}`);
if (code !== 1000) {
// eslint-disable-next-line no-console
console.error(`Socket ${ip} closed. Code: ${code}, reason: ${reason}, was clean: ${wasClean}`);
}
this.resolveRead(false);
this.closed = true;
if (this.disconnectedCallback) {