Skip to content

Commit 0186dcb

Browse files
committed
Upgrade MQTT to mochi-mqtt v2.4.0
1 parent 59a0774 commit 0186dcb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2469
-954
lines changed

mqtt/clients.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// SPDX-FileCopyrightText: 2022 J. Blake / mochi-co
2+
// SPDX-FileCopyrightText: 2023 mochi-mqtt, mochi-co
33
// SPDX-FileContributor: mochi-co, wind
44

55
package mqtt
@@ -104,13 +104,13 @@ func (cl *Clients) GetByListener(id string) []*Client {
104104

105105
// Client contains information about a client known by the broker.
106106
type Client struct {
107-
ops *ops // ops provides a reference to server ops.
107+
Properties ClientProperties // client properties
108108
State ClientState // the operational state of the client.
109+
Net ClientConnection // network connection state of the client
109110
ID string // the client id.
110-
Net ClientConnection // network connection state of the clinet
111-
Properties ClientProperties // client properties
112-
InheritWay int // session inheritance way
111+
ops *ops // ops provides a reference to server ops.
113112
sync.RWMutex // mutex
113+
InheritWay int // session inheritance way
114114
}
115115

116116
// ClientConnection contains the connection transport and metadata for the client.
@@ -119,42 +119,42 @@ type ClientConnection struct {
119119
bconn *bufio.ReadWriter // a buffered net.Conn for reading packets
120120
Remote string // the remote address of the client
121121
Listener string // listener id of the client
122-
Inline bool // client is an inline programmetic client
122+
Inline bool // if true, the client is the built-in 'inline' embedded client
123123
}
124124

125125
// ClientProperties contains the properties which define the client behaviour.
126126
type ClientProperties struct {
127-
Username []byte
128-
Will Will
129127
Props packets.Properties
128+
Will Will
129+
Username []byte
130130
ProtocolVersion byte
131131
Clean bool
132132
}
133133

134134
// Will contains the last will and testament details for a client connection.
135135
type Will struct {
136-
TopicName string // -
137136
Payload []byte // -
138137
User []packets.UserProperty // -
138+
TopicName string // -
139139
Flag uint32 // 0,1
140140
WillDelayInterval uint32 // -
141141
Qos byte // -
142142
Retain bool // -
143143
}
144144

145-
// State tracks the state of the client.
145+
// ClientState tracks the state of the client.
146146
type ClientState struct {
147147
TopicAliases TopicAliases // a map of topic aliases
148148
stopCause atomic.Value // reason for stopping
149-
open context.Context // indicate that the client is open for packet exchange
150-
Subscriptions *Subscriptions // a map of the subscription filters a client maintains
151-
outbound chan *packets.Packet // queue for pending outbound packets
152149
Inflight *Inflight // a map of in-flight qos messages
153-
cancelOpen context.CancelFunc // cancel function for open context
150+
Subscriptions *Subscriptions // a map of the subscription filters a client maintains
154151
disconnected int64 // the time the client disconnected in unix time, for calculating expiry
152+
outbound chan *packets.Packet // queue for pending outbound packets
155153
endOnce sync.Once // only end once
156154
isTakenOver uint32 // used to identify orphaned clients
157155
packetID uint32 // the current highest packetID
156+
open context.Context // indicate that the client is open for packet exchange
157+
cancelOpen context.CancelFunc // cancel function for open context
158158
outboundQty int32 // number of messages currently in the outbound queue
159159
Keepalive uint16 // the number of seconds the connection can wait
160160
ServerKeepalive bool // keepalive was set by the server
@@ -200,7 +200,8 @@ func (cl *Client) WriteLoop() {
200200
select {
201201
case pk := <-cl.State.outbound:
202202
if err := cl.WritePacket(*pk); err != nil {
203-
cl.ops.log.Debug().Err(err).Str("client", cl.ID).Interface("packet", pk).Msg("failed publishing packet")
203+
// TODO : Figure out what to do with error
204+
cl.ops.log.Debug("failed publishing packet", "error", err, "client", cl.ID, "packet", pk)
204205
}
205206
atomic.AddInt32(&cl.State.outboundQty, -1)
206207
case <-cl.State.open.Done():
@@ -318,7 +319,7 @@ func (cl *Client) ResendInflightMessages(force bool) error {
318319
return nil
319320
}
320321

321-
// ClearInflights deletes all inflight messages for the client, eg. for a disconnected user with a clean session.
322+
// ClearInflights deletes all inflight messages for the client, e.g. for a disconnected user with a clean session.
322323
func (cl *Client) ClearInflights(now, maximumExpiry int64) []uint16 {
323324
deleted := []uint16{}
324325
for _, tk := range cl.State.Inflight.GetAll(false) {

mqtt/clients_test.go

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// SPDX-FileCopyrightText: 2022 mochi-co
2+
// SPDX-FileCopyrightText: 2022 mochi-mqtt, mochi-co
33
// SPDX-FileContributor: mochi-co
44

55
package mqtt
@@ -29,7 +29,7 @@ func newTestClient() (cl *Client, r net.Conn, w net.Conn) {
2929
cl = newClient(w, &ops{
3030
info: new(system.Info),
3131
hooks: new(Hooks),
32-
log: &logger,
32+
log: logger,
3333
options: &Options{
3434
Capabilities: &Capabilities{
3535
ReceiveMaximum: 10,
@@ -263,7 +263,7 @@ func TestClientNextPacketIDOverflow(t *testing.T) {
263263
cl.State.Inflight.internal[uint16(i)] = packets.Packet{}
264264
}
265265

266-
cl.State.packetID = uint32(cl.ops.options.Capabilities.maximumPacketID - 1)
266+
cl.State.packetID = cl.ops.options.Capabilities.maximumPacketID - 1
267267
i, err := cl.NextPacketID()
268268
require.NoError(t, err)
269269
require.Equal(t, cl.ops.options.Capabilities.maximumPacketID, i)
@@ -303,7 +303,7 @@ func TestClientResendInflightMessages(t *testing.T) {
303303
err := cl.ResendInflightMessages(true)
304304
require.NoError(t, err)
305305
time.Sleep(time.Millisecond)
306-
w.Close()
306+
_ = w.Close()
307307
}()
308308

309309
buf, err := io.ReadAll(r)
@@ -315,7 +315,7 @@ func TestClientResendInflightMessages(t *testing.T) {
315315
func TestClientResendInflightMessagesWriteFailure(t *testing.T) {
316316
pk1 := packets.TPacketData[packets.Publish].Get(packets.TPublishQos1Dup)
317317
cl, r, _ := newTestClient()
318-
r.Close()
318+
_ = r.Close()
319319

320320
cl.State.Inflight.Set(*pk1.Packet)
321321
require.Equal(t, 1, cl.State.Inflight.Len())
@@ -342,8 +342,8 @@ func TestClientReadFixedHeader(t *testing.T) {
342342

343343
defer cl.Stop(errClientStop)
344344
go func() {
345-
r.Write([]byte{packets.Connect << 4, 0x00})
346-
r.Close()
345+
_, _ = r.Write([]byte{packets.Connect << 4, 0x00})
346+
_ = r.Close()
347347
}()
348348

349349
fh := new(packets.FixedHeader)
@@ -357,8 +357,8 @@ func TestClientReadFixedHeaderDecodeError(t *testing.T) {
357357
defer cl.Stop(errClientStop)
358358

359359
go func() {
360-
r.Write([]byte{packets.Connect<<4 | 1<<1, 0x00, 0x00})
361-
r.Close()
360+
_, _ = r.Write([]byte{packets.Connect<<4 | 1<<1, 0x00, 0x00})
361+
_ = r.Close()
362362
}()
363363

364364
fh := new(packets.FixedHeader)
@@ -372,8 +372,8 @@ func TestClientReadFixedHeaderPacketOversized(t *testing.T) {
372372
defer cl.Stop(errClientStop)
373373

374374
go func() {
375-
r.Write(packets.TPacketData[packets.Publish].Get(packets.TPublishQos1Dup).RawBytes)
376-
r.Close()
375+
_, _ = r.Write(packets.TPacketData[packets.Publish].Get(packets.TPublishQos1Dup).RawBytes)
376+
_ = r.Close()
377377
}()
378378

379379
fh := new(packets.FixedHeader)
@@ -387,7 +387,7 @@ func TestClientReadFixedHeaderReadEOF(t *testing.T) {
387387
defer cl.Stop(errClientStop)
388388

389389
go func() {
390-
r.Close()
390+
_ = r.Close()
391391
}()
392392

393393
fh := new(packets.FixedHeader)
@@ -401,8 +401,8 @@ func TestClientReadFixedHeaderNoLengthTerminator(t *testing.T) {
401401
defer cl.Stop(errClientStop)
402402

403403
go func() {
404-
r.Write([]byte{packets.Connect << 4, 0xd5, 0x86, 0xf9, 0x9e, 0x01})
405-
r.Close()
404+
_, _ = r.Write([]byte{packets.Connect << 4, 0xd5, 0x86, 0xf9, 0x9e, 0x01})
405+
_ = r.Close()
406406
}()
407407

408408
fh := new(packets.FixedHeader)
@@ -414,7 +414,7 @@ func TestClientReadOK(t *testing.T) {
414414
cl, r, _ := newTestClient()
415415
defer cl.Stop(errClientStop)
416416
go func() {
417-
r.Write([]byte{
417+
_, _ = r.Write([]byte{
418418
packets.Publish << 4, 18, // Fixed header
419419
0, 5, // Topic Name - LSB+MSB
420420
'a', '/', 'b', '/', 'c', // Topic Name
@@ -424,7 +424,7 @@ func TestClientReadOK(t *testing.T) {
424424
'd', '/', 'e', '/', 'f', // Topic Name
425425
'y', 'e', 'a', 'h', // Payload
426426
})
427-
r.Close()
427+
_ = r.Close()
428428
}()
429429

430430
var pks []packets.Packet
@@ -499,10 +499,10 @@ func TestClientReadFixedHeaderError(t *testing.T) {
499499
cl, r, _ := newTestClient()
500500
defer cl.Stop(errClientStop)
501501
go func() {
502-
r.Write([]byte{
502+
_, _ = r.Write([]byte{
503503
packets.Publish << 4, 11, // Fixed header
504504
})
505-
r.Close()
505+
_ = r.Close()
506506
}()
507507

508508
cl.Net.bconn = nil
@@ -516,13 +516,13 @@ func TestClientReadReadHandlerErr(t *testing.T) {
516516
cl, r, _ := newTestClient()
517517
defer cl.Stop(errClientStop)
518518
go func() {
519-
r.Write([]byte{
519+
_, _ = r.Write([]byte{
520520
packets.Publish << 4, 11, // Fixed header
521521
0, 5, // Topic Name - LSB+MSB
522522
'd', '/', 'e', '/', 'f', // Topic Name
523523
'y', 'e', 'a', 'h', // Payload
524524
})
525-
r.Close()
525+
_ = r.Close()
526526
}()
527527

528528
err := cl.Read(func(cl *Client, pk packets.Packet) error {
@@ -536,13 +536,13 @@ func TestClientReadReadPacketOK(t *testing.T) {
536536
cl, r, _ := newTestClient()
537537
defer cl.Stop(errClientStop)
538538
go func() {
539-
r.Write([]byte{
539+
_, _ = r.Write([]byte{
540540
packets.Publish << 4, 11, // Fixed header
541541
0, 5,
542542
'd', '/', 'e', '/', 'f',
543543
'y', 'e', 'a', 'h',
544544
})
545-
r.Close()
545+
_ = r.Close()
546546
}()
547547

548548
fh := new(packets.FixedHeader)
@@ -573,7 +573,7 @@ func TestClientReadPacket(t *testing.T) {
573573
t.Run(tt.Desc, func(t *testing.T) {
574574
atomic.StoreInt64(&cl.ops.info.PacketsReceived, 0)
575575
go func() {
576-
r.Write(tt.RawBytes)
576+
_, _ = r.Write(tt.RawBytes)
577577
}()
578578

579579
fh := new(packets.FixedHeader)
@@ -600,7 +600,7 @@ func TestClientReadPacket(t *testing.T) {
600600

601601
func TestClientReadPacketInvalidTypeError(t *testing.T) {
602602
cl, _, _ := newTestClient()
603-
cl.Net.Conn.Close()
603+
_ = cl.Net.Conn.Close()
604604
_, err := cl.ReadPacket(&packets.FixedHeader{})
605605
require.Error(t, err)
606606
require.Contains(t, err.Error(), "invalid packet type")
@@ -624,7 +624,7 @@ func TestClientWritePacket(t *testing.T) {
624624
require.NoError(t, err, pkInfo, tt.Case, tt.Desc)
625625

626626
time.Sleep(2 * time.Millisecond)
627-
cl.Net.Conn.Close()
627+
_ = cl.Net.Conn.Close()
628628

629629
require.Equal(t, tt.RawBytes, <-o, pkInfo, tt.Case, tt.Desc)
630630

@@ -660,13 +660,13 @@ func TestClientReadPacketReadingError(t *testing.T) {
660660
cl, r, _ := newTestClient()
661661
defer cl.Stop(errClientStop)
662662
go func() {
663-
r.Write([]byte{
663+
_, _ = r.Write([]byte{
664664
0, 11, // Fixed header
665665
0, 5,
666666
'd', '/', 'e', '/', 'f',
667667
'y', 'e', 'a', 'h',
668668
})
669-
r.Close()
669+
_ = r.Close()
670670
}()
671671

672672
_, err := cl.ReadPacket(&packets.FixedHeader{
@@ -680,13 +680,13 @@ func TestClientReadPacketReadUnknown(t *testing.T) {
680680
cl, r, _ := newTestClient()
681681
defer cl.Stop(errClientStop)
682682
go func() {
683-
r.Write([]byte{
683+
_, _ = r.Write([]byte{
684684
0, 11, // Fixed header
685685
0, 5,
686686
'd', '/', 'e', '/', 'f',
687687
'y', 'e', 'a', 'h',
688688
})
689-
r.Close()
689+
_ = r.Close()
690690
}()
691691

692692
_, err := cl.ReadPacket(&packets.FixedHeader{
@@ -706,7 +706,7 @@ func TestClientWritePacketWriteNoConn(t *testing.T) {
706706

707707
func TestClientWritePacketWriteError(t *testing.T) {
708708
cl, _, _ := newTestClient()
709-
cl.Net.Conn.Close()
709+
_ = cl.Net.Conn.Close()
710710

711711
err := cl.WritePacket(*pkTable[1].Packet)
712712
require.Error(t, err)

mqtt/cmd/main.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// SPDX-FileCopyrightText: 2022 mochi-co
2+
// SPDX-FileCopyrightText: 2022 mochi-mqtt, mochi-co
33
// SPDX-FileContributor: mochi-co
44

55
package main
@@ -68,7 +68,8 @@ func main() {
6868
}()
6969

7070
<-done
71-
server.Log.Warn().Msg("caught signal, stopping...")
72-
server.Close()
73-
server.Log.Info().Msg("main.go finished")
71+
server.Log.Warn("caught signal, stopping...")
72+
_ = server.Close()
73+
server.Log.Info("main.go finished")
74+
7475
}

mqtt/examples/auth/basic/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// SPDX-FileCopyrightText: 2022 mochi-co
2+
// SPDX-FileCopyrightText: 2022 mochi-mqtt, mochi-co
33
// SPDX-FileContributor: mochi-co
44

55
package main
@@ -77,7 +77,7 @@ func main() {
7777
}()
7878

7979
<-done
80-
server.Log.Warn().Msg("caught signal, stopping...")
81-
server.Close()
82-
server.Log.Info().Msg("main.go finished")
80+
server.Log.Warn("caught signal, stopping...")
81+
_ = server.Close()
82+
server.Log.Info("main.go finished")
8383
}

mqtt/examples/auth/encoded/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// SPDX-FileCopyrightText: 2022 mochi-co
2+
// SPDX-FileCopyrightText: 2022 mochi-mqtt, mochi-co
33
// SPDX-FileContributor: mochi-co
44

55
package main
@@ -59,7 +59,7 @@ func main() {
5959
}()
6060

6161
<-done
62-
server.Log.Warn().Msg("caught signal, stopping...")
63-
server.Close()
64-
server.Log.Info().Msg("main.go finished")
62+
server.Log.Warn("caught signal, stopping...")
63+
_ = server.Close()
64+
server.Log.Info("main.go finished")
6565
}

mqtt/examples/benchmark/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// SPDX-FileCopyrightText: 2022 mochi-co
2+
// SPDX-FileCopyrightText: 2022 mochi-mqtt, mochi-co
33
// SPDX-FileContributor: mochi-co
44

55
package main
@@ -45,7 +45,7 @@ func main() {
4545
}()
4646

4747
<-done
48-
server.Log.Warn().Msg("caught signal, stopping...")
49-
server.Close()
50-
server.Log.Info().Msg("main.go finished")
48+
server.Log.Warn("caught signal, stopping...")
49+
_ = server.Close()
50+
server.Log.Info("main.go finished")
5151
}

0 commit comments

Comments
 (0)