From eec9f3f004f2eb3f5f205d6afcfa4acce0c458ed Mon Sep 17 00:00:00 2001 From: PandaX <18610165312@163.com> Date: Mon, 14 Apr 2025 02:22:09 +0000 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=BD=B1=E5=AD=90=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=92=E6=96=A5=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: PandaX <18610165312@163.com> --- pkg/shadow/shadow.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/shadow/shadow.go b/pkg/shadow/shadow.go index f980c7e..235d2e9 100644 --- a/pkg/shadow/shadow.go +++ b/pkg/shadow/shadow.go @@ -38,6 +38,7 @@ type DeviceShadow interface { type deviceShadow struct { m *sync.Map ticker *time.Ticker + mutex *sync.RWMutex // 添加互斥锁 handlerFunc OnlineChangeCallback //上下线执行的回调函数 ttl int } @@ -50,6 +51,7 @@ func init() { ticker: time.NewTicker(time.Second), ttl: 3600, // 默认1小时 handlerFunc: deviceHandler, + mutex: &sync.RWMutex{}, } go shadow.checkOnOff() DeviceShadowInstance = shadow @@ -79,6 +81,8 @@ func (d *deviceShadow) SetDeviceTTL(ttl int) { } func (d *deviceShadow) GetDevice(deviceName string) (device Device, err error) { + d.mutex.RLock() + defer d.mutex.RUnlock() if deviceAny, ok := d.m.Load(deviceName); ok { return deviceAny.(Device), nil } else {