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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to