fix(simple): count upstream errors in stats

This commit is contained in:
Virgil 2026-04-04 19:44:19 +00:00
parent e92c6070be
commit 21fce78ffe
2 changed files with 67 additions and 3 deletions

View file

@ -171,9 +171,27 @@ func (s *SimpleSplitter) Upstreams() proxy.UpstreamStats {
s.mu.Lock()
defer s.mu.Unlock()
var stats proxy.UpstreamStats
stats.Active = uint64(len(s.active))
stats.Sleep = uint64(len(s.idle))
stats.Total = stats.Active + stats.Sleep
for _, mapper := range s.active {
if mapper == nil {
continue
}
if mapper.stopped || mapper.strategy == nil || !mapper.strategy.IsActive() {
stats.Error++
continue
}
stats.Active++
}
for _, mapper := range s.idle {
if mapper == nil {
continue
}
if mapper.stopped || mapper.strategy == nil || !mapper.strategy.IsActive() {
stats.Error++
continue
}
stats.Sleep++
}
stats.Total = stats.Active + stats.Sleep + stats.Error
return stats
}

View file

@ -66,3 +66,49 @@ func TestSimpleSplitter_OnLogin_Ugly(t *testing.T) {
t.Fatalf("expected expired mapper to remain idle until GC, got %d idle mappers", len(splitter.idle))
}
}
func TestSimpleSplitter_Upstreams_Good(t *testing.T) {
splitter := NewSimpleSplitter(&proxy.Config{ReuseTimeout: 30}, nil, func(listener pool.StratumListener) pool.Strategy {
return activeStrategy{}
})
splitter.active[1] = &SimpleMapper{id: 1, strategy: activeStrategy{}}
splitter.idle[2] = &SimpleMapper{id: 2, strategy: activeStrategy{}, idleAt: time.Now()}
stats := splitter.Upstreams()
if stats.Active != 1 {
t.Fatalf("expected one active upstream, got %d", stats.Active)
}
if stats.Sleep != 1 {
t.Fatalf("expected one sleeping upstream, got %d", stats.Sleep)
}
if stats.Error != 0 {
t.Fatalf("expected no error upstreams, got %d", stats.Error)
}
if stats.Total != 2 {
t.Fatalf("expected total upstreams to be 2, got %d", stats.Total)
}
}
func TestSimpleSplitter_Upstreams_Ugly(t *testing.T) {
splitter := NewSimpleSplitter(&proxy.Config{ReuseTimeout: 30}, nil, func(listener pool.StratumListener) pool.Strategy {
return activeStrategy{}
})
splitter.active[1] = &SimpleMapper{id: 1, strategy: activeStrategy{}, stopped: true}
splitter.idle[2] = &SimpleMapper{id: 2, strategy: activeStrategy{}, stopped: true, idleAt: time.Now()}
stats := splitter.Upstreams()
if stats.Active != 0 {
t.Fatalf("expected no active upstreams, got %d", stats.Active)
}
if stats.Sleep != 0 {
t.Fatalf("expected no sleeping upstreams, got %d", stats.Sleep)
}
if stats.Error != 2 {
t.Fatalf("expected both upstreams to be counted as error, got %d", stats.Error)
}
if stats.Total != 2 {
t.Fatalf("expected total upstreams to be 2, got %d", stats.Total)
}
}