|
@@ -4,6 +4,7 @@ import (
|
|
"context"
|
|
"context"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
+ "github.com/gogf/gf/os/grpool"
|
|
"runtime"
|
|
"runtime"
|
|
"runtime/debug"
|
|
"runtime/debug"
|
|
"sparrow/pkg/protocol"
|
|
"sparrow/pkg/protocol"
|
|
@@ -42,7 +43,8 @@ type DevSubHandle interface {
|
|
}
|
|
}
|
|
|
|
|
|
type MqttClient struct {
|
|
type MqttClient struct {
|
|
- client *client.MqttClient
|
|
|
|
|
|
+ client *client.MqttClient
|
|
|
|
+ handlePool *grpool.Pool
|
|
}
|
|
}
|
|
|
|
|
|
const (
|
|
const (
|
|
@@ -67,7 +69,8 @@ func newEmqClient(conf *client.MqttConfig) (SubDev, error) {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
return &MqttClient{
|
|
return &MqttClient{
|
|
- client: mc,
|
|
|
|
|
|
+ client: mc,
|
|
|
|
+ handlePool: grpool.New(1000),
|
|
}, nil
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
|
|
@@ -151,6 +154,7 @@ func (d *MqttClient) subConnectStatus(handle Handle) func(ctx context.Context, t
|
|
status := protocol.DevConnectStatus{
|
|
status := protocol.DevConnectStatus{
|
|
DeviceCode: msg.Username,
|
|
DeviceCode: msg.Username,
|
|
DeviceId: msg.Clientid,
|
|
DeviceId: msg.Clientid,
|
|
|
|
+ ClientIp: msg.Ipaddress,
|
|
}
|
|
}
|
|
if strings.HasSuffix(topic, "/connected") {
|
|
if strings.HasSuffix(topic, "/connected") {
|
|
status.Action = "LOGIN"
|
|
status.Action = "LOGIN"
|
|
@@ -166,15 +170,17 @@ func (d *MqttClient) subConnectStatus(handle Handle) func(ctx context.Context, t
|
|
func (d *MqttClient) subscribeWithFunc(cli mqtt.Client, topic string,
|
|
func (d *MqttClient) subscribeWithFunc(cli mqtt.Client, topic string,
|
|
handle func(ctx context.Context, topic string, payload []byte) error) error {
|
|
handle func(ctx context.Context, topic string, payload []byte) error) error {
|
|
return d.client.Subscribe(cli, topic, 1, func(c mqtt.Client, message mqtt.Message) {
|
|
return d.client.Subscribe(cli, topic, 1, func(c mqtt.Client, message mqtt.Message) {
|
|
- go func() {
|
|
|
|
- ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
|
|
|
|
- defer cancel()
|
|
|
|
- Recover(ctx)
|
|
|
|
- err := handle(ctx, message.Topic(), message.Payload())
|
|
|
|
- if err != nil {
|
|
|
|
- server.Log.Errorf("handle failure err :%s, topic :%v", err, topic)
|
|
|
|
- }
|
|
|
|
- }()
|
|
|
|
|
|
+ _ = d.handlePool.Add(func() {
|
|
|
|
+ go func() {
|
|
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
|
|
|
|
+ defer cancel()
|
|
|
|
+ Recover(ctx)
|
|
|
|
+ err := handle(ctx, message.Topic(), message.Payload())
|
|
|
|
+ if err != nil {
|
|
|
|
+ server.Log.Errorf("handle failure err :%s, topic :%v", err, topic)
|
|
|
|
+ }
|
|
|
|
+ }()
|
|
|
|
+ })
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|