Page MenuHomePhabricator

improve logic around checking if user is channel lead
AbandonedPublic

Authored by varun on Oct 4 2024, 8:26 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Nov 10, 6:26 AM
Unknown Object (File)
Fri, Nov 8, 10:37 PM
Unknown Object (File)
Fri, Nov 8, 1:48 PM
Unknown Object (File)
Thu, Oct 31, 7:07 PM
Unknown Object (File)
Thu, Oct 31, 8:30 AM
Unknown Object (File)
Wed, Oct 23, 10:32 PM
Unknown Object (File)
Wed, Oct 23, 9:22 PM
Unknown Object (File)
Tue, Oct 22, 5:34 AM
Subscribers

Details

Reviewers
ashoat
Summary

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

Test Plan
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

Repository
rCOMM Comm
Branch
fcchannel (branched from master)
Lint
No Lint Coverage
Unit
No Test Coverage

Event Timeline

varun held this revision as a draft.
varun retitled this revision from [wip] improve logic around checking if user is channel lead to improve logic around checking if user is channel lead.Oct 4 2024, 12:27 PM
varun edited the summary of this revision. (Show Details)

increase TTL for cache and clean up code a little

varun published this revision for review.Oct 4 2024, 12:34 PM
ashoat requested changes to this revision.Oct 4 2024, 1:36 PM

Talked to @varun offline. We want to make two changes:

  1. Replace in-memory caching with Redis
  2. 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
This revision now requires changes to proceed.Oct 4 2024, 1:36 PM

discarding in favor of D13637