OSDN Git Service

fk
authorHAOYUatHZ <haoyu@protonmail.com>
Mon, 19 Aug 2019 03:17:18 +0000 (11:17 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Mon, 19 Aug 2019 03:17:18 +0000 (11:17 +0800)
toolbar/precog/database/orm/node.go
toolbar/precog/database/orm/node_liveness.go
toolbar/precog/monitor/monitor.go
toolbar/precog/monitor/stats.go

index 151ad56..84fbc0c 100644 (file)
@@ -5,6 +5,7 @@ import (
 )
 
 type Node struct {
+       ID                       uint16 `gorm:"primary_key"`
        Alias                    string
        Xpub                     string
        PublicKey                string
index 20e1f65..19bbfea 100644 (file)
@@ -6,6 +6,7 @@ import (
 )
 
 type NodeLiveness struct {
+       ID            uint64 `gorm:"primary_key"`
        NodeID        uint16
        PingTimes     uint64
        PongTimes     uint64
index 09e7a70..5fdacd2 100644 (file)
@@ -41,7 +41,7 @@ type monitor struct {
 // TODO: set myself as SPV?
 func NewMonitor(cfg *config.Config, db *gorm.DB) *monitor {
        //TODO: for test
-       cfg.CheckFreqSeconds = 10
+       cfg.CheckFreqSeconds = 15
 
        dbPath, err := makePath()
        if err != nil {
@@ -161,7 +161,7 @@ func (m *monitor) prepareReactors(peers *peers.PeerSet) error {
 
 // TODO:
 // 现象是,时间区间过小时,  会一直有 dial ,但是不能 send业务层 msg
-// 还不确定是不是死锁,时间调大一点比如10s 就可以正确运行
+// 还不确定是不是死锁,时间调大一点比如15s 就可以正确运行
 // 想法,自己再另外加锁,或者找到锁住的真正原因
 func (m *monitor) checkStatusRoutine() {
        peers := peers.NewPeerSet(m.sw)
index cf1250d..0effb7b 100644 (file)
@@ -1,12 +1,14 @@
 package monitor
 
 import (
+       "database/sql"
        "fmt"
 
        "github.com/jinzhu/gorm"
 
        "github.com/vapor/crypto/ed25519/chainkd"
        "github.com/vapor/netsync/peers"
+       "github.com/vapor/toolbar/precog/common"
        "github.com/vapor/toolbar/precog/config"
        "github.com/vapor/toolbar/precog/database/orm"
 )
@@ -62,5 +64,20 @@ func (m *monitor) savePeerInfo(peerInfo *peers.PeerInfo) error {
                return err
        }
 
+       ormNodeLiveness := &orm.NodeLiveness{
+               NodeID:        ormNode.ID,
+               BestHeight:    ormNode.BestHeight,
+               AvgLantencyMS: sql.NullInt64{Int64: 1, Valid: true},
+               // PingTimes     uint64
+               // PongTimes     uint64
+       }
+       if err := m.db.Model(&orm.NodeLiveness{}).Where("node_id = ?, status is NOT ?", ormNode.ID, common.NodeOfflineStatus).
+               UpdateColumn(&orm.NodeLiveness{
+                       BestHeight:    ormNodeLiveness.BestHeight,
+                       AvgLantencyMS: ormNodeLiveness.AvgLantencyMS,
+               }).FirstOrCreate(ormNodeLiveness).Error; err != nil {
+               return err
+       }
+
        return nil
 }