accounting: clear finished transfer in stats-reset

In order to reduce memory usage `stats-reset` also
clears finished transfers.

Fixes #3734
This commit is contained in:
Maciej Zimnoch 2019-11-14 15:35:49 +01:00 committed by Nick Craig-Wood
parent 7cf056b2c2
commit f5443ac939
4 changed files with 45 additions and 6 deletions

View File

@ -521,8 +521,8 @@ The value for "eta" is null if an eta cannot be determined.
### core/stats-reset: Reset stats. {#core/stats-reset}
This clears counters and errors for all stats or specific stats group if group
is provided.
This clears counters, errors and finished transfers for all stats or specific
stats group if group is provided.
Parameters

View File

@ -627,6 +627,20 @@ func (s *StatsInfo) RemoveTransfer(transfer *Transfer) {
s.mu.Unlock()
}
// PruneAllTransfers removes all finished transfers.
func (s *StatsInfo) PruneAllTransfers() {
s.mu.Lock()
for i := 0; i < len(s.startedTransfers); i++ {
tr := s.startedTransfers[i]
if tr.IsDone() {
s.removeTransfer(tr, i)
// i'th element is removed, recover iterator to not skip next element.
i--
}
}
s.mu.Unlock()
}
// PruneTransfers makes sure there aren't too many old transfers by removing
// single finished transfer.
func (s *StatsInfo) PruneTransfers() {

View File

@ -59,8 +59,10 @@ func resetStats(ctx context.Context, in rc.Params) (rc.Params, error) {
}
if group != "" {
groups.get(group).ResetCounters()
groups.get(group).ResetErrors()
stats := groups.get(group)
stats.ResetCounters()
stats.ResetErrors()
stats.PruneAllTransfers()
} else {
groups.clear()
}
@ -190,8 +192,8 @@ Returns the following values:
Fn: resetStats,
Title: "Reset stats.",
Help: `
This clears counters and errors for all stats or specific stats group if group
is provided.
This clears counters, errors and finished transfers for all stats or specific
stats group if group is provided.
Parameters
@ -333,6 +335,7 @@ func (sg *statsGroups) clear() {
for _, stats := range sg.m {
stats.ResetErrors()
stats.ResetCounters()
stats.PruneAllTransfers()
}
sg.m = make(map[string]*StatsInfo)

View File

@ -431,3 +431,25 @@ func TestPruneTransfers(t *testing.T) {
})
}
}
func TestPruneAllTransfers(t *testing.T) {
const transfers = 10
s := NewStats()
for i := int64(1); i <= int64(transfers); i++ {
s.AddTransfer(&Transfer{
startedAt: time.Unix(i, 0),
completedAt: time.Unix(i+1, 0),
})
}
s.mu.Lock()
assert.Equal(t, transfers, len(s.startedTransfers))
s.mu.Unlock()
s.PruneAllTransfers()
s.mu.Lock()
assert.Empty(t, s.startedTransfers)
s.mu.Unlock()
}