Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding backpressure design pattern #3233 #3249

Merged
merged 8 commits into from
Apr 12, 2025
Merged

Conversation

sanurah
Copy link
Contributor

@sanurah sanurah commented Apr 9, 2025

Pull Request Template

What does this PR do?

This PR is adding the backpressure design pattern
Relates to #3233

Copy link

github-actions bot commented Apr 9, 2025

PR Summary

This PR implements the backpressure design pattern in Java using Reactor. It includes a Publisher that generates a stream of integers, and a Subscriber that processes them with a simulated delay, demonstrating backpressure handling to prevent consumer overload. The implementation uses Reactor's BaseSubscriber to control the request rate and prevent overwhelming the subscriber. The PR also includes comprehensive unit tests and documentation.

Changes

File Summary
backpressure/README.md This file provides a comprehensive explanation of the Backpressure design pattern, including real-world examples, a detailed explanation, a programmatic example in Java using Reactor, and when to use or avoid this pattern. It also includes diagrams and references.
backpressure/etc/backpressure.png New file: Diagram showing the Backpressure pattern architecture.
backpressure/pom.xml This file configures the Maven project dependencies, including Reactor and testing libraries. It also sets up the main class for execution.
backpressure/src/main/java/com/iluwatar/backpressure/App.java This Java class serves as the main application entry point. It demonstrates the backpressure pattern by creating a publisher and subscriber, and then subscribing the subscriber to the publisher's stream.
backpressure/src/main/java/com/iluwatar/backpressure/Publisher.java This Java class implements the Publisher, which generates a stream of integers with a configurable delay. It uses Reactor's Flux to create the stream.
backpressure/src/main/java/com/iluwatar/backpressure/Subscriber.java This Java class implements the Subscriber, which processes the integer stream from the Publisher. It uses Reactor's BaseSubscriber to implement backpressure, requesting items in batches and simulating processing time.
backpressure/src/test/java/com/iluwatar/backpressure/AppTest.java This Java class contains a unit test to verify that the main application runs without throwing any exceptions.
backpressure/src/test/java/com/iluwatar/backpressure/LoggerExtension.java This Java class is a JUnit extension that captures log messages for testing purposes. It uses Logback to capture log events and provides methods to access them.
backpressure/src/test/java/com/iluwatar/backpressure/PublisherTest.java This Java class contains unit tests for the Publisher class, verifying that it generates the expected stream of integers with the correct delays.
backpressure/src/test/java/com/iluwatar/backpressure/SubscriberTest.java This Java class contains unit tests for the Subscriber class, verifying that it correctly processes the stream and applies backpressure.
pom.xml Added a module for the backpressure example to the main pom.xml file.

autogenerated by presubmit.ai

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (3)

temp

Files Processed (11)
  • backpressure/README.md (1 hunk)
  • backpressure/etc/backpressure.png (0 hunks)
  • backpressure/pom.xml (1 hunk)
  • backpressure/src/main/java/com/iluwatar/backpressure/App.java (1 hunk)
  • backpressure/src/main/java/com/iluwatar/backpressure/Publisher.java (1 hunk)
  • backpressure/src/main/java/com/iluwatar/backpressure/Subscriber.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/AppTest.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/LoggerExtension.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/PublisherTest.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/SubscriberTest.java (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
Files Processed (10)
  • backpressure/README.md (1 hunk)
  • backpressure/pom.xml (1 hunk)
  • backpressure/src/main/java/com/iluwatar/backpressure/App.java (1 hunk)
  • backpressure/src/main/java/com/iluwatar/backpressure/Publisher.java (1 hunk)
  • backpressure/src/main/java/com/iluwatar/backpressure/Subscriber.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/AppTest.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/LoggerExtension.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/PublisherTest.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/SubscriberTest.java (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
Files Processed (5)
  • backpressure/src/main/java/com/iluwatar/backpressure/App.java (1 hunk)
  • backpressure/src/main/java/com/iluwatar/backpressure/Publisher.java (1 hunk)
  • backpressure/src/main/java/com/iluwatar/backpressure/Subscriber.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/PublisherTest.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/SubscriberTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
  • backpressure/src/main/java/com/iluwatar/backpressure/Subscriber.java [36-42]

    performance: "Inefficient delay simulation in processItem method."

@iluwatar
Copy link
Owner

Check out the Sonar issue

@Slf4j
public class Subscriber extends BaseSubscriber<Integer> {

private static final Logger logger = LoggerFactory.getLogger(Subscriber.class);
Copy link
Owner

Choose a reason for hiding this comment

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

No need to declare logger like this. With the Lombok annotation you automatically have LOGGER available.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
Files Processed (8)
  • backpressure/pom.xml (1 hunk)
  • backpressure/src/main/java/com/iluwatar/backpressure/App.java (1 hunk)
  • backpressure/src/main/java/com/iluwatar/backpressure/Publisher.java (1 hunk)
  • backpressure/src/main/java/com/iluwatar/backpressure/Subscriber.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/AppTest.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/LoggerExtension.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/PublisherTest.java (1 hunk)
  • backpressure/src/test/java/com/iluwatar/backpressure/SubscriberTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
Files Processed (1)
  • leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
  • leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java [70-76]

    best practice: "Resource Management Improvement"

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (1)
  • 817cfbc: Merge branch 'master' into master
Files Processed (0)
Actionable Comments (0)
Skipped Comments (0)

@iluwatar iluwatar merged commit 0b83b6d into iluwatar:master Apr 12, 2025
2 of 3 checks passed
@iluwatar
Copy link
Owner

Looks good! Thank you for the contribution 🎉

@all-contributors please add @sanurah for code

Copy link
Contributor

@iluwatar

@sanurah already contributed before to code

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

Successfully merging this pull request may close these issues.

2 participants