]> Cypherpunks repositories - gostls13.git/commitdiff
expvar: add (*Int).Set
authorSam Thorogood <thorogood@google.com>
Mon, 11 Oct 2010 17:14:07 +0000 (13:14 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 11 Oct 2010 17:14:07 +0000 (13:14 -0400)
R=golang-dev, adg, rsc
CC=golang-dev
https://golang.org/cl/2336044

src/pkg/expvar/expvar.go
src/pkg/expvar/expvar_test.go

index 9435481dbc47cb40f6e368b7d82a7cd47806a518..7a5244143918d0089758ef94d2f11fcf588c8f94 100644 (file)
@@ -6,6 +6,8 @@
 // such as operation counters in servers. It exposes these variables via
 // HTTP at /debug/vars in JSON format.
 //
+// Operations to set or modify these public variables are atomic.
+//
 // In addition to adding the HTTP handler, this package registers the
 // following variables:
 //
@@ -50,6 +52,12 @@ func (v *Int) Add(delta int64) {
        v.i += delta
 }
 
+func (v *Int) Set(value int64) {
+       v.mu.Lock()
+       defer v.mu.Unlock()
+       v.i = value
+}
+
 // Map is a string-to-Var map variable, and satisfies the Var interface.
 type Map struct {
        m  map[string]Var
index dc173b9a6bb54f71b53cc8a390588c48c6ab89f2..3dfc55af36406b18f1cad8580433491c2b9c0023 100644 (file)
@@ -27,6 +27,11 @@ func TestInt(t *testing.T) {
        if s := reqs.String(); s != "4" {
                t.Errorf("reqs.String() = %q, want \"4\"", s)
        }
+
+       reqs.Set(-2)
+       if reqs.i != -2 {
+               t.Errorf("reqs.i = %v, want -2", reqs.i)
+       }
 }
 
 func TestString(t *testing.T) {