Minimize client lock duration (#223)

* Minimize client lock duration
* Fix server option example
This commit is contained in:
thedevop
2023-05-18 12:01:35 -07:00
committed by GitHub
parent 6acd775a6b
commit 565e07747e
2 changed files with 15 additions and 14 deletions

View File

@@ -136,13 +136,13 @@ A number of configurable options are available which can be used to alter the be
```go ```go
server := mqtt.New(&mqtt.Options{ server := mqtt.New(&mqtt.Options{
Capabilities: mqtt.Capabilities{ Capabilities: mqtt.Capabilities{
ClientNetWriteBufferSize: 4096,
ClientNetReadBufferSize: 4096,
MaximumSessionExpiryInterval: 3600, MaximumSessionExpiryInterval: 3600,
Compatibilities: mqtt.Compatibilities{ Compatibilities: mqtt.Compatibilities{
ObscureNotAuthorized: true, ObscureNotAuthorized: true,
}, },
}, },
ClientNetWriteBufferSize: 4096,
ClientNetReadBufferSize: 4096,
SysTopicResendInterval: 10, SysTopicResendInterval: 10,
}) })
``` ```

View File

@@ -481,6 +481,14 @@ func (cl *Client) ReadPacket(fh *packets.FixedHeader) (pk packets.Packet, err er
// WritePacket encodes and writes a packet to the client. // WritePacket encodes and writes a packet to the client.
func (cl *Client) WritePacket(pk packets.Packet) error { func (cl *Client) WritePacket(pk packets.Packet) error {
if cl.Closed() {
return ErrConnectionClosed
}
if cl.Net.Conn == nil {
return nil
}
if pk.Expiry > 0 { if pk.Expiry > 0 {
pk.Properties.MessageExpiryInterval = uint32(pk.Expiry - time.Now().Unix()) // [MQTT-3.3.2-6] pk.Properties.MessageExpiryInterval = uint32(pk.Expiry - time.Now().Unix()) // [MQTT-3.3.2-6]
} }
@@ -544,19 +552,12 @@ func (cl *Client) WritePacket(pk packets.Packet) error {
return packets.ErrPacketTooLarge // [MQTT-3.1.2-24] [MQTT-3.1.2-25] return packets.ErrPacketTooLarge // [MQTT-3.1.2-24] [MQTT-3.1.2-25]
} }
nb := net.Buffers{buf.Bytes()}
n, err := func() (int64, error) {
cl.Lock() cl.Lock()
defer cl.Unlock() defer cl.Unlock()
return nb.WriteTo(cl.Net.Conn)
if cl.Closed() { }()
return ErrConnectionClosed
}
if cl.Net.Conn == nil {
return nil
}
nb := net.Buffers{buf.Bytes()}
n, err := nb.WriteTo(cl.Net.Conn)
if err != nil { if err != nil {
return err return err
} }