From 33b7f53e1f02b7572183d940feb656f0c1525fbd Mon Sep 17 00:00:00 2001 From: iulianpascalau Date: Mon, 7 Oct 2019 19:42:33 +0300 Subject: [PATCH] fixed heartbeat counters to be called when a new message is received --- node/heartbeat/monitor.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/node/heartbeat/monitor.go b/node/heartbeat/monitor.go index 1d845587772..6550e9bedfc 100644 --- a/node/heartbeat/monitor.go +++ b/node/heartbeat/monitor.go @@ -169,6 +169,12 @@ func (m *Monitor) ProcessReceivedMessage(message p2p.MessageP2P) error { //message is validated, process should be done async, method can return nil go m.addHeartbeatMessageToMap(hbRecv) + go func() { + m.mutHeartbeatMessages.Lock() + defer m.mutHeartbeatMessages.Unlock() + + m.computeAllHeartbeatMessages() + }() return nil } @@ -239,16 +245,9 @@ func (m *Monitor) computeShardID(pubkey string) uint32 { func (m *Monitor) computeAllHeartbeatMessages() { counterActiveValidators := 0 counterConnectedNodes := 0 - for pk, v := range m.heartbeatMessages { - //TODO change here + for _, v := range m.heartbeatMessages { v.computeActive(m.timer.Now()) - hbDTO := m.convertToExportedStruct(v) - err := m.storer.SavePubkeyData([]byte(pk), &hbDTO) - if err != nil { - log.Warn(fmt.Sprintf("cannot save heartbeat to db: %s", err.Error())) - } - if v.isActive { counterConnectedNodes++ @@ -262,12 +261,23 @@ func (m *Monitor) computeAllHeartbeatMessages() { m.appStatusHandler.SetUInt64Value(core.MetricConnectedNodes, uint64(counterConnectedNodes)) } +func (m *Monitor) saveHeartbeats() { + for pk, v := range m.heartbeatMessages { + hbDTO := m.convertToExportedStruct(v) + err := m.storer.SavePubkeyData([]byte(pk), &hbDTO) + if err != nil { + log.Warn(fmt.Sprintf("cannot save heartbeat to db: %s", err.Error())) + } + } +} + // GetHeartbeats returns the heartbeat status func (m *Monitor) GetHeartbeats() []PubKeyHeartbeat { m.mutHeartbeatMessages.Lock() status := make([]PubKeyHeartbeat, len(m.heartbeatMessages)) m.computeAllHeartbeatMessages() + m.saveHeartbeats() idx := 0 for k, v := range m.heartbeatMessages {