nsq icon indicating copy to clipboard operation
nsq copied to clipboard

nsqd: support length 0 messages

Open twmb opened this issue 9 years ago • 1 comments

Currently, length 0 messages are rejected in doPUB for the http protocol and PUB in protocol_v2.

nsqd's diskQueue reads/writes enforce only the message header size (not requiring body). Also, the go-nsq api allows NewMessage to be called with an empty body, and DecodeMessage would make sense to also support a length 0 body to allow for idempotence between the two funcs.

A length 0 message would allow signals to be passed through an nsq system.

twmb avatar Apr 15 '16 18:04 twmb

consumer, err := nsq.NewConsumer("topic", "channel", nsq.NewConfig())
if err != nil {
    // Handle error
}

consumer.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
    // Check if the message body is empty
    if len(message.Body) == 0 {
        // Handle length 0 message
        return nil // Acknowledge the message
    }

    // Process non-empty messages here
    // ...

    return nil // Acknowledge the message
}))

err = consumer.ConnectToNSQLookupd("nsqlookupd-address:4161")
if err != nil {
    // Handle error
}

// Run the consumer
<-consumer.StopChan

ljluestc avatar Dec 27 '23 01:12 ljluestc