Skip to content

Commit

Permalink
fixed heartbeat counters to be called when a new message is received
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Oct 7, 2019
1 parent 418f5ef commit 33b7f53
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions node/heartbeat/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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++

Expand All @@ -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 {
Expand Down

0 comments on commit 33b7f53

Please sign in to comment.