You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "BewareMyPower (via GitHub)" <gi...@apache.org> on 2023/03/01 03:13:32 UTC
[GitHub] [pulsar-client-go] BewareMyPower commented on pull request #955: [feat] Support memory limit for producer.
BewareMyPower commented on PR #955:
URL: https://github.com/apache/pulsar-client-go/pull/955#issuecomment-1449270747
For example, if we add a `ch chan bool` field to `memoryLimitController`, we can implement blocking `ReserveMemory` method like:
```go
func (m *memoryLimitController) ReserveMemory(ctx context.Context, size int64) bool {
currentUsage := atomic.AddInt64(&m.currentUsage, size)
for currentUsage > m.limit {
select {
case <-m.ch:
currentUsage = atomic.LoadInt64(&m.currentUsage)
case <-ctx.Done():
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 is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org