Use a single goroutine for stats aggregation

This commit is contained in:
Justin Li 2014-07-22 23:49:00 -04:00
parent ce2e335562
commit 7400792cc4

View file

@ -107,9 +107,6 @@ func New(chanSize int) *Stats {
} }
go s.handleEvents() go s.handleEvents()
go s.handlePeerEvents()
go s.handleTimings()
return s return s
} }
@ -143,54 +140,61 @@ func (s *Stats) RecordTiming(event int, duration time.Duration) {
} }
func (s *Stats) handleEvents() { func (s *Stats) handleEvents() {
for event := range s.events {
switch event {
case Announce:
s.Announces++
case Scrape:
s.Scrapes++
case NewTorrent:
s.TorrentsAdded++
case DeletedTorrent:
s.TorrentsRemoved++
case ReapedTorrent:
s.TorrentsReaped++
case AcceptedConnection:
s.ConnectionsAccepted++
s.OpenConnections++
case ClosedConnection:
s.OpenConnections--
case HandledRequest:
s.RequestsHandled++
case ErroredRequest:
s.RequestsErrored++
default:
panic("stats: RecordEvent called with an unknown event")
}
}
}
func (s *Stats) handlePeerEvents() {
for { for {
select { select {
case event := <-s.events:
s.handleEvent(event)
case event := <-s.ipv4PeerEvents: case event := <-s.ipv4PeerEvents:
s.handlePeerEvent(&s.IPv4Peers, event) s.handlePeerEvent(&s.IPv4Peers, event)
case event := <-s.ipv6PeerEvents: case event := <-s.ipv6PeerEvents:
s.handlePeerEvent(&s.IPv6Peers, event) s.handlePeerEvent(&s.IPv6Peers, event)
case duration := <-s.responseTimeEvents:
f := float64(duration) / float64(time.Millisecond)
s.ResponseTime.P50.AddSample(f)
s.ResponseTime.P90.AddSample(f)
s.ResponseTime.P95.AddSample(f)
} }
} }
} }
func (s *Stats) handleEvent(event int) {
switch event {
case Announce:
s.Announces++
case Scrape:
s.Scrapes++
case NewTorrent:
s.TorrentsAdded++
case DeletedTorrent:
s.TorrentsRemoved++
case ReapedTorrent:
s.TorrentsReaped++
case AcceptedConnection:
s.ConnectionsAccepted++
s.OpenConnections++
case ClosedConnection:
s.OpenConnections--
case HandledRequest:
s.RequestsHandled++
case ErroredRequest:
s.RequestsErrored++
default:
panic("stats: RecordEvent called with an unknown event")
}
}
func (s *Stats) handlePeerEvent(ps *PeerStats, event int) { func (s *Stats) handlePeerEvent(ps *PeerStats, event int) {
switch event { switch event {
case Completed: case Completed:
@ -226,18 +230,9 @@ func (s *Stats) handlePeerEvent(ps *PeerStats, event int) {
ps.SeedsCurrent-- ps.SeedsCurrent--
ps.Reaped++ ps.Reaped++
ps.Current-- ps.Current--
}
}
func (s *Stats) handleTimings() { default:
for { panic("stats: RecordPeerEvent called with an unknown event")
select {
case duration := <-s.responseTimeEvents:
f := float64(duration) / float64(time.Millisecond)
s.ResponseTime.P50.AddSample(f)
s.ResponseTime.P90.AddSample(f)
s.ResponseTime.P95.AddSample(f)
}
} }
} }