From 3d9395e1df1df279e7f54de9e00dd4c37c882042 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Thu, 2 Feb 2017 08:25:42 +0100 Subject: [PATCH] Simplify lock logic --- metrics.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/metrics.go b/metrics.go index 76c2a8d..5577864 100644 --- a/metrics.go +++ b/metrics.go @@ -88,12 +88,13 @@ func newErrorsTotalMap() ErrorsTotalMap { // incErrorsTotal increases the counter for this error atomically by one. func (e *ErrorsTotalMap) incError(err HTTPError) { - // The goal is to have a valid ptr to the number of HTTPError + // The goal is to have a valid ptr to the counter for this err e.RLock() - if ptr, ok := e.m[err]; !ok { + ptr, ok := e.m[err] + e.RUnlock() + if !ok { // The ptr does not seem to exist for this err // Hence we create it (using a write lock) - e.RUnlock() e.Lock() // We ensure that the ptr wasn't created in the meantime if ptr, ok = e.m[err]; !ok { @@ -102,8 +103,6 @@ func (e *ErrorsTotalMap) incError(err HTTPError) { e.m[err] = ptr } e.Unlock() - } else { - e.RUnlock() } // We can then increase the counter atomic.AddUint64(e.m[err], 1)