Commit 715588f1 authored by David Symonds's avatar David Symonds

expvar: revise API.

Nuke RemoveAll from the public API.
Replace Iter functions with Do functions.

Fixes #2852.

R=rsc, r
CC=golang-dev
https://golang.org/cl/5622055
parent f25a3873
...@@ -913,6 +913,23 @@ to be implemented in the future. ...@@ -913,6 +913,23 @@ to be implemented in the future.
No changes will be needed. No changes will be needed.
</p> </p>
<h3 id="expvar">The expvar package</h3>
<p>
In Go 1, the <code>RemoveAll</code> function has been removed.
The <code>Iter</code> function and Iter method on <code>*Map</code> have
been replaced by
<a href="/pkg/expvar/#Do"><code>Do</code></a>
and
<a href="/pkg/expvar/#Map.Do"><code>(*Map).Do</code></a>.
</p>
<p>
<em>Updating</em>:
Most code using <code>expvar</code> will not need changing. The rare code that used
<code>Iter</code> can be updated to pass a closure to Do to achieve the same effect.
</p>
<h3 id="flag">The flag package</h3> <h3 id="flag">The flag package</h3>
<p> <p>
......
...@@ -817,6 +817,23 @@ to be implemented in the future. ...@@ -817,6 +817,23 @@ to be implemented in the future.
No changes will be needed. No changes will be needed.
</p> </p>
<h3 id="expvar">The expvar package</h3>
<p>
In Go 1, the <code>RemoveAll</code> function has been removed.
The <code>Iter</code> function and Iter method on <code>*Map</code> have
been replaced by
<a href="/pkg/expvar/#Do"><code>Do</code></a>
and
<a href="/pkg/expvar/#Map.Do"><code>(*Map).Do</code></a>.
</p>
<p>
<em>Updating</em>:
Most code using <code>expvar</code> will not need changing. The rare code that used
<code>Iter</code> can be updated to pass a closure to Do to achieve the same effect.
</p>
<h3 id="flag">The flag package</h3> <h3 id="flag">The flag package</h3>
<p> <p>
......
...@@ -83,7 +83,7 @@ func (v *Float) Set(value float64) { ...@@ -83,7 +83,7 @@ func (v *Float) Set(value float64) {
// Map is a string-to-Var map variable that satisfies the Var interface. // Map is a string-to-Var map variable that satisfies the Var interface.
type Map struct { type Map struct {
m map[string]Var m map[string]Var
mu sync.Mutex mu sync.RWMutex
} }
// KeyValue represents a single entry in a Map. // KeyValue represents a single entry in a Map.
...@@ -93,8 +93,8 @@ type KeyValue struct { ...@@ -93,8 +93,8 @@ type KeyValue struct {
} }
func (v *Map) String() string { func (v *Map) String() string {
v.mu.Lock() v.mu.RLock()
defer v.mu.Unlock() defer v.mu.RUnlock()
b := new(bytes.Buffer) b := new(bytes.Buffer)
fmt.Fprintf(b, "{") fmt.Fprintf(b, "{")
first := true first := true
...@@ -115,8 +115,8 @@ func (v *Map) Init() *Map { ...@@ -115,8 +115,8 @@ func (v *Map) Init() *Map {
} }
func (v *Map) Get(key string) Var { func (v *Map) Get(key string) Var {
v.mu.Lock() v.mu.RLock()
defer v.mu.Unlock() defer v.mu.RUnlock()
return v.m[key] return v.m[key]
} }
...@@ -127,12 +127,17 @@ func (v *Map) Set(key string, av Var) { ...@@ -127,12 +127,17 @@ func (v *Map) Set(key string, av Var) {
} }
func (v *Map) Add(key string, delta int64) { func (v *Map) Add(key string, delta int64) {
v.mu.Lock() v.mu.RLock()
defer v.mu.Unlock()
av, ok := v.m[key] av, ok := v.m[key]
v.mu.RUnlock()
if !ok { if !ok {
av = new(Int) // check again under the write lock
v.m[key] = av v.mu.Lock()
if _, ok = v.m[key]; !ok {
av = new(Int)
v.m[key] = av
}
v.mu.Unlock()
} }
// Add to Int; ignore otherwise. // Add to Int; ignore otherwise.
...@@ -143,12 +148,17 @@ func (v *Map) Add(key string, delta int64) { ...@@ -143,12 +148,17 @@ func (v *Map) Add(key string, delta int64) {
// AddFloat adds delta to the *Float value stored under the given map key. // AddFloat adds delta to the *Float value stored under the given map key.
func (v *Map) AddFloat(key string, delta float64) { func (v *Map) AddFloat(key string, delta float64) {
v.mu.Lock() v.mu.RLock()
defer v.mu.Unlock()
av, ok := v.m[key] av, ok := v.m[key]
v.mu.RUnlock()
if !ok { if !ok {
av = new(Float) // check again under the write lock
v.m[key] = av v.mu.Lock()
if _, ok = v.m[key]; !ok {
av = new(Float)
v.m[key] = av
}
v.mu.Unlock()
} }
// Add to Float; ignore otherwise. // Add to Float; ignore otherwise.
...@@ -157,18 +167,15 @@ func (v *Map) AddFloat(key string, delta float64) { ...@@ -157,18 +167,15 @@ func (v *Map) AddFloat(key string, delta float64) {
} }
} }
// TODO(rsc): Make sure map access in separate thread is safe. // Do calls f for each entry in the map.
func (v *Map) iterate(c chan<- KeyValue) { // The map is locked during the iteration,
// but existing entries may be concurrently updated.
func (v *Map) Do(f func(KeyValue)) {
v.mu.RLock()
defer v.mu.RUnlock()
for k, v := range v.m { for k, v := range v.m {
c <- KeyValue{k, v} f(KeyValue{k, v})
} }
close(c)
}
func (v *Map) Iter() <-chan KeyValue {
c := make(chan KeyValue)
go v.iterate(c)
return c
} }
// String is a string variable, and satisfies the Var interface. // String is a string variable, and satisfies the Var interface.
...@@ -190,8 +197,10 @@ func (f Func) String() string { ...@@ -190,8 +197,10 @@ func (f Func) String() string {
} }
// All published variables. // All published variables.
var vars map[string]Var = make(map[string]Var) var (
var mutex sync.Mutex mutex sync.RWMutex
vars map[string]Var = make(map[string]Var)
)
// Publish declares a named exported variable. This should be called from a // Publish declares a named exported variable. This should be called from a
// package's init function when it creates its Vars. If the name is already // package's init function when it creates its Vars. If the name is already
...@@ -207,17 +216,11 @@ func Publish(name string, v Var) { ...@@ -207,17 +216,11 @@ func Publish(name string, v Var) {
// Get retrieves a named exported variable. // Get retrieves a named exported variable.
func Get(name string) Var { func Get(name string) Var {
mutex.RLock()
defer mutex.RUnlock()
return vars[name] return vars[name]
} }
// RemoveAll removes all exported variables.
// This is for tests; don't call this on a real server.
func RemoveAll() {
mutex.Lock()
defer mutex.Unlock()
vars = make(map[string]Var)
}
// Convenience functions for creating new exported variables. // Convenience functions for creating new exported variables.
func NewInt(name string) *Int { func NewInt(name string) *Int {
...@@ -244,31 +247,28 @@ func NewString(name string) *String { ...@@ -244,31 +247,28 @@ func NewString(name string) *String {
return v return v
} }
// TODO(rsc): Make sure map access in separate thread is safe. // Do calls f for each exported variable.
func iterate(c chan<- KeyValue) { // The global variable map is locked during the iteration,
// but existing entries may be concurrently updated.
func Do(f func(KeyValue)) {
mutex.RLock()
defer mutex.RUnlock()
for k, v := range vars { for k, v := range vars {
c <- KeyValue{k, v} f(KeyValue{k, v})
} }
close(c)
}
func Iter() <-chan KeyValue {
c := make(chan KeyValue)
go iterate(c)
return c
} }
func expvarHandler(w http.ResponseWriter, r *http.Request) { func expvarHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "{\n") fmt.Fprintf(w, "{\n")
first := true first := true
for name, value := range vars { Do(func(kv KeyValue) {
if !first { if !first {
fmt.Fprintf(w, ",\n") fmt.Fprintf(w, ",\n")
} }
first = false first = false
fmt.Fprintf(w, "%q: %s", name, value) fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
} })
fmt.Fprintf(w, "\n}\n") fmt.Fprintf(w, "\n}\n")
} }
...@@ -281,7 +281,7 @@ func memstats() interface{} { ...@@ -281,7 +281,7 @@ func memstats() interface{} {
} }
func init() { func init() {
http.Handle("/debug/vars", http.HandlerFunc(expvarHandler)) http.HandleFunc("/debug/vars", expvarHandler)
Publish("cmdline", Func(cmdline)) Publish("cmdline", Func(cmdline))
Publish("memstats", Func(memstats)) Publish("memstats", Func(memstats))
} }
...@@ -9,6 +9,14 @@ import ( ...@@ -9,6 +9,14 @@ import (
"testing" "testing"
) )
// RemoveAll removes all exported variables.
// This is for tests only.
func RemoveAll() {
mutex.Lock()
defer mutex.Unlock()
vars = make(map[string]Var)
}
func TestInt(t *testing.T) { func TestInt(t *testing.T) {
reqs := NewInt("requests") reqs := NewInt("requests")
if reqs.i != 0 { if reqs.i != 0 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment