Skip to content

Commit

Permalink
* Optimized lock functionality
Browse files Browse the repository at this point in the history
* Optimized computeIfAbsent functionality
* Optimized YouTube requests
  • Loading branch information
danthonywalker committed Aug 11, 2023
1 parent 09f7442 commit cbf2ea4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
31 changes: 19 additions & 12 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,25 @@ const computeIfAbsent = async <T>(
key: string,
callback: () => Promise<NonNullable<T>>,
expireInMilliseconds = Number.MAX_VALUE,
) =>
lock(key, async (lock) => {
let value = await get<NonNullable<T>>(key);

if (value === undefined) {
value = await callback();
const json = JSON.stringify(value);
await atomicSet(lock, key, json, expireInMilliseconds);
}

return value;
});
) => {
let value = await get<NonNullable<T>>(key);

if (value === undefined) {
value = await lock(key, async (lock) => {
let value = await get<NonNullable<T>>(key);

if (value === undefined) {
value = await callback();
const json = JSON.stringify(value);
await atomicSet(lock, key, json, expireInMilliseconds);
}

return value;
});
}

return value;
};

export default {
computeIfAbsent,
Expand Down
8 changes: 4 additions & 4 deletions src/features/creators/subscriptions/create/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ export default async (interaction: ChatInputCommandInteraction) => {
}

const buttonId = buttonInteraction.customId;
const youtubeChannelId = selectedYoutubeChannel?.channelId;
const { channelId, channelTitle, title } = selectedYoutubeChannel ?? {};

if (buttonId === cancelButtonId || typeof youtubeChannelId !== "string")
if (buttonId === cancelButtonId || typeof channelId !== "string")
return buttonInteraction.update(noResultsExistOptions(null));

await database.createCreatorSubscriptions({
domainId: youtubeChannelId,
domainId: channelId,
creatorType: CreatorType.YOUTUBE,
creatorChannelIds: selectedCreatorChannelIds,
});
Expand All @@ -300,7 +300,7 @@ export default async (interaction: ChatInputCommandInteraction) => {
.map(channelMention)
.join(", ");

const youtubeChannelName = selectedYoutubeChannel?.title ?? name;
const youtubeChannelName = channelTitle ?? title ?? name;
const description = compress`
Successfully created a subscription for ${bold(youtubeChannelName)}! Posts
will now be automatically created in ${channelMentions} when
Expand Down
17 changes: 6 additions & 11 deletions src/features/creators/youtube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const getChannels = (query: string) => {
const key = CacheKey.channels(query);
const callback = async () => {
const { data } = await search.list({
fields:
"items(snippet(channelId,channelTitle,description,publishedAt,thumbnails,title))",
maxResults: 50,
part: ["snippet"],
q: query,
Expand All @@ -30,19 +32,11 @@ export const getChannel = (channelId: string) => {
const key = CacheKey.channel(channelId);
const callback = async () => {
const { data } = await channels.list({
fields:
"items(contentDetails(relatedPlaylists(uploads)),id,snippet(title,thumbnails))",
id: [channelId],
maxResults: 1,
part: [
"brandingSettings",
"contentDetails",
"contentOwnerDetails",
"id",
"localizations",
"snippet",
"statistics",
"status",
"topicDetails",
],
part: ["contentDetails", "id", "snippet"],
});

const items = data.items ?? [];
Expand All @@ -59,6 +53,7 @@ export const getVideos = (playlistId: string) => {
const key = CacheKey.videos(playlistId);
const callback = async () => {
const { data } = await playlistItems.list({
fields: "items(snippet(description,publishedAt,resourceId,title))",
maxResults: 50,
part: ["snippet"],
playlistId,
Expand Down
8 changes: 6 additions & 2 deletions src/lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,14 @@ export default async <T>(
let callbackResult = callback(lockObject);

while (callbackResult instanceof Promise) {
await extendLock(lockObject, expireInMilliseconds);
const sleepPromise = sleep(expireInMilliseconds / 2);
const result = await Promise.race([callbackResult, sleepPromise]);
callbackResult = result === undefined ? callbackResult : result;

if (result === undefined) {
await extendLock(lockObject, expireInMilliseconds);
} else {
callbackResult = result;
}
}

return callbackResult;
Expand Down

0 comments on commit cbf2ea4

Please sign in to comment.