forked from eclipse/paho.golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
244 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package paho | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/eclipse/paho.golang/packets" | ||
) | ||
|
||
var ( | ||
ErrPacketNotFound = errors.New("packet not found") | ||
) | ||
|
||
type acksTracker struct { | ||
mx sync.Mutex | ||
order []packet | ||
} | ||
|
||
func (t *acksTracker) add(pb *packets.Publish) { | ||
t.mx.Lock() | ||
defer t.mx.Unlock() | ||
|
||
// @TODO don't just add, check for duplicates and skip if already acked | ||
t.order = append(t.order, packet{pb: pb}) | ||
} | ||
|
||
func (t *acksTracker) markAsAcked(pb *packets.Publish) error { | ||
t.mx.Lock() | ||
defer t.mx.Unlock() | ||
|
||
for k, v := range t.order { | ||
if pb.PacketID == v.pb.PacketID { | ||
t.order[k].acknowledged = true | ||
return nil | ||
} | ||
} | ||
|
||
return ErrPacketNotFound | ||
} | ||
|
||
func (t *acksTracker) flush(do func([]*packets.Publish)) { | ||
t.mx.Lock() | ||
defer t.mx.Unlock() | ||
|
||
var ( | ||
buf []*packets.Publish | ||
) | ||
for _, v := range t.order { | ||
if v.acknowledged { | ||
buf = append(buf, v.pb) | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
if len(buf) == 0 { | ||
return | ||
} | ||
|
||
do(buf) | ||
t.order = t.order[len(buf):] | ||
} | ||
|
||
// reset should be used upon disconnections | ||
func (t *acksTracker) reset() { | ||
t.mx.Lock() | ||
defer t.mx.Unlock() | ||
t.order = nil | ||
} | ||
|
||
type packet struct { | ||
pb *packets.Publish | ||
acknowledged bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package paho | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/eclipse/paho.golang/packets" | ||
) | ||
|
||
func TestAcksTracker(t *testing.T) { | ||
var ( | ||
at acksTracker | ||
p1 = &packets.Publish{PacketID: 1} | ||
p2 = &packets.Publish{PacketID: 2} | ||
p3 = &packets.Publish{PacketID: 3} | ||
p4 = &packets.Publish{PacketID: 4} // to test not found | ||
) | ||
|
||
t.Run("flush-without-acking", func(t *testing.T) { | ||
at.add(p1) | ||
at.add(p2) | ||
at.add(p3) | ||
require.Equal(t, ErrPacketNotFound, at.markAsAcked(p4)) | ||
at.flush(func(_ []*packets.Publish) { | ||
t.Fatal("flush should not call 'do' since no packets have been acknowledged so far") | ||
}) | ||
}) | ||
|
||
t.Run("ack-in-the-middle", func(t *testing.T) { | ||
require.NoError(t, at.markAsAcked(p3)) | ||
at.flush(func(_ []*packets.Publish) { | ||
t.Fatal("flush should not call 'do' since p1 and p2 have not been acknowledged yet") | ||
}) | ||
}) | ||
|
||
t.Run("idempotent-acking", func(t *testing.T) { | ||
require.NoError(t, at.markAsAcked(p3)) | ||
require.NoError(t, at.markAsAcked(p3)) | ||
require.NoError(t, at.markAsAcked(p3)) | ||
}) | ||
|
||
t.Run("ack-first", func(t *testing.T) { | ||
var flushCalled bool | ||
require.NoError(t, at.markAsAcked(p1)) | ||
at.flush(func(pbs []*packets.Publish) { | ||
require.Equal(t, []*packets.Publish{p1}, pbs, "Only p1 expected even though p3 was acked, p2 is still missing") | ||
flushCalled = true | ||
}) | ||
require.True(t, flushCalled) | ||
}) | ||
|
||
t.Run("ack-after-flush", func(t *testing.T) { | ||
var flushCalled bool | ||
require.NoError(t, at.markAsAcked(p2)) | ||
at.add(p4) // this should just be appended and not flushed (yet) | ||
at.flush(func(pbs []*packets.Publish) { | ||
require.Equal(t, []*packets.Publish{p2, p3}, pbs, "Only p2 and p3 expected, p1 was flushed in the previous call") | ||
flushCalled = true | ||
}) | ||
require.True(t, flushCalled) | ||
}) | ||
|
||
t.Run("ack-last", func(t *testing.T) { | ||
var flushCalled bool | ||
require.NoError(t, at.markAsAcked(p4)) | ||
at.flush(func(pbs []*packets.Publish) { | ||
require.Equal(t, []*packets.Publish{p4}, pbs, "Only p4 expected, the rest was flushed in previous calls") | ||
flushCalled = true | ||
}) | ||
require.True(t, flushCalled) | ||
}) | ||
|
||
t.Run("flush-after-acking-everything", func(t *testing.T) { | ||
at.flush(func(_ []*packets.Publish) { | ||
t.Fatal("no call to 'do' expected, we flushed all packets") | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters