Skip to content

Commit

Permalink
Merge pull request ElementsProject#189 from nepet/202306-silence-nois…
Browse files Browse the repository at this point in the history
…y-peer-message-log

Silence noisy poll log
  • Loading branch information
nepet committed Jun 9, 2023
2 parents c03961d + b88bb84 commit 7fac467
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions poll/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,31 @@ type Service struct {
ctx context.Context
done context.CancelFunc

assets []string
messenger Messenger
policy Policy
peers PeerGetter
store Store
tmpStore map[string]string
removeDuration time.Duration
assets []string
messenger Messenger
policy Policy
peers PeerGetter
store Store
tmpStore map[string]string
removeDuration time.Duration
loggedDisconnect map[string]struct{}
}

func NewService(tickDuration time.Duration, removeDuration time.Duration, store Store, messenger Messenger, policy Policy, peers PeerGetter, allowedAssets []string) *Service {
clock := time.NewTicker(tickDuration)
ctx, done := context.WithCancel(context.Background())
s := &Service{
clock: clock,
ctx: ctx,
done: done,
assets: allowedAssets,
messenger: messenger,
policy: policy,
peers: peers,
store: store,
tmpStore: make(map[string]string),
removeDuration: removeDuration,
clock: clock,
ctx: ctx,
done: done,
assets: allowedAssets,
messenger: messenger,
policy: policy,
peers: peers,
store: store,
tmpStore: make(map[string]string),
removeDuration: removeDuration,
loggedDisconnect: make(map[string]struct{}),
}

s.messenger.AddMessageHandler(s.MessageHandler)
Expand Down Expand Up @@ -127,9 +129,7 @@ func (s *Service) Poll(peer string) {
return
}

if err := s.messenger.SendMessage(peer, msg, int(poll.MessageType())); err != nil {
log.Debugf("poll_service: could not send poll msg: %v", err)
}
s.sendMessage(peer, msg, int(poll.MessageType()))
}

func (s *Service) PollAllPeers() {
Expand All @@ -153,9 +153,7 @@ func (s *Service) RequestPoll(peer string) {
return
}

if err := s.messenger.SendMessage(peer, msg, int(request.MessageType())); err != nil {
log.Debugf("poll_service: could not send request_poll msg: %v", err)
}
s.sendMessage(peer, msg, int(request.MessageType()))
}

// RequestAllPeerPolls requests the poll message from
Expand All @@ -166,8 +164,8 @@ func (s *Service) RequestAllPeerPolls() {
}
}

// MessageHandler checks for the incomming messages
// type and takes the incomming payload to update the
// MessageHandler checks for the incoming messages
// type and takes the incoming payload to update the
// store.
func (s *Service) MessageHandler(peerId string, msgType string, payload []byte) error {
messageType, err := messages.HexStringToMessageType(msgType)
Expand Down Expand Up @@ -265,3 +263,22 @@ func (s *Service) GetPollFrom(peerId string) (*PollInfo, error) {

return nil, PollNotFoundErr(peerId)
}

func (s *Service) sendMessage(peer string, msg []byte, msgType int) {
if err := s.messenger.SendMessage(peer, msg, msgType); err != nil {
s.Lock()
defer s.Unlock()
// Only log message if not already logged an error on this peer. Mostly
// these errors will deal with disconnected peers so there is no need to
// continue logging if the peer is 'still' disconnected.
if _, seen := s.loggedDisconnect[peer]; !seen {
log.Debugf("poll_service: could not send msg to %s: %v", peer, err)
s.loggedDisconnect[peer] = struct{}{}
} else {
s.Lock()
defer s.Unlock()
// Message could be sent. Release peer from `loggedDisconnect`.
delete(s.loggedDisconnect, peer)
}
}
}

0 comments on commit 7fac467

Please sign in to comment.