Skip to content

develop asynchronous request#243

Open
kastashenkova wants to merge 3 commits into
mate-academy:mainfrom
kastashenkova:res
Open

develop asynchronous request#243
kastashenkova wants to merge 3 commits into
mate-academy:mainfrom
kastashenkova:res

Conversation

@kastashenkova

Copy link
Copy Markdown

No description provided.

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Great job on setting up the overall structure with CompletableFuture and the ExecutorService! However, a couple of changes are needed before this can be approved.

First, the task requires simulating a processing delay for new requests to properly demonstrate the benefits of caching. Please add a small delay (e.g., using Thread.sleep()) inside the logic that fetches new user data in AsyncRequestProcessor.java.

Second, your current caching logic has a race condition. The if (cache.get(userId) != null) check and the subsequent cache.put() are not atomic. A safer, more efficient approach is to use the cache.computeIfAbsent() method, which guarantees atomicity for this check-and-put operation.

Addressing these points will make your solution fully compliant and robust. Keep up the good work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment on lines +23 to +28
if (cache.get(userId) != null) {
res = cache.get(userId);
} else {
res = new UserData(userId, "Details for " + userId);
cache.put(userId, res);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check-then-put pattern is not atomic and can lead to a race condition. If two threads try to process the same new userId concurrently, both could find that the user is not in the cache, and both would proceed to create a new UserData object. Using an atomic operation like cache.computeIfAbsent() would be a safer and more efficient way to handle this.

if (cache.get(userId) != null) {
res = cache.get(userId);
} else {
res = new UserData(userId, "Details for " + userId);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task requirements mention simulating a processing delay (e.g., for a database call). You should add a small delay here inside the else block, for example using Thread.sleep(), to properly simulate fetching new data. This would also make the benefits of caching more obvious.

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Great job on the overall structure and for correctly using CompletableFuture and computeIfAbsent!

I am requesting a change because there's a key logical issue that prevents the caching from working as required. In AsyncRequestProcessor.java, the Thread.sleep(1000) delay is applied to every single request, even if the data is already in the cache. This negates the performance benefit, as a core requirement is that cached results should be returned immediately.

To fix this, the simulated delay should only happen when a new UserData object is being created. Please move the Thread.sleep(1000) call inside the lambda function passed to cache.computeIfAbsent(). That way, the delay will only be triggered for non-cached requests.

You're very close to finishing this task. Keep up the great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

return null;
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(1000);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This delay is currently applied to every request, even for data that is already in the cache. According to the requirements, cached results should be returned immediately to demonstrate a significant reduction in response time.

To fix this, the simulated processing delay should only happen when a new UserData object is being created. This logic should be inside the mapping function provided to cache.computeIfAbsent(). That function is only executed when the userId is not found in the cache.

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Excellent work! I'm happy to approve your solution. You've perfectly addressed the feedback from the previous review by moving the Thread.sleep call inside the computeIfAbsent lambda. This ensures the delay only happens for new, non-cached requests, which was the main goal of the task. Your overall implementation is clean, efficient, and correctly handles asynchronous processing. Keep up the great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants