Skip to content

Commit

Permalink
Merge pull request #2588 from myxmaster/reasonable-timeout-node-conne…
Browse files Browse the repository at this point in the history
…ction

Faster connection timeouts for non-Tor connections
  • Loading branch information
kaloudis authored Dec 19, 2024
2 parents 85c7c66 + 44c3800 commit db8bd7d
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 106 deletions.
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

0 comments on commit db8bd7d

Please sign in to comment.