1
0
mirror of https://github.com/golang/go synced 2024-11-19 04:24:39 -07:00
go/internal/telemetry/stats/worker.go
Ian Cottrell d9ab56aa29 internal/telemetry: change concurrency model
This changes to use a mutex and directly execute the less performance
sensitive telemetry calls (tracing and logging) and then uses a submission
queue only for stats adjustments as those are much more sensitive (but it
should also be easier to keep up with them in bursts)

Fixes golang/go#33692

Change-Id: Ia59a8975f21dfbfcf115be1f1d11b097be8dd9c8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190737
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2019-08-20 03:27:56 +00:00

41 lines
1012 B
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package stats
import (
"fmt"
"os"
)
var (
// TODO: Think about whether this is the right concurrency model, and what
// TODO: the queue length should be
workQueue = make(chan func(), 1000)
)
func init() {
go func() {
for task := range workQueue {
task()
}
}()
}
// do adds a task to the list of things to work on in the background.
// All tasks will be handled in submission order, and no two tasks will happen
// concurrently so they do not need to do any kind of locking.
// It is safe however to call Do concurrently.
// No promises are made about when the tasks will be performed.
// This function may block, but in general it will return very quickly and
// before the task has been run.
func do(task func()) {
select {
case workQueue <- task:
default:
fmt.Fprint(os.Stderr, "work queue is full\n")
workQueue <- task
}
}