mirror of
https://github.com/libp2p/go-libp2p-resource-manager.git
synced 2025-01-26 12:20:20 +08:00
Don't wait for a chan that will never close
This commit is contained in:
parent
fe31993a76
commit
09011ebc78
16
obs/stats_test.go
Normal file
16
obs/stats_test.go
Normal file
@ -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()
|
||||
}
|
@ -1004,6 +1004,7 @@ func TestResourceManagerWithAllowlist(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rcmgr.Close()
|
||||
|
||||
ableToGetAllowlist := GetAllowlist(rcmgr)
|
||||
if ableToGetAllowlist == nil {
|
||||
|
8
trace.go
8
trace.go
@ -19,7 +19,7 @@ type trace struct {
|
||||
|
||||
ctx context.Context
|
||||
cancel func()
|
||||
closed chan struct{}
|
||||
wg sync.WaitGroup
|
||||
|
||||
mx sync.Mutex
|
||||
done bool
|
||||
@ -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)
|
||||
@ -315,7 +315,6 @@ 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)
|
||||
@ -323,6 +322,7 @@ func (t *trace) Start(limits Limiter) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
t.wg.Add(1)
|
||||
go t.backgroundWriter(out)
|
||||
}
|
||||
|
||||
@ -350,7 +350,7 @@ func (t *trace) Close() error {
|
||||
t.done = true
|
||||
t.mx.Unlock()
|
||||
|
||||
<-t.closed
|
||||
t.wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user