Skip to content

Commit

Permalink
Merge pull request #65 from libp2p/marco-fix-trace-close
Browse files Browse the repository at this point in the history
Don't wait for a chan that will never close
  • Loading branch information
MarcoPolo committed Jul 6, 2022
2 parents 9176f63 + 392771e commit a0be99e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
16 changes: 16 additions & 0 deletions p2p/host/resource-manager/obs/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package obs_test

import (
"testing"

rcmgr "github.com/libp2p/go-libp2p-resource-manager"
"github.com/libp2p/go-libp2p-resource-manager/obs"
)

func TestTraceReporterStartAndClose(t *testing.T) {
rcmgr, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(rcmgr.DefaultLimits.AutoScale()), rcmgr.WithTraceReporter(obs.StatsTraceReporter{}))
if err != nil {
t.Fatal(err)
}
defer rcmgr.Close()
}
1 change: 1 addition & 0 deletions p2p/host/resource-manager/rcmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ func TestResourceManagerWithAllowlist(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer rcmgr.Close()

ableToGetAllowlist := GetAllowlist(rcmgr)
if ableToGetAllowlist == nil {
Expand Down
8 changes: 4 additions & 4 deletions p2p/host/resource-manager/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type trace struct {

ctx context.Context
cancel func()
closed chan struct{}
wg sync.WaitGroup

mx sync.Mutex
done bool
Expand Down Expand Up @@ -232,7 +232,7 @@ func (t *trace) push(evt TraceEvt) {
}

func (t *trace) backgroundWriter(out io.WriteCloser) {
defer close(t.closed)
defer t.wg.Done()
defer out.Close()

gzOut := gzip.NewWriter(out)
Expand Down Expand Up @@ -315,14 +315,14 @@ func (t *trace) Start(limits Limiter) error {
}

t.ctx, t.cancel = context.WithCancel(context.Background())
t.closed = make(chan struct{})

if t.path != "" {
out, err := os.OpenFile(t.path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return nil
}

t.wg.Add(1)
go t.backgroundWriter(out)
}

Expand Down Expand Up @@ -350,7 +350,7 @@ func (t *trace) Close() error {
t.done = true
t.mx.Unlock()

<-t.closed
t.wg.Wait()
return nil
}

Expand Down

0 comments on commit a0be99e

Please sign in to comment.