Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ func (r *router) matchAndDispatch(messages <-chan *packets.PublishPacket, order
}

go func() { // Main go routine handling inbound messages
var handlers []MessageHandler
for message := range messages {
// DEBUG.Println(ROU, "matchAndDispatch received message")
sent := false
r.RLock()
m := messageFromPublish(message, ackFunc(ackInChan, client.persist, message))
var handlers []MessageHandler
for e := r.routes.Front(); e != nil; e = e.Next() {
if e.Value.(*route).match(message.TopicName) {
if order {
Expand Down Expand Up @@ -214,11 +214,14 @@ func (r *router) matchAndDispatch(messages <-chan *packets.PublishPacket, order
}
}
r.RUnlock()
for _, handler := range handlers {
handler(client, m)
if !client.options.AutoAckDisabled {
m.Ack()
if order {
for _, handler := range handlers {
handler(client, m)
if !client.options.AutoAckDisabled {
m.Ack()
}
}
handlers = handlers[:0]
}
// DEBUG.Println(ROU, "matchAndDispatch handled message")
}
Expand Down
45 changes: 45 additions & 0 deletions unit_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package mqtt

import (
"sync"
"testing"
"time"

Expand Down Expand Up @@ -354,3 +355,47 @@ func Test_SharedSubscription_MatchAndDispatch(t *testing.T) {
}

}

func Benchmark_MatchAndDispatch(b *testing.B) {
calledback := make(chan bool, 1)

cb := func(c Client, m Message) {
calledback <- true
}

pub := packets.NewControlPacket(packets.Publish).(*packets.PublishPacket)
pub.TopicName = "a"
pub.Payload = []byte("foo")

msgs := make(chan *packets.PublishPacket, 1)

router := newRouter()
router.addRoute("a", cb)

var wg sync.WaitGroup
wg.Add(1)

stopped := make(chan bool)
go func() {
wg.Done() // started
<-router.matchAndDispatch(msgs, true, &client{oboundP: make(chan *PacketAndToken, 100)})
stopped <- true
}()

wg.Wait()
b.ResetTimer()

for i := 0; i < b.N; i++ {
msgs <- pub
<-calledback
}

close(msgs)

select {
case <-stopped:
break
case <-time.After(time.Second):
b.Errorf("matchAndDispatch should have exited")
}
}