-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[WIP][FIXED] Fix request/reply performance when using allow_responses perms #6064
Open
jack7803m
wants to merge
3
commits into
nats-io:main
Choose a base branch
from
jack7803m:replies-map-perf-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -262,6 +262,9 @@ type client struct { | |
last time.Time | ||
lastIn time.Time | ||
|
||
repliesSincePrune uint16 | ||
lastReplyPrune time.Time | ||
|
||
headers bool | ||
|
||
rtt time.Duration | ||
|
@@ -422,6 +425,7 @@ const ( | |
pruneSize = 32 | ||
routeTargetInit = 8 | ||
replyPermLimit = 4096 | ||
replyPruneTime = time.Second | ||
) | ||
|
||
// Represent read cache booleans with a bitmask | ||
|
@@ -3636,7 +3640,8 @@ func (c *client) deliverMsg(prodIsMQTT bool, sub *subscription, acc *Account, su | |
// do that accounting here. We only look at client.replies which will be non-nil. | ||
if client.replies != nil && len(reply) > 0 { | ||
client.replies[string(reply)] = &resp{time.Now(), 0} | ||
if len(client.replies) > replyPermLimit { | ||
client.repliesSincePrune++ | ||
if client.repliesSincePrune > replyPermLimit || time.Since(client.lastReplyPrune) > replyPruneTime { | ||
client.pruneReplyPerms() | ||
} | ||
} | ||
|
@@ -3760,6 +3765,9 @@ func (c *client) pruneReplyPerms() { | |
delete(c.replies, k) | ||
} | ||
} | ||
|
||
c.repliesSincePrune = 0 | ||
c.lastReplyPrune = time.Now() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can just now from above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point! Just fixed it |
||
} | ||
|
||
// pruneDenyCache will prune the deny cache via randomly | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a sense under heaby load how much more memory this will hold onto?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original issue was it holding onto too much memory and looping through all of it every message.
Just added some debug statements and found that if the reply subject is already allowed (eg.
"pub": ">"
), then the reply counter never actually goes up and therefore it never is able to prune that subject out until it expires by time. Just noting this because I'm going to be looking at a fix for that too but not sure if it'll have broader effects (hopefully not).Assuming that the subjects are getting pruned as they're replied to, at most it should only be able to get the
replies
map to an extrareplyPermLimit
size than what it could've possible been before in the worst case scenario. Even for that to happen it would have to fill the map with subjects, attempt to prune, then expire them all by the next message - in that case, pruning for every message over thereplyPermLimit
would cause it to immediately prune whereas this solution would hold onto that memory for the nextreplyPermLimit
messages, making the map sizereplyPermLimit * 2
.Under normal heavy load it shouldn't make any significant difference, as the current behavior typically should only run the prune once every
replyPermLimit
messages anyway when it's configured properly.