-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
workers.go
92 lines (70 loc) · 2.14 KB
/
workers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package expvars
import (
"expvar"
"fmt"
"sync"
)
const (
// Top-level key for this expvar
workersExpvarKey = "Workers"
countExpvarKey = "Count"
instancesExpvarKey = "Instances"
)
var (
workerInstancesStats *expvar.Map
workersStats *expvar.Map
workersStatsLock sync.Mutex
)
// WorkerStats is the update object that will be used to populate
// the individual `worker.Worker` instance expvar stats
type WorkerStats struct {
Utilization float64
}
// String is used by expvar package to print the variables
func (ws *WorkerStats) String() string {
return fmt.Sprintf("{\"Utilization\": %.2f}", ws.Utilization)
}
func newWorkersExpvar(parent *expvar.Map) {
workersStatsLock.Lock()
defer workersStatsLock.Unlock()
workerInstancesStats = &expvar.Map{}
workersStats = &expvar.Map{}
workersStats.Add(countExpvarKey, 0)
workersStats.Set(instancesExpvarKey, workerInstancesStats)
parent.Set(workersExpvarKey, workersStats)
}
func resetWorkersExpvar(parent *expvar.Map) {
newWorkersExpvar(parent)
}
// SetWorkerStats is used to add individual worker stats or update them
// if they were already present
func SetWorkerStats(name string, ws *WorkerStats) {
workersStatsLock.Lock()
if workerInstancesStats.Get(name) == nil {
workersStats.Add(countExpvarKey, int64(1))
}
workerInstancesStats.Set(name, ws)
workersStatsLock.Unlock()
}
// DeleteWorkerStats is used to remove instance information about a worker and update
// the count of the curretly running workers
func DeleteWorkerStats(name string) {
workersStatsLock.Lock()
if workerInstancesStats.Get(name) != nil {
workersStats.Add(countExpvarKey, -1)
}
workerInstancesStats.Delete(name)
workersStatsLock.Unlock()
}
// GetWorkerCount is used to get the value of 'Workers'->'Count' expvar
func GetWorkerCount() int {
count := workersStats.Get(countExpvarKey)
if count == nil {
return 0
}
return int(count.(*expvar.Int).Value())
}