Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #1112] feat: optimize producer send async #1111

Merged
merged 2 commits into from
Dec 4, 2023

Conversation

twz915
Copy link
Contributor

@twz915 twz915 commented Nov 28, 2023

What is the purpose of the change

#1112 optimize producer send async, without block (connect and sendRequest)

Comparison of the time spent sending messages

old version
image

new version
image

Example to Test

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
)

var defaultProducer = getProducer()

func getProducer() rocketmq.Producer {
	p, err := rocketmq.NewProducer(
		producer.WithSendMsgTimeout(time.Second*3),
		producer.WithRetry(2),
	)
	if err != nil {
		panic(err)
	}
	err = p.Start()
	if err != nil {
		panic(err)
	}
	return p
}

func sendMsg(topic string, body []byte) error {
	msg := primitive.NewMessage(topic, body)
	ctx := context.Background()
	startTime := time.Now()
	err := defaultProducer.SendAsync(ctx, func(ctx context.Context, result *primitive.SendResult, err error) {
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println(result.MsgID)
	}, msg)
	fmt.Println("t: ", time.Since(startTime))
	return err
}

func main() {
	for i := 0; i < 20; i++ {
		sendMsg("your-topic-here", []byte("test-body1"))
	}
	time.Sleep(time.Second * 10)
	return
}

@twz915 twz915 changed the title feat: optimize producer send async [ISSUE #1112] feat: optimize producer send async Nov 28, 2023
@ShannonDing ShannonDing merged commit 7ffb599 into apache:master Dec 4, 2023
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants