internal/lsp/debug: guard rpc stats with a Mutex

This type was previously guarded implicitly by the global exporter
mutex. With that gone, we've started seeing panics due to races in
(*rpcs).Metric.

Guard it with a mutex.

Change-Id: I2a8c670ecfbaee9422e1f1d282b1fedb86b6a5e0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/222300
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rob Findley 2020-03-06 10:28:29 -05:00 committed by Robert Findley
parent a0897bacdd
commit 8287d625a0
2 changed files with 5 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import (
"log"
"net/http"
"sort"
"sync"
tlm "golang.org/x/tools/internal/lsp/telemetry"
"golang.org/x/tools/internal/telemetry"
@ -42,6 +43,7 @@ var rpcTmpl = template.Must(template.Must(baseTemplate.Clone()).Parse(`
`))
type rpcs struct {
mu sync.Mutex
Inbound []*rpcStats
Outbound []*rpcStats
}
@ -91,6 +93,8 @@ type rpcCodeBucket struct {
}
func (r *rpcs) Metric(ctx context.Context, data telemetry.MetricData) {
r.mu.Lock()
defer r.mu.Unlock()
for i, group := range data.Groups() {
set := &r.Inbound
if group.Get(tlm.RPCDirection) == tlm.Outbound {

View File

@ -4,7 +4,7 @@
package telemetry
// Data represents a single point in the time series of a metric.
// MetricData represents a single point in the time series of a metric.
// This provides the common interface to all metrics no matter their data
// format.
// To get the actual values for the metric you must type assert to a concrete