فهرست منبع

(#86) Add UnregisterAll to Registry and StandardRegistry

This new interface method and its standard implementation will make it
easier to test libraries that depend on go-metrics and assume the
default registry (which is analogous to the relationship between expvar
and http.DefaultServeMux).
Richard Crowley 11 سال پیش
والد
کامیت
dee209f245
1فایلهای تغییر یافته به همراه12 افزوده شده و 0 حذف شده
  1. 12 0
      registry.go

+ 12 - 0
registry.go

@@ -41,6 +41,9 @@ type Registry interface {
 
 	// Unregister the metric with the given name.
 	Unregister(string)
+
+	// Unregister all metrics.  (Mostly for testing.)
+	UnregisterAll()
 }
 
 // The standard implementation of a Registry is a mutex-protected map
@@ -112,6 +115,15 @@ func (r *StandardRegistry) Unregister(name string) {
 	delete(r.metrics, name)
 }
 
+// Unregister all metrics.  (Mostly for testing.)
+func (r *StandardRegistry) UnregisterAll() {
+	r.mutex.Lock()
+	defer r.mutex.Unlock()
+	for name, _ := range r.metrics {
+		delete(r.metrics, name)
+	}
+}
+
 func (r *StandardRegistry) register(name string, i interface{}) error {
 	if _, ok := r.metrics[name]; ok {
 		return DuplicateMetric(name)