github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/connectivity/status_storage.go (about)

     1  /*
     2   * Copyright (C) 2019 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 connectivity
    19  
    20  import (
    21  	"sort"
    22  	"sync"
    23  	"time"
    24  
    25  	"github.com/mysteriumnetwork/node/identity"
    26  )
    27  
    28  // maxEntriesKeepPeriod describes how long entries are kept in memory storage.
    29  // Older than this duration entries are removed on insert.
    30  const maxEntriesKeepDuration = time.Hour * 24 * 30
    31  
    32  // StatusStorage is responsible for status storage operations.
    33  type StatusStorage interface {
    34  	GetAllStatusEntries() []StatusEntry
    35  	AddStatusEntry(msg StatusEntry)
    36  }
    37  
    38  // StatusEntry describes status entry.
    39  type StatusEntry struct {
    40  	PeerID       identity.Identity
    41  	SessionID    string
    42  	StatusCode   StatusCode
    43  	Message      string
    44  	CreatedAtUTC time.Time
    45  }
    46  
    47  // NewStatusStorage returns new StatusStorage instance.
    48  func NewStatusStorage() StatusStorage {
    49  	return &statusStorage{}
    50  }
    51  
    52  type statusStorage struct {
    53  	entries    []StatusEntry
    54  	entriesMux sync.RWMutex
    55  }
    56  
    57  func (s *statusStorage) GetAllStatusEntries() []StatusEntry {
    58  	s.entriesMux.RLock()
    59  	defer s.entriesMux.RUnlock()
    60  
    61  	res := make([]StatusEntry, len(s.entries))
    62  	copy(res, s.entries)
    63  
    64  	// Sort by CreatedAtUTC descending to show newest entries first.
    65  	sort.Slice(res, func(i, j int) bool {
    66  		return res[i].CreatedAtUTC.After(res[j].CreatedAtUTC)
    67  	})
    68  	return res
    69  }
    70  
    71  func (s *statusStorage) AddStatusEntry(msg StatusEntry) {
    72  	s.entriesMux.Lock()
    73  	defer s.entriesMux.Unlock()
    74  
    75  	// Remove old entries which are older that maxEntriesKeepDuration.
    76  	var res []StatusEntry
    77  	minValidEntryTime := time.Now().UTC().Add(-maxEntriesKeepDuration)
    78  	for _, entry := range s.entries {
    79  		if entry.CreatedAtUTC.After(minValidEntryTime) {
    80  			res = append(res, entry)
    81  		}
    82  	}
    83  	s.entries = res
    84  
    85  	// Add new entry.
    86  	s.entries = append(s.entries, msg)
    87  }