@@ -67,7 +67,7 @@ type Producer struct {
6767// producers.
6868type ProducerRequest struct {
6969 Topic string
70- Message []byte
70+ Messages [] []byte
7171 Response chan <- error
7272 Deadline time.Time
7373}
@@ -158,7 +158,17 @@ func (p *Producer) Stop() {
158158// first unsuccessful attempt to publish the message. It is the responsibility
159159// of the caller to retry if necessary.
160160func (p * Producer ) Publish (message []byte ) (err error ) {
161- return p .PublishTo (p .topic , message )
161+ return p .MultiPublishTo (p .topic , [][]byte {message })
162+ }
163+
164+ // MultiPublish sends message batch using the producer p, returning an error if it was
165+ // already closed or if an error occurred while publishing the messages.
166+ //
167+ // Note that no retry is done internally, the producer will fail after the
168+ // first unsuccessful attempt to publish the message. It is the responsibility
169+ // of the caller to retry if necessary.
170+ func (p * Producer ) MultiPublish (message [][]byte ) (err error ) {
171+ return p .MultiPublishTo (p .topic , message )
162172}
163173
164174// PublishTo sends a message to a specific topic using the producer p, returning
@@ -169,6 +179,17 @@ func (p *Producer) Publish(message []byte) (err error) {
169179// first unsuccessful attempt to publish the message. It is the responsibility
170180// of the caller to retry if necessary.
171181func (p * Producer ) PublishTo (topic string , message []byte ) (err error ) {
182+ return p .MultiPublishTo (p .topic , [][]byte {message })
183+ }
184+
185+ // MultiPublishTo sends a message batch to a specific topic using the producer p, returning
186+ // an error if it was already closed or if an error occurred while publishing the
187+ // message.
188+ //
189+ // Note that no retry is done internally, the producer will fail after the
190+ // first unsuccessful attempt to publish the message. It is the responsibility
191+ // of the caller to retry if necessary.
192+ func (p * Producer ) MultiPublishTo (topic string , messages [][]byte ) (err error ) {
172193 defer func () {
173194 if recover () != nil {
174195 err = errors .New ("publishing to a producer that was already stopped" )
@@ -186,7 +207,7 @@ func (p *Producer) PublishTo(topic string, message []byte) (err error) {
186207 // it up.
187208 p .reqs <- ProducerRequest {
188209 Topic : topic ,
189- Message : message ,
210+ Messages : messages ,
190211 Response : response ,
191212 Deadline : deadline ,
192213 }
@@ -297,7 +318,7 @@ func (p *Producer) run() {
297318 continue
298319 }
299320
300- if err := p .publish (conn , req .Topic , req .Message ); err != nil {
321+ if err := p .publish (conn , req .Topic , req .Messages ); err != nil {
301322 req .complete (err )
302323 shutdown (err )
303324 continue
@@ -365,11 +386,21 @@ func (p *Producer) write(conn *Conn, cmd Command) (err error) {
365386 return
366387}
367388
368- func (p * Producer ) publish (conn * Conn , topic string , message []byte ) error {
369- return p .write (conn , Pub {
370- Topic : topic ,
371- Message : message ,
372- })
389+ func (p * Producer ) publish (conn * Conn , topic string , messages [][]byte ) error {
390+ switch len (messages ) {
391+ case 0 :
392+ return nil
393+ case 1 :
394+ return p .write (conn , Pub {
395+ Topic : topic ,
396+ Message : messages [0 ],
397+ })
398+ default :
399+ return p .write (conn , MPub {
400+ Topic : topic ,
401+ Messages : messages ,
402+ })
403+ }
373404}
374405
375406func (p * Producer ) ping (conn * Conn ) error {
0 commit comments