Skip to content

Commit 0303763

Browse files
eero-tPaweł Szulik
authored andcommitted
Clarify MetricSet thread-safety status
Both Add() and Set() methods modify the existing mapping. As Add() method is used only by Set() and Difference(), and in latter it's done thread-safely, make add private method and document that Set() is not thread-safe. Set() is used only by flag package through Value interface, which is documented to be called only once, i.e. it's safe.
1 parent ded4f2f commit 0303763

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

container/factory.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (ms MetricSet) Has(mk MetricKind) bool {
106106
return exists
107107
}
108108

109-
func (ms MetricSet) Add(mk MetricKind) {
109+
func (ms MetricSet) add(mk MetricKind) {
110110
ms[mk] = struct{}{}
111111
}
112112

@@ -119,14 +119,15 @@ func (ms MetricSet) String() string {
119119
return strings.Join(values, ",")
120120
}
121121

122+
// Not thread-safe, exported only for https://pkg.go.dev/flag#Value
122123
func (ms *MetricSet) Set(value string) error {
123124
*ms = MetricSet{}
124125
if value == "" {
125126
return nil
126127
}
127128
for _, metric := range strings.Split(value, ",") {
128129
if AllMetrics.Has(MetricKind(metric)) {
129-
(*ms).Add(MetricKind(metric))
130+
(*ms).add(MetricKind(metric))
130131
} else {
131132
return fmt.Errorf("unsupported metric %q specified", metric)
132133
}
@@ -138,7 +139,7 @@ func (ms MetricSet) Difference(ms1 MetricSet) MetricSet {
138139
result := MetricSet{}
139140
for kind := range ms {
140141
if !ms1.Has(kind) {
141-
result.Add(kind)
142+
result.add(kind)
142143
}
143144
}
144145
return result

0 commit comments

Comments
 (0)