RobertIndie commented on code in PR #1055:
URL: https://github.com/apache/pulsar-client-go/pull/1055#discussion_r1262354724


##########
pulsar/producer_partition.go:
##########
@@ -1407,31 +1410,39 @@ func (p *partitionProducer) releaseSemaphoreAndMem(size 
int64) {
        p.client.memLimit.ReleaseMemory(size)
 }
 
-func (p *partitionProducer) canAddToQueue(sr *sendRequest, 
uncompressedPayloadSize int64) bool {
+func (p *partitionProducer) canAddToQueue(sr *sendRequest) bool {
        if p.options.DisableBlockIfQueueFull {
                if !p.publishSemaphore.TryAcquire() {
                        runCallback(sr.callback, nil, sr.msg, 
errSendQueueIsFull)
                        return false
                }
-               if !p.client.memLimit.TryReserveMemory(uncompressedPayloadSize) 
{
+       } else {
+               if !p.publishSemaphore.Acquire(sr.ctx) {
+                       runCallback(sr.callback, nil, sr.msg, errContextExpired)
+                       return false
+               }
+       }
+       p.metrics.MessagesPending.Inc()
+       p.metrics.BytesPending.Add(float64(len(sr.msg.Payload)))

Review Comment:
   I think we should consider the `msg.Schema` here when using the Schema.
   And why we add the bytes pending here instead of `canReserveMem` ?



##########
pulsar/producer_test.go:
##########
@@ -1924,6 +1924,159 @@ func TestMemLimitRejectProducerMessages(t *testing.T) {
        assert.NoError(t, err)
 }
 
+func TestMemLimitRejectProducerMessagesWithSchema(t *testing.T) {
+
+       c, err := NewClient(ClientOptions{
+               URL:              serviceURL,
+               MemoryLimitBytes: 100 * 6,
+       })
+       assert.NoError(t, err)
+       defer c.Close()
+
+       schema := NewAvroSchema(`{"fields":
+       [
+               
{"name":"id","type":"int"},{"default":null,"name":"name","type":["null","string"]}
+       ],
+       "name":"MyAvro","namespace":"schemaNotFoundTestCase","type":"record"}`, 
nil)
+
+       topicName := newTopicName()
+       producer1, _ := c.CreateProducer(ProducerOptions{
+               Topic:                   topicName,
+               DisableBlockIfQueueFull: true,
+               DisableBatching:         false,
+               BatchingMaxPublishDelay: 100 * time.Second,
+               SendTimeout:             2 * time.Second,
+       })
+
+       producer2, _ := c.CreateProducer(ProducerOptions{
+               Topic:                   topicName,
+               DisableBlockIfQueueFull: true,
+               DisableBatching:         false,
+               BatchingMaxPublishDelay: 100 * time.Second,
+               SendTimeout:             2 * time.Second,
+       })
+
+       // the size of encoded value is 6 bytes
+       value := map[string]interface{}{
+               "id": 0,
+               "name": map[string]interface{}{
+                       "string": "abc",
+               },
+       }
+
+       n := 101
+       for i := 0; i < n/2; i++ {
+               producer1.SendAsync(context.Background(), &ProducerMessage{
+                       Value:  value,
+                       Schema: schema,
+               }, func(id MessageID, message *ProducerMessage, e error) {})
+
+               producer2.SendAsync(context.Background(), &ProducerMessage{
+                       Value:  value,
+                       Schema: schema,
+               }, func(id MessageID, message *ProducerMessage, e error) {})
+       }
+       // Last message in order to reach the limit
+       producer1.SendAsync(context.Background(), &ProducerMessage{
+               Value:  value,
+               Schema: schema,
+       }, func(id MessageID, message *ProducerMessage, e error) {})
+       time.Sleep(100 * time.Millisecond)
+       assert.Equal(t, int64(n*6), c.(*client).memLimit.CurrentUsage())
+
+       _, err = producer1.Send(context.Background(), &ProducerMessage{
+               Value:  value,
+               Schema: schema,
+       })
+       assert.Error(t, err)
+       assert.ErrorContains(t, err, getResultStr(ClientMemoryBufferIsFull))
+
+       _, err = producer2.Send(context.Background(), &ProducerMessage{
+               Value:  value,
+               Schema: schema,
+       })
+       assert.Error(t, err)
+       assert.ErrorContains(t, err, getResultStr(ClientMemoryBufferIsFull))
+
+       // flush pending msg
+       err = producer1.Flush()
+       assert.NoError(t, err)
+       err = producer2.Flush()
+       assert.NoError(t, err)
+       assert.Equal(t, int64(0), c.(*client).memLimit.CurrentUsage())
+
+       _, err = producer1.Send(context.Background(), &ProducerMessage{
+               Value:  value,
+               Schema: schema,
+       })
+       assert.NoError(t, err)
+       _, err = producer2.Send(context.Background(), &ProducerMessage{
+               Value:  value,
+               Schema: schema,
+       })
+       assert.NoError(t, err)
+}
+
+func TestMemLimitRejectProducerMessagesWithChunking(t *testing.T) {
+
+       c, err := NewClient(ClientOptions{
+               URL:              serviceURL,
+               MemoryLimitBytes: 10 * 1024,
+       })
+       assert.NoError(t, err)
+       defer c.Close()
+
+       topicName := newTopicName()
+       producer1, _ := c.CreateProducer(ProducerOptions{
+               Topic:                   topicName,
+               DisableBlockIfQueueFull: true,
+               DisableBatching:         true,
+               EnableChunking:          true,
+               ChunkMaxMessageSize:     1024,
+               SendTimeout:             2 * time.Second,
+       })
+
+       producer1.SendAsync(context.Background(), &ProducerMessage{
+               Payload: make([]byte, 10*1024+1),
+       }, func(id MessageID, message *ProducerMessage, e error) {
+               if e != nil {
+                       t.Fatal(e)
+               }
+       })
+
+       _, err = producer1.Send(context.Background(), &ProducerMessage{
+               Payload: make([]byte, 1),
+       })
+       assert.Error(t, err)
+       assert.ErrorContains(t, err, getResultStr(ClientMemoryBufferIsFull))
+
+       // wait all the chunks have been released

Review Comment:
   Is it better that we add `producer.flush` before retryAssert?



-- 
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