github.com/decred/dcrlnd@v0.7.6/watchtower/wtclient/stats.go (about)

     1  package wtclient
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  )
     7  
     8  // ClientStats is a collection of in-memory statistics of the actions the client
     9  // has performed since its creation.
    10  type ClientStats struct {
    11  	mu sync.Mutex
    12  
    13  	// NumTasksPending is the total number of backups that are pending to
    14  	// be acknowledged by all active and exhausted watchtower sessions.
    15  	NumTasksPending int
    16  
    17  	// NumTasksAccepted is the total number of backups made to all active
    18  	// and exhausted watchtower sessions.
    19  	NumTasksAccepted int
    20  
    21  	// NumTasksIneligible is the total number of backups that all active and
    22  	// exhausted watchtower sessions have failed to acknowledge.
    23  	NumTasksIneligible int
    24  
    25  	// NumSessionsAcquired is the total number of new sessions made to
    26  	// watchtowers.
    27  	NumSessionsAcquired int
    28  
    29  	// NumSessionsExhausted is the total number of watchtower sessions that
    30  	// have been exhausted.
    31  	NumSessionsExhausted int
    32  }
    33  
    34  // taskReceived increments the number to backup requests the client has received
    35  // from active channels.
    36  func (s *ClientStats) taskReceived() {
    37  	s.mu.Lock()
    38  	defer s.mu.Unlock()
    39  	s.NumTasksPending++
    40  }
    41  
    42  // taskAccepted increments the number of tasks that have been assigned to active
    43  // session queues, and are awaiting upload to a tower.
    44  func (s *ClientStats) taskAccepted() {
    45  	s.mu.Lock()
    46  	defer s.mu.Unlock()
    47  	s.NumTasksAccepted++
    48  	s.NumTasksPending--
    49  }
    50  
    51  // taskIneligible increments the number of tasks that were unable to satisfy the
    52  // active session queue's policy. These can potentially be retried later, but
    53  // typically this means that the balance created dust outputs, so it may not be
    54  // worth backing up at all.
    55  func (s *ClientStats) taskIneligible() {
    56  	s.mu.Lock()
    57  	defer s.mu.Unlock()
    58  	s.NumTasksIneligible++
    59  }
    60  
    61  // sessionAcquired increments the number of sessions that have been successfully
    62  // negotiated by the client during this execution.
    63  func (s *ClientStats) sessionAcquired() {
    64  	s.mu.Lock()
    65  	defer s.mu.Unlock()
    66  	s.NumSessionsAcquired++
    67  }
    68  
    69  // sessionExhausted increments the number of session that have become full as a
    70  // result of accepting backup tasks.
    71  func (s *ClientStats) sessionExhausted() {
    72  	s.mu.Lock()
    73  	defer s.mu.Unlock()
    74  	s.NumSessionsExhausted++
    75  }
    76  
    77  // String returns a human readable summary of the client's metrics.
    78  func (s *ClientStats) String() string {
    79  	s.mu.Lock()
    80  	defer s.mu.Unlock()
    81  	return fmt.Sprintf("tasks(received=%d accepted=%d ineligible=%d) "+
    82  		"sessions(acquired=%d exhausted=%d)", s.NumTasksPending,
    83  		s.NumTasksAccepted, s.NumTasksIneligible, s.NumSessionsAcquired,
    84  		s.NumSessionsExhausted)
    85  }
    86  
    87  // Copy returns a copy of the current stats.
    88  func (s *ClientStats) Copy() ClientStats {
    89  	s.mu.Lock()
    90  	defer s.mu.Unlock()
    91  	return ClientStats{
    92  		NumTasksPending:      s.NumTasksPending,
    93  		NumTasksAccepted:     s.NumTasksAccepted,
    94  		NumTasksIneligible:   s.NumTasksIneligible,
    95  		NumSessionsAcquired:  s.NumSessionsAcquired,
    96  		NumSessionsExhausted: s.NumSessionsExhausted,
    97  	}
    98  }