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

Update consume backpressure handling #508

Closed
wants to merge 10 commits into from
Closed

Update consume backpressure handling #508

wants to merge 10 commits into from

Conversation

mtmk
Copy link
Collaborator

@mtmk mtmk commented Jun 5, 2024

Moved pull request calculations to when the messages are yielded to calculate if we need to pull more messages accurately to prevent pulling excessive messages from the server. It also has been bounded to a maximum of 1024 to avoid large object heap allocations.

The capacity now has a limit set to maxMsgs to prevent pulling
excessive messages from the server. It also has been bounded to a
maximum of 1024 to avoid large object heap allocations.
@mtmk mtmk marked this pull request as draft June 5, 2024 23:41
@mtmk mtmk marked this pull request as ready for review June 5, 2024 23:49
@mtmk mtmk marked this pull request as draft June 5, 2024 23:49
@mtmk mtmk requested a review from caleblloyd June 5, 2024 23:59
// resulting in a large number of pending messages (which can be a problem if the
// application is slow to process messages). The capacity is bounded to a maximum of 1024
// to avoid LOH allocations.
_userMsgs = Channel.CreateBounded<NatsJSMsg<TMsg>>(maxMsgs > 1024 ? 1024 : (int)maxMsgs);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there ever a case where max msgs is 0(or negative), such as if they only specify max bytes?

Copy link
Collaborator Author

@mtmk mtmk Jun 6, 2024

Choose a reason for hiding this comment

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

Good point. should we set a minimum?
edit set minimum to 64

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should just make sure it’s at least 1? If <=0 default it to 1024, because that probably means they are setting max bytes and not max msgs

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

makes sense

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changing the implementation to use 'Delivered()' function. this is set to a fixed 1024 since the size of the channel will not affect the pull requests anymore.

@mtmk mtmk marked this pull request as ready for review June 6, 2024 10:51
@caleblloyd
Copy link
Collaborator

What happens if they use MaxBytes instead? Won't the channel be 1024 again and it will pull through all of the messages until it is full again?

Should we introduce an intermediate channel that consumes the full pull, then calls for the next pull after it is half way consumed?

@mtmk mtmk changed the title Update consumer channel capacity to maxMsgs Update consume backpressure handling Jun 7, 2024
@mtmk mtmk self-assigned this Jun 14, 2024
@mtmk mtmk marked this pull request as draft June 14, 2024 01:23
@mtmk
Copy link
Collaborator Author

mtmk commented Aug 22, 2024

(revisiting this because of #608)

So the idea is to properly handle the backpressure i.e. do not issue pull requests unless application code has received the message. Before this PR that check was done after the message was buffered through the channel and we were pulling 1000 messages (size of the bounded channel) regardless of application code actually picking up the message.

Issuing a Delivered() call after yielding the message to application, we can then make the calculation and decide if we should issue another Pull Request to the server or not.

@mtmk
Copy link
Collaborator Author

mtmk commented Aug 22, 2024

I need to fix the tests... edit: fixed

@mtmk mtmk added this to the 2.4.0 milestone Sep 9, 2024
@mtmk
Copy link
Collaborator Author

mtmk commented Sep 9, 2024

What happens if they use MaxBytes instead? Won't the channel be 1024 again and it will pull through all of the messages until it is full again?

Should we introduce an intermediate channel that consumes the full pull, then calls for the next pull after it is half way consumed?

we don't have to worry about this any more since calculation is done after message is yielded through the Delivered() method.

@mtmk mtmk marked this pull request as ready for review September 9, 2024 18:09
@mtmk
Copy link
Collaborator Author

mtmk commented Sep 10, 2024

moved to #626

@mtmk mtmk closed this Sep 10, 2024
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.

MaxMsg=1 not respected. second message ALWAYS gets redelivered as if its ackwait started before yielded
2 participants