使用 GoRoutine 和通道在 Paho MQTT 上通过 websockets 发布消息的正确方法是什么?
来源:stackoverflow
时间:2024-03-18 11:39:31 291浏览 收藏
在 Paho MQTT 库中使用 GoRoutine 和通道来发布消息时,需要采用正确的方法来确保消息的可靠性和有序性。本文将探讨两种可行的解决方案:一是通过取消对 `token.wait()` 的调用,实现异步消息发送;二是通过使用带缓冲通道的 GoRoutine,实现消息排序。本文还提供了示例代码,展示如何使用这些方法,以及如何将消息发送任务分成一组工作者,以提高性能。
作为我用来发布消息以进行测试的标准代码:
func main() { opts := mqtt.newclientoptions().addbroker("tcp://127.0.0.1:1883") opts.setclientid("myclientid_") opts.setdefaultpublishhandler(f) opts.setconnectionlosthandler(connlosthandler) opts.onconnect = func(c mqtt.client) { fmt.printf("client connected, subscribing to: test/topic\n") if token := c.subscribe("logs", 0, nil); token.wait() && token.error() != nil { fmt.println(token.error()) os.exit(1) } } c := mqtt.newclient(opts) if token := c.connect(); token.wait() && token.error() != nil { panic(token.error()) } for i := 0; i < 5; i++ { text := fmt.sprintf("this is msg #%d!", i) token := c.publish("logs", 0, false, text) token.wait() } time.sleep(3 * time.second) if token := c.unsubscribe("logs"); token.wait() && token.error() != nil { fmt.println(token.error()) os.exit(1) } c.disconnect(250) }
这个效果很好!但是在执行高延迟任务时大量传递消息,我的程序性能会很低,所以我必须使用 goroutine 和 channel。
所以,我正在寻找一种方法在 goroutine 中创建一个 worker,以便使用 golang 的 paho mqtt 库向浏览器发布消息,我很难找到满足我的需求的更好的解决方案,但经过一番搜索后,我找到了这段代码:
package main import ( "crypto/tls" "crypto/x509" "fmt" "io/ioutil" "strings" "time" MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git" "linksmart.eu/lc/core/catalog" "linksmart.eu/lc/core/catalog/service" ) // MQTTConnector provides MQTT protocol connectivity type MQTTConnector struct { config *MqttProtocol clientID string client *MQTT.Client pubCh chan AgentResponse subCh chan<- DataRequest pubTopics map[string]string subTopicsRvsd map[string]string // store SUB topics "reversed" to optimize lookup in messageHandler } const defaultQoS = 1 func (c *MQTTConnector) start() { logger.Println("MQTTConnector.start()") if c.config.Discover && c.config.URL == "" { err := c.discoverBrokerEndpoint() if err != nil { logger.Println("MQTTConnector.start() failed to start publisher:", err.Error()) return } } // configure the mqtt client c.configureMqttConnection() // start the connection routine logger.Printf("MQTTConnector.start() Will connect to the broker %v\n", c.config.URL) go c.connect(0) // start the publisher routine go c.publisher() } // reads outgoing messages from the pubCh und publishes them to the broker func (c *MQTTConnector) publisher() { for resp := range c.pubCh { if !c.client.IsConnected() { logger.Println("MQTTConnector.publisher() got data while not connected to the broker. **discarded**") continue } if resp.IsError { logger.Println("MQTTConnector.publisher() data ERROR from agent manager:", string(resp.Payload)) continue } topic := c.pubTopics[resp.ResourceId] c.client.Publish(topic, byte(defaultQoS), false, resp.Payload) // We dont' wait for confirmation from broker (avoid blocking here!) //<-r logger.Println("MQTTConnector.publisher() published to", topic) } } func (c *MQTTConnector) stop() { logger.Println("MQTTConnector.stop()") if c.client != nil && c.client.IsConnected() { c.client.Disconnect(500) } } func (c *MQTTConnector) connect(backOff int) { if c.client == nil { logger.Printf("MQTTConnector.connect() client is not configured") return } for { logger.Printf("MQTTConnector.connect() connecting to the broker %v, backOff: %v sec\n", c.config.URL, backOff) time.Sleep(time.Duration(backOff) * time.Second) if c.client.IsConnected() { break } token := c.client.Connect() token.Wait() if token.Error() == nil { break } logger.Printf("MQTTConnector.connect() failed to connect: %v\n", token.Error().Error()) if backOff == 0 { backOff = 10 } else if backOff <= 600 { backOff *= 2 } } logger.Printf("MQTTConnector.connect() connected to the broker %v", c.config.URL) return } func (c *MQTTConnector) onConnected(client *MQTT.Client) { // subscribe if there is at least one resource with SUB in MQTT protocol is configured if len(c.subTopicsRvsd) > 0 { logger.Println("MQTTPulbisher.onConnected() will (re-)subscribe to all configured SUB topics") topicFilters := make(map[string]byte) for topic, _ := range c.subTopicsRvsd { logger.Printf("MQTTPulbisher.onConnected() will subscribe to topic %s", topic) topicFilters[topic] = defaultQoS } client.SubscribeMultiple(topicFilters, c.messageHandler) } else { logger.Println("MQTTPulbisher.onConnected() no resources with SUB configured") } } func (c *MQTTConnector) onConnectionLost(client *MQTT.Client, reason error) { logger.Println("MQTTPulbisher.onConnectionLost() lost connection to the broker: ", reason.Error()) // Initialize a new client and reconnect c.configureMqttConnection() go c.connect(0) } func (c *MQTTConnector) configureMqttConnection() { connOpts := MQTT.NewClientOptions(). AddBroker(c.config.URL). SetClientID(c.clientID). SetCleanSession(true). SetConnectionLostHandler(c.onConnectionLost). SetOnConnectHandler(c.onConnected). SetAutoReconnect(false) // we take care of re-connect ourselves // Username/password authentication if c.config.Username != "" && c.config.Password != "" { connOpts.SetUsername(c.config.Username) connOpts.SetPassword(c.config.Password) } // SSL/TLS if strings.HasPrefix(c.config.URL, "ssl") { tlsConfig := &tls.Config{} // Custom CA to auth broker with a self-signed certificate if c.config.CaFile != "" { caFile, err := ioutil.ReadFile(c.config.CaFile) if err != nil { logger.Printf("MQTTConnector.configureMqttConnection() ERROR: failed to read CA file %s:%s\n", c.config.CaFile, err.Error()) } else { tlsConfig.RootCAs = x509.NewCertPool() ok := tlsConfig.RootCAs.AppendCertsFromPEM(caFile) if !ok { logger.Printf("MQTTConnector.configureMqttConnection() ERROR: failed to parse CA certificate %s\n", c.config.CaFile) } } } // Certificate-based client authentication if c.config.CertFile != "" && c.config.KeyFile != "" { cert, err := tls.LoadX509KeyPair(c.config.CertFile, c.config.KeyFile) if err != nil { logger.Printf("MQTTConnector.configureMqttConnection() ERROR: failed to load client TLS credentials: %s\n", err.Error()) } else { tlsConfig.Certificates = []tls.Certificate{cert} } } connOpts.SetTLSConfig(tlsConfig) } c.client = MQTT.NewClient(connOpts) }
这段代码正是我想要的!
但是作为 golang 中的菜鸟,我不知道如何在主函数中运行 start()
函数以及要传递什么参数!
特别是,我将如何使用通道将消息传递给工作人员(发布者)?!
我们将不胜感激您的帮助!
解决方案
我在 github repo 上发布了下面的答案,但由于您在这里提出了同样的问题,因此认为值得交叉发布(包含更多信息)。
当您说“在执行高延迟任务时大量传递消息”时,我假设您的意思是您想要异步发送消息(因此消息由与主代码运行不同的 go 例程处理) .
如果是这种情况,那么对您的初始示例进行非常简单的更改即可:
for i := 0; i < 5; i++ { text := fmt.sprintf("this is msg #%d!", i) token := c.publish("logs", 0, false, text) // comment out... token.wait() }
注意:您的示例代码可能会在消息实际发送之前退出;添加 time.sleep(10 * time.second) 会给它时间让它们熄灭;请参阅下面的代码以了解处理此问题的另一种方法
您的初始代码在消息发送之前停止的唯一原因是您调用了 token.wait()。如果您不关心错误(并且您不检查错误,所以我假设您不关心),那么调用 token.wait() 就没有什么意义(它只是等待消息发送;消息将消失无论您是否调用 token.wait())。
如果您想记录任何错误,您可以使用以下内容:
for i := 0; i < 5; i++ { text := fmt.sprintf("this is msg #%d!", i) token := c.publish("logs", 0, false, text) go func(){ token.wait() err := token.error() if err != nil { fmt.printf("error: %s\n", err.error()) // or whatever you want to do with your error } }() }
请注意,如果消息传递至关重要(但由于您没有检查错误,我假设它不是),您还需要做一些其他事情。
就您找到的代码而言;我怀疑这会增加您不需要的复杂性(并且需要更多信息才能解决此问题;例如,mqttprotocol 结构未在您粘贴的位中定义)。
额外一点...在您的评论中您提到“发布的消息必须排序”。如果这是必要的(因此您希望等到每条消息都已送达后再发送另一条消息),那么您需要类似以下内容:
msgchan := make(chan string, 200) // allow a queue of up to 200 messages var wg sync.waitgroup wg.add(1) go func(){ // go routine to send messages from channel for msg := range msgchan { token := c.publish("logs", 2, false, msg) // use qos2 is order is vital token.wait() // should check for errors here } wg.done() }() for i := 0; i < 5; i++ { text := fmt.sprintf("this is msg #%d!", i) msgchan <- text } close(msgchan) // this will stop the goroutine (when all messages processed) wg.wait() // wait for all messages to be sent before exiting (may wait for ever is mqtt broker down!)
注意:这与 ilya kaznacheev 的解决方案类似(如果将workerpoolsize 设置为 1 并使通道缓冲)
正如您的评论表明等待组使这难以理解,这里是另一种可能更清晰的等待方式(等待组通常在您等待多个事物完成时使用;在本例中,我们只等待一个因此可以使用更简单的方法)
msgchan := make(chan string, 200) // allow a queue of up to 200 messages done := make(chan struct{}) // channel used to indicate when go routine has finnished go func(){ // go routine to send messages from channel for msg := range msgchan { token := c.publish("logs", 2, false, msg) // use qos2 is order is vital token.wait() // should check for errors here } close(done) // let main routine know we have finnished }() for i := 0; i < 5; i++ { text := fmt.sprintf("this is msg #%d!", i) msgchan <- text } close(msgchan) // this will stop the goroutine (when all messages processed) <-done // wait for publish go routine to complete
为什么不将消息发送分成一组工作人员呢?
类似这样的事情:
... const workerPoolSize = 10 // the number of workers you want to have wg := &sync.WaitGroup{} wCh := make(chan string) wg.Add(workerPoolSize) // you want to wait for 10 workers to finish the job // run workers in goroutines for i := 0; i < workerPoolSize; i++ { go func(wch <-chan string) { // get the data from the channel for text := range wch { c.Publish("logs", 0, false, text) token.Wait() } wg.Done() // worker says that he finishes the job }(wCh) } for i := 0; i < 5; i++ { // put the data to the channel wCh <- fmt.Sprintf("this is msg #%d!", i) } close(wCh) wg.Wait() // wait for all workers to finish ...
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《使用 GoRoutine 和通道在 Paho MQTT 上通过 websockets 发布消息的正确方法是什么?》文章吧,也可关注golang学习网公众号了解相关技术文章。
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习