From 697009c0a1a81f633d755ec5dceb5cbb2879174e Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Sun, 2 Aug 2015 11:57:47 -0700 Subject: [PATCH] trace on New and add read locks --- pkg/probe/probe.go | 12 ++++++++++-- pkg/probe/probe_test.go | 15 ++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pkg/probe/probe.go b/pkg/probe/probe.go index c686fc23d..bf6d4c6fc 100644 --- a/pkg/probe/probe.go +++ b/pkg/probe/probe.go @@ -60,7 +60,7 @@ type tracePoint struct { // Error implements tracing error functionality. type Error struct { - lock sync.Mutex + lock sync.RWMutex e error sysInfo map[string]string tracePoints []tracePoint @@ -71,7 +71,8 @@ type Error struct { // trace the return path with Probe.Trace and finally handle reporting or quitting // at the top level. func New(e error) *Error { - return &Error{sync.Mutex{}, e, GetSysInfo(), []tracePoint{}} + Err := Error{sync.RWMutex{}, e, GetSysInfo(), []tracePoint{}} + return Err.Trace() } // Trace records the point at which it is invoked. Stack traces are important for @@ -114,6 +115,9 @@ func (e *Error) Error() string { // String returns error message. func (e *Error) String() string { + e.lock.RLock() + defer e.lock.RUnlock() + trace := e.e.Error() + "\n" for i, tp := range e.tracePoints { if len(tp.Env) > 0 { @@ -135,6 +139,9 @@ func (e *Error) String() string { // JSON returns JSON formated error trace. func (e *Error) JSON() string { + e.lock.RLock() + defer e.lock.RUnlock() + anonError := struct { SysInfo map[string]string TracePoints []tracePoint @@ -153,5 +160,6 @@ func (e *Error) JSON() string { // ToError returns original emnedded error. func (e *Error) ToError() error { + // No need to lock. "e.e" is set once during New and never changed. return e.e } diff --git a/pkg/probe/probe_test.go b/pkg/probe/probe_test.go index b630e847a..f50e8c63e 100644 --- a/pkg/probe/probe_test.go +++ b/pkg/probe/probe_test.go @@ -16,7 +16,6 @@ package probe import ( - "fmt" "os" "testing" ) @@ -33,8 +32,14 @@ func TestProbe(t *testing.T) { if es == nil { t.Fail() } - es.Trace() - fmt.Println(es) - // fmt.Println(es.JSON()) - // fmt.Println(es.ToError()) + + newES := es.Trace() + if newES == nil { + t.Fail() + } + /* + fmt.Println(es) + fmt.Println(es.JSON()) + fmt.Println(es.ToError()) + */ }