accounting: add context.Context #3257 #4685

This commit is contained in:
Nick Craig-Wood
2020-11-05 16:59:59 +00:00
parent e3fe31f7cb
commit 1fb6ad700f
29 changed files with 138 additions and 109 deletions

View File

@@ -59,10 +59,10 @@ func rcRemoteStats(ctx context.Context, in rc.Params) (rc.Params, error) {
return rc.Params{}, err
}
if group != "" {
return StatsGroup(group).RemoteStats()
return StatsGroup(ctx, group).RemoteStats()
}
return groups.sum().RemoteStats()
return groups.sum(ctx).RemoteStats()
}
func init() {
@@ -129,9 +129,9 @@ func rcTransferredStats(ctx context.Context, in rc.Params) (rc.Params, error) {
out := make(rc.Params)
if group != "" {
out["transferred"] = StatsGroup(group).Transferred()
out["transferred"] = StatsGroup(ctx, group).Transferred()
} else {
out["transferred"] = groups.sum().Transferred()
out["transferred"] = groups.sum(ctx).Transferred()
}
return out, nil
@@ -265,28 +265,28 @@ func Stats(ctx context.Context) *StatsInfo {
if !ok {
return GlobalStats()
}
return StatsGroup(group)
return StatsGroup(ctx, group)
}
// StatsGroup gets stats by group name.
func StatsGroup(group string) *StatsInfo {
func StatsGroup(ctx context.Context, group string) *StatsInfo {
stats := groups.get(group)
if stats == nil {
return NewStatsGroup(group)
return NewStatsGroup(ctx, group)
}
return stats
}
// GlobalStats returns special stats used for global accounting.
func GlobalStats() *StatsInfo {
return StatsGroup(globalStats)
return StatsGroup(context.Background(), globalStats)
}
// NewStatsGroup creates new stats under named group.
func NewStatsGroup(group string) *StatsInfo {
stats := NewStats()
func NewStatsGroup(ctx context.Context, group string) *StatsInfo {
stats := NewStats(ctx)
stats.group = group
groups.set(group, stats)
groups.set(ctx, group, stats)
return stats
}
@@ -305,7 +305,7 @@ func newStatsGroups() *statsGroups {
}
// set marks the stats as belonging to a group
func (sg *statsGroups) set(group string, stats *StatsInfo) {
func (sg *statsGroups) set(ctx context.Context, group string, stats *StatsInfo) {
sg.mu.Lock()
defer sg.mu.Unlock()
@@ -343,11 +343,11 @@ func (sg *statsGroups) names() []string {
}
// sum returns aggregate stats that contains summation of all groups.
func (sg *statsGroups) sum() *StatsInfo {
func (sg *statsGroups) sum(ctx context.Context) *StatsInfo {
sg.mu.Lock()
defer sg.mu.Unlock()
sum := NewStats()
sum := NewStats(ctx)
for _, stats := range sg.m {
stats.mu.RLock()
{