github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/wireguard/service/stats_publisher.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package service
    19  
    20  import (
    21  	"sync"
    22  	"time"
    23  
    24  	"github.com/rs/zerolog/log"
    25  
    26  	"github.com/mysteriumnetwork/node/eventbus"
    27  	"github.com/mysteriumnetwork/node/services/wireguard/wgcfg"
    28  	"github.com/mysteriumnetwork/node/session/event"
    29  )
    30  
    31  type statsSupplier interface {
    32  	PeerStats() (wgcfg.Stats, error)
    33  }
    34  
    35  type statsPublisher struct {
    36  	done      chan struct{}
    37  	bus       eventbus.Publisher
    38  	frequency time.Duration
    39  	once      sync.Once
    40  }
    41  
    42  func newStatsPublisher(bus eventbus.Publisher, frequency time.Duration) statsPublisher {
    43  	return statsPublisher{
    44  		done:      make(chan struct{}),
    45  		bus:       bus,
    46  		frequency: frequency,
    47  	}
    48  }
    49  
    50  func (s *statsPublisher) start(sessionID string, supplier statsSupplier) {
    51  	for {
    52  		select {
    53  		case <-time.After(s.frequency):
    54  			stats, err := supplier.PeerStats()
    55  			if err != nil {
    56  				log.Warn().Err(err).Msg("Could not get peer statistics")
    57  				continue
    58  			}
    59  			s.bus.Publish(event.AppTopicDataTransferred, event.AppEventDataTransferred{
    60  				ID:   sessionID,
    61  				Up:   stats.BytesSent,
    62  				Down: stats.BytesReceived,
    63  			})
    64  		case <-s.done:
    65  			log.Info().Msgf("Stopped publishing statistics for session %s", sessionID)
    66  			return
    67  		}
    68  	}
    69  }
    70  
    71  func (s *statsPublisher) stop() {
    72  	s.once.Do(func() {
    73  		close(s.done)
    74  	})
    75  }