Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster connection timeouts for non-Tor connections #2588

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 50 additions & 44 deletions backends/CLNRest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,54 +73,60 @@ export default class CLNRest {
})
);
} else {
calls.set(
id,
ReactNativeBlobUtil.config({
trusty: !certVerification
})
.fetch(
method,
url,
headers,
data ? JSON.stringify(data) : data
)
.then((response: any) => {
calls.delete(id);
if (response.info().status < 300) {
// handle ws responses
if (response.data.includes('\n')) {
const split = response.data.split('\n');
const length = split.length;
// last instance is empty
return JSON.parse(split[length - 2]);
}
return response.json();
} else {
try {
const errorInfo = response.json();
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('Request timeout')), 30000);
});

const fetchPromise = ReactNativeBlobUtil.config({
trusty: !certVerification
})
.fetch(method, url, headers, data ? JSON.stringify(data) : data)
.then((response: any) => {
calls.delete(id);
if (response.info().status < 300) {
// handle ws responses
if (response.data.includes('\n')) {
const split = response.data.split('\n');
const length = split.length;
// last instance is empty
return JSON.parse(split[length - 2]);
}
return response.json();
} else {
try {
const errorInfo = response.json();
throw new Error(
(errorInfo.error && errorInfo.error.message) ||
errorInfo.message ||
errorInfo.error
);
} catch (e) {
if (
response.data &&
typeof response.data === 'string'
) {
throw new Error(response.data);
} else {
throw new Error(
(errorInfo.error &&
errorInfo.error.message) ||
errorInfo.message ||
errorInfo.error
localeString(
'backends.LND.restReq.connectionError'
)
);
} catch (e) {
if (
response.data &&
typeof response.data === 'string'
) {
throw new Error(response.data);
} else {
throw new Error(
localeString(
'backends.LND.restReq.connectionError'
)
);
}
}
}
})
);
}
});

const racePromise = Promise.race([
fetchPromise,
timeoutPromise
]).catch((error) => {
calls.delete(id);
console.log('Request timed out for:', url);
throw error;
});

calls.set(id, racePromise);
}

return await calls.get(id);
Expand Down
94 changes: 50 additions & 44 deletions backends/LND.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,54 +52,60 @@ export default class LND {
})
);
} else {
calls.set(
id,
ReactNativeBlobUtil.config({
trusty: !certVerification
})
.fetch(
method,
url,
headers,
data ? JSON.stringify(data) : data
)
.then((response: any) => {
calls.delete(id);
if (response.info().status < 300) {
// handle ws responses
if (response.data.includes('\n')) {
const split = response.data.split('\n');
const length = split.length;
// last instance is empty
return JSON.parse(split[length - 2]);
}
return response.json();
} else {
try {
const errorInfo = response.json();
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('Request timeout')), 30000);
});

const fetchPromise = ReactNativeBlobUtil.config({
trusty: !certVerification
})
.fetch(method, url, headers, data ? JSON.stringify(data) : data)
.then((response: any) => {
calls.delete(id);
if (response.info().status < 300) {
// handle ws responses
if (response.data.includes('\n')) {
const split = response.data.split('\n');
const length = split.length;
// last instance is empty
return JSON.parse(split[length - 2]);
}
return response.json();
} else {
try {
const errorInfo = response.json();
throw new Error(
(errorInfo.error && errorInfo.error.message) ||
errorInfo.message ||
errorInfo.error
);
} catch (e) {
if (
response.data &&
typeof response.data === 'string'
) {
throw new Error(response.data);
} else {
throw new Error(
(errorInfo.error &&
errorInfo.error.message) ||
errorInfo.message ||
errorInfo.error
localeString(
'backends.LND.restReq.connectionError'
)
);
} catch (e) {
if (
response.data &&
typeof response.data === 'string'
) {
throw new Error(response.data);
} else {
throw new Error(
localeString(
'backends.LND.restReq.connectionError'
)
);
}
}
}
})
);
}
});

const racePromise = Promise.race([
fetchPromise,
timeoutPromise
]).catch((error) => {
calls.delete(id);
console.log('Request timed out for:', url);
throw error;
});

calls.set(id, racePromise);
}

return await calls.get(id);
Expand Down
4 changes: 2 additions & 2 deletions stores/NodeInfoStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default class NodeInfoStore {
this.errorMsg = '';
this.loading = true;
const currentRequest = (this.currentRequest = {});
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
BackendUtils.getMyNodeInfo()
.then((data: any) => {
if (this.currentRequest !== currentRequest) {
Expand All @@ -81,7 +81,7 @@ export default class NodeInfoStore {
// handle error
this.errorMsg = errorToUserFriendly(error.toString());
this.getNodeInfoError();
resolve(error);
reject(error);
});
});
};
Expand Down
47 changes: 31 additions & 16 deletions views/Wallet/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,13 @@ export default class Wallet extends React.Component<WalletProps, WalletState> {
}
} else if (implementation === 'lndhub') {
if (connecting) {
await login({ login: username, password }).then(async () => {
BalanceStore.getLightningBalance(true);
});
try {
await login({ login: username, password });
await BalanceStore.getLightningBalance(true);
} catch (connectionError) {
console.log('LNDHub connection failed:', connectionError);
return;
}
} else {
BalanceStore.getLightningBalance(true);
}
Expand All @@ -455,22 +459,33 @@ export default class Wallet extends React.Component<WalletProps, WalletState> {
error = await connect();
}
if (!error) {
await BackendUtils.checkPerms();
await NodeInfoStore.getNodeInfo();
if (BackendUtils.supportsAccounts())
await UTXOsStore.listAccounts();
await BalanceStore.getCombinedBalance();
if (BackendUtils.supportsChannelManagement())
ChannelsStore.getChannels();
try {
await BackendUtils.checkPerms();
await NodeInfoStore.getNodeInfo();
if (BackendUtils.supportsAccounts())
await UTXOsStore.listAccounts();
await BalanceStore.getCombinedBalance();
if (BackendUtils.supportsChannelManagement())
ChannelsStore.getChannels();
} catch (connectionError) {
console.log('LNC connection failed:', connectionError);
return;
}
}
} else {
await NodeInfoStore.getNodeInfo();
if (BackendUtils.supportsAccounts()) {
UTXOsStore.listAccounts();
try {
await NodeInfoStore.getNodeInfo();
if (BackendUtils.supportsAccounts()) {
UTXOsStore.listAccounts();
}
await BalanceStore.getCombinedBalance();
ChannelsStore.getChannels();
} catch (connectionError) {
console.log('Node connection failed:', connectionError);
NodeInfoStore.getNodeInfoError();
setConnectingStatus(false);
return;
}

await BalanceStore.getCombinedBalance();
ChannelsStore.getChannels();
}

if (
Expand Down
Loading