From e736602bcaf5ed1e2191a730813ac2bc7caed9eb Mon Sep 17 00:00:00 2001 From: Red Date: Wed, 16 Dec 2020 22:08:08 +0800 Subject: [PATCH] =?UTF-8?q?fix=20memory=20leak=20cause=20by=20the=20spanSt?= =?UTF-8?q?ore.(census-instrumentation/opence=E2=80=A6=20(#1246)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix memory leak in the spanStore --- trace/spanstore.go | 10 +++++----- trace/trace.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/trace/spanstore.go b/trace/spanstore.go index bdcc8db41..e601f76f2 100644 --- a/trace/spanstore.go +++ b/trace/spanstore.go @@ -49,7 +49,7 @@ func (i internalOnly) ReportActiveSpans(name string) []*SpanData { s.mu.Lock() defer s.mu.Unlock() for activeSpan := range s.active { - if s, ok := activeSpan.internal.(*span); ok { + if s, ok := activeSpan.(*span); ok { out = append(out, s.makeSpanData()) } } @@ -187,7 +187,7 @@ func (i internalOnly) ReportSpansByLatency(name string, minLatency, maxLatency t // bucketed by latency. type spanStore struct { mu sync.Mutex // protects everything below. - active map[*Span]struct{} + active map[SpanInterface]struct{} errors map[int32]*bucket latency []bucket maxSpansPerErrorBucket int @@ -196,7 +196,7 @@ type spanStore struct { // newSpanStore creates a span store. func newSpanStore(name string, latencyBucketSize int, errorBucketSize int) *spanStore { s := &spanStore{ - active: make(map[*Span]struct{}), + active: make(map[SpanInterface]struct{}), latency: make([]bucket, len(defaultLatencies)+1), maxSpansPerErrorBucket: errorBucketSize, } @@ -273,7 +273,7 @@ func (s *spanStore) resize(latencyBucketSize int, errorBucketSize int) { } // add adds a span to the active bucket of the spanStore. -func (s *spanStore) add(span *Span) { +func (s *spanStore) add(span SpanInterface) { s.mu.Lock() s.active[span] = struct{}{} s.mu.Unlock() @@ -281,7 +281,7 @@ func (s *spanStore) add(span *Span) { // finished removes a span from the active set, and adds a corresponding // SpanData to a latency or error bucket. -func (s *spanStore) finished(span *Span, sd *SpanData) { +func (s *spanStore) finished(span SpanInterface, sd *SpanData) { latency := sd.EndTime.Sub(sd.StartTime) if latency < 0 { latency = 0 diff --git a/trace/trace.go b/trace/trace.go index 58217d1e3..861df9d39 100644 --- a/trace/trace.go +++ b/trace/trace.go @@ -266,7 +266,7 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa ss = spanStoreForNameCreateIfNew(name) if ss != nil { s.spanStore = ss - ss.add(NewSpan(s)) + ss.add(s) } } @@ -291,7 +291,7 @@ func (s *span) End() { sd := s.makeSpanData() sd.EndTime = internal.MonotonicEndTime(sd.StartTime) if s.spanStore != nil { - s.spanStore.finished(NewSpan(s), sd) + s.spanStore.finished(s, sd) } if mustExport { for e := range exp {