mirror of
https://github.com/mochi-mqtt/server.git
synced 2025-10-05 16:17:01 +08:00
Minimize client lock duration (#223)
* Minimize client lock duration * Fix server option example
This commit is contained in:
@@ -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,
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
23
clients.go
23
clients.go
@@ -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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user