try both client methods that are available to check if a given user leads a given channel in a Promise.race. this ensures faster lookups initially, before the cache contains any data about a user
Details
Details
- Reviewers
ashoat
async userLeadsChannel( fid: string, farcasterChannelID: string, ): Promise<boolean> { const userChannelsPromise = (async () => { const ledChannels = await this.fetchLedFarcasterChannels(fid); const result = ledChannels.some( channel => channel.id === farcasterChannelID, ); if (farcasterChannelID === 'bromero') { console.log('user channels result:', result); console.log('led channels:', ledChannels); } return { result, source: 'userChannels' }; })(); const channelSearchPromise = (async () => { try { const channel = await this.fetchFarcasterChannelByName(farcasterChannelID); const result = channel?.lead.fid === parseInt(fid); if (farcasterChannelID === 'bromero') { console.log('channel search result:', result); console.log('channel details:', channel); } return { result, source: 'channelSearch' }; } catch (e) { // Wait 5 seconds to give the other promise a chance to settle console.log('sleeping'); await sleep(5000); throw e; } })(); const { result, source } = await Promise.race([ userChannelsPromise, channelSearchPromise, ]); console.log(`Promise from ${source} settled first.`); return result; // Only return the boolean result }
modified my function to see which promise settled first, and as expected, in the first batch of 5, the channelSearchPromise settled first, but afterwards the userChannelsPromise always settled first.
modified fetchFarcasterChannelByName to throw and confirmed that the promise still resolved with the result form userChannelsPromise (simulates, for example, a 429 Too Many Requests response)
also confirmed that function returns true for channel bromero, which I confirmed is led by j4ck.eth (whose fid i used for my test user)
Diff Detail
Diff Detail
- Repository
- rCOMM Comm
- Branch
- fcchannel (branched from master)
- Lint
No Lint Coverage - Unit
No Test Coverage
Event Timeline
Comment Actions
Talked to @varun offline. We want to make two changes:
- Replace in-memory caching with Redis
- Replace Promise.race with logic that first checks Redis. If it's there we return cache result, if not we start the big batch request in the background (and save to Redis at the end), and then hit the fast API for an actual response to the keyserver endpoint request