-
Notifications
You must be signed in to change notification settings - Fork 344
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
[feat] Support memory limit for producer. #955
Conversation
e152655
to
5da7a0e
Compare
5da7a0e
to
a9823e5
Compare
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.
And I don't get why do we have to wrap a conditional variable? We can achieve the same goal with the channel itself, or just reuse the Semaphore
here
The native cond does not support timeout wait, and mainly wants to control the timeout or cancel the wait through context.
Do you want
This semaphore can only support |
Yes. But we can adopt a similar way to handle this case. For example, if we add a func (m *memoryLimitController) ReserveMemory(ctx context.Context, size int64) bool {
// NOTE: maybe we need to check if m.currentUsage > m.limit first
currentUsage := atomic.AddInt64(&m.currentUsage, size)
for currentUsage > m.limit {
select {
case <-m.ch:
currentUsage = atomic.LoadInt64(&m.currentUsage)
case <-ctx.Done(): // NOTE: Not sure if we need to reset some fields here
return false
}
}
return true
}
func (m *memoryLimitController) ReleaseMemory(size int64) {
newUsage := atomic.AddInt64(&m.currentUsage, -size)
// newUsage+size > m.limit means m was blocked in ReserveMemory method
if newUsage+size > m.limit && newUsage <= m.limit {
m.ch <- true
}
} The code above is not verified yet. But with the channel the code looks more simple and clear. |
This implementation has a problem, and the For example:
// NOTE: maybe we need to check if m.currentUsage > m.limit first Like this note. If we want to handle this case, we may need to introduce a variable |
I see. Currently let's go head with the conditional variable, but don't expose this struct to users. |
@shibd For this case, another workaround is to close the channel instead of sending value. But it's actually what this PR does. |
Master Issue: #927
Motivation
#927
Modifications
channel_cond
class to enhancesync.cond
to support wait that can accept context.memory_limit_controller
class.Verifying this change
channel_cond_test
to coverchannel_cond
memory_limit_controller_test
to covermem_mory_limit_controller
TestMemLimitRejectProducerMessages
andTestMemLimitContextCancel
to cover producer relate logic.Does this pull request potentially affect one of the following parts:
If
yes
was chosen, please highlight the changesDocumentation