From e16164f63a04cf81049c927a753ab670fc7f1f5b Mon Sep 17 00:00:00 2001 From: hantmac Date: Tue, 24 Dec 2019 16:11:24 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E9=87=8D=E5=A4=8D=E6=B3=A8=E5=86=8C=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/services/node.go | 45 +++++++++++++++++++++++----------------- backend/utils/helpers.go | 18 ++++++++++++++++ 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/backend/services/node.go b/backend/services/node.go index e6c2ac08..edaf9f16 100644 --- a/backend/services/node.go +++ b/backend/services/node.go @@ -167,27 +167,34 @@ func UpdateNodeData() { debug.PrintStack() return } - // 构造节点数据 - data := Data{ - Key: key, - Mac: mac, - Ip: ip, - Master: model.IsMaster(), - UpdateTs: time.Now(), - UpdateTsUnix: time.Now().Unix(), + + //先获取所有Redis的nodekey + list, _ := database.RedisClient.HKeys("nodes") + + if i := utils.Contains(list, key); i != -1 { + // 构造节点数据 + data := Data{ + Key: key, + Mac: mac, + Ip: ip, + Master: model.IsMaster(), + UpdateTs: time.Now(), + UpdateTsUnix: time.Now().Unix(), + } + + // 注册节点到Redis + dataBytes, err := json.Marshal(&data) + if err != nil { + log.Errorf(err.Error()) + debug.PrintStack() + return + } + if err := database.RedisClient.HSet("nodes", key, utils.BytesToString(dataBytes)); err != nil { + log.Errorf(err.Error()) + return + } } - // 注册节点到Redis - dataBytes, err := json.Marshal(&data) - if err != nil { - log.Errorf(err.Error()) - debug.PrintStack() - return - } - if err := database.RedisClient.HSet("nodes", key, utils.BytesToString(dataBytes)); err != nil { - log.Errorf(err.Error()) - return - } } func MasterNodeCallback(message redis.Message) (err error) { diff --git a/backend/utils/helpers.go b/backend/utils/helpers.go index 8a80e9e8..ce29e4b7 100644 --- a/backend/utils/helpers.go +++ b/backend/utils/helpers.go @@ -6,6 +6,7 @@ import ( "github.com/apex/log" "github.com/gomodule/redigo/redis" "io" + "reflect" "runtime/debug" "unsafe" ) @@ -40,3 +41,20 @@ func Close(c io.Closer) { //log.WithError(err).Error("关闭资源文件失败。") } } + +func Contains(array interface{}, val interface{}) (index int) { + index = -1 + switch reflect.TypeOf(array).Kind() { + case reflect.Slice: + { + s := reflect.ValueOf(array) + for i := 0; i < s.Len(); i++ { + if reflect.DeepEqual(val, s.Index(i).Interface()) { + index = i + return + } + } + } + } + return +} \ No newline at end of file From 8888e71d1b40b32ba8a5503779a53f0104414a87 Mon Sep 17 00:00:00 2001 From: hantmac Date: Tue, 24 Dec 2019 16:44:54 +0800 Subject: [PATCH 2/3] fix --- backend/services/node.go | 2 +- backend/utils/helpers.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/services/node.go b/backend/services/node.go index edaf9f16..d14ce4ae 100644 --- a/backend/services/node.go +++ b/backend/services/node.go @@ -171,7 +171,7 @@ func UpdateNodeData() { //先获取所有Redis的nodekey list, _ := database.RedisClient.HKeys("nodes") - if i := utils.Contains(list, key); i != -1 { + if i := utils.Contains(list, key); i == false { // 构造节点数据 data := Data{ Key: key, diff --git a/backend/utils/helpers.go b/backend/utils/helpers.go index ce29e4b7..e181c66c 100644 --- a/backend/utils/helpers.go +++ b/backend/utils/helpers.go @@ -42,19 +42,19 @@ func Close(c io.Closer) { } } -func Contains(array interface{}, val interface{}) (index int) { - index = -1 +func Contains(array interface{}, val interface{}) (fla bool) { + fla = false switch reflect.TypeOf(array).Kind() { case reflect.Slice: { s := reflect.ValueOf(array) for i := 0; i < s.Len(); i++ { if reflect.DeepEqual(val, s.Index(i).Interface()) { - index = i + fla = true return } } } } return -} \ No newline at end of file +} From 5fc2370f2402f0e233c31a9271fd97d19b1f1602 Mon Sep 17 00:00:00 2001 From: yaziming Date: Wed, 25 Dec 2019 14:02:45 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix(backend):=20=E4=BF=AE=E5=A4=8Dredis?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=B1=A0=E8=AE=A2=E9=98=85=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E6=97=B6=EF=BC=8C=E5=9B=A0=E4=B8=BAping?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E4=BC=98=E5=85=88=E8=8E=B7=E5=8F=96=E5=88=B0?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E9=80=A0=E6=88=90chain=E6=8F=90=E5=89=8D?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E5=BC=95=E5=8F=91panic=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/database/pubsub.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/database/pubsub.go b/backend/database/pubsub.go index 7f647cda..444ce91a 100644 --- a/backend/database/pubsub.go +++ b/backend/database/pubsub.go @@ -58,9 +58,9 @@ func (r *Redis) subscribe(ctx context.Context, consume ConsumeFunc, channel ...s } done <- nil case <-tick.C: - //fmt.Printf("ping message \n") if err := psc.Ping(""); err != nil { - done <- err + fmt.Printf("ping message error: %s \n", err) + //done <- err } case err := <-done: close(done)