Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3493283
D6552.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D6552.diff
View Options
diff --git a/lib/utils/ens-cache.js b/lib/utils/ens-cache.js
--- a/lib/utils/ens-cache.js
+++ b/lib/utils/ens-cache.js
@@ -2,8 +2,16 @@
import namehash from 'eth-ens-namehash';
+import sleep from './sleep';
+
const cacheTimeout = 24 * 60 * 60 * 1000; // one day
const failedQueryCacheTimeout = 5 * 60 * 1000; // five minutes
+const queryTimeout = 5 * 1000; // five seconds
+
+async function throwOnTimeout(ethAddressOrName: string) {
+ await sleep(queryTimeout);
+ throw new Error(`ENS fetch for ${ethAddressOrName} timed out`);
+}
export type EthersProvider = {
+lookupAddress: (address: string) => Promise<?string>,
@@ -73,7 +81,10 @@
// ethers.js handles checking forward resolution (point 1 above) for us
let ensName;
try {
- ensName = await this.provider.lookupAddress(normalizedETHAddress);
+ ensName = await Promise.race([
+ this.provider.lookupAddress(normalizedETHAddress),
+ throwOnTimeout(normalizedETHAddress),
+ ]);
} catch (e) {
console.log(e);
return null;
@@ -92,7 +103,7 @@
this.nameQueryCache.set(normalizedETHAddress, {
normalizedETHAddress,
- expirationTime: Date.now() + cacheTimeout,
+ expirationTime: Date.now() + queryTimeout * 2,
normalizedENSName: fetchENSNamePromise,
});
@@ -154,7 +165,10 @@
const fetchETHAddressPromise = (async () => {
let ethAddress;
try {
- ethAddress = await this.provider.resolveName(normalizedENSName);
+ ethAddress = await Promise.race([
+ this.provider.resolveName(normalizedENSName),
+ throwOnTimeout(normalizedENSName),
+ ]);
} catch (e) {
console.log(e);
return null;
@@ -167,7 +181,7 @@
this.addressQueryCache.set(normalizedENSName, {
normalizedENSName,
- expirationTime: Date.now() + cacheTimeout,
+ expirationTime: Date.now() + queryTimeout * 2,
normalizedETHAddress: fetchETHAddressPromise,
});
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Dec 20, 2:45 AM (20 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2679895
Default Alt Text
D6552.diff (1 KB)
Attached To
Mode
D6552: [lib] Add timeout to ENSCache
Attached
Detach File
Event Timeline
Log In to Comment