2019-07-08 10:51:26 -06:00
|
|
|
// 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.
|
|
|
|
|
2019-08-17 21:26:28 -06:00
|
|
|
package stats
|
2019-07-08 10:51:26 -06:00
|
|
|
|
2019-07-17 17:17:43 -06:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2019-07-08 10:51:26 -06:00
|
|
|
var (
|
2019-08-14 10:51:42 -06:00
|
|
|
// TODO: Think about whether this is the right concurrency model, and what
|
|
|
|
// TODO: the queue length should be
|
2019-07-17 17:17:43 -06:00
|
|
|
workQueue = make(chan func(), 1000)
|
2019-07-08 10:51:26 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
go func() {
|
|
|
|
for task := range workQueue {
|
|
|
|
task()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-08-17 21:26:28 -06:00
|
|
|
// do adds a task to the list of things to work on in the background.
|
2019-07-08 10:51:26 -06:00
|
|
|
// 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.
|
2019-08-17 21:26:28 -06:00
|
|
|
func do(task func()) {
|
2019-07-17 17:17:43 -06:00
|
|
|
select {
|
|
|
|
case workQueue <- task:
|
|
|
|
default:
|
|
|
|
fmt.Fprint(os.Stderr, "work queue is full\n")
|
|
|
|
workQueue <- task
|
|
|
|
}
|
2019-07-08 10:51:26 -06:00
|
|
|
}
|