mirror of
https://github.com/libp2p/go-libp2p-resource-manager.git
synced 2025-01-27 12:50:07 +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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer rcmgr.Close()
|
||||||
|
|
||||||
ableToGetAllowlist := GetAllowlist(rcmgr)
|
ableToGetAllowlist := GetAllowlist(rcmgr)
|
||||||
if ableToGetAllowlist == nil {
|
if ableToGetAllowlist == nil {
|
||||||
|
8
trace.go
8
trace.go
@ -19,7 +19,7 @@ type trace struct {
|
|||||||
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel func()
|
cancel func()
|
||||||
closed chan struct{}
|
wg sync.WaitGroup
|
||||||
|
|
||||||
mx sync.Mutex
|
mx sync.Mutex
|
||||||
done bool
|
done bool
|
||||||
@ -232,7 +232,7 @@ func (t *trace) push(evt TraceEvt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *trace) backgroundWriter(out io.WriteCloser) {
|
func (t *trace) backgroundWriter(out io.WriteCloser) {
|
||||||
defer close(t.closed)
|
defer t.wg.Done()
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
|
|
||||||
gzOut := gzip.NewWriter(out)
|
gzOut := gzip.NewWriter(out)
|
||||||
@ -315,7 +315,6 @@ func (t *trace) Start(limits Limiter) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.ctx, t.cancel = context.WithCancel(context.Background())
|
t.ctx, t.cancel = context.WithCancel(context.Background())
|
||||||
t.closed = make(chan struct{})
|
|
||||||
|
|
||||||
if t.path != "" {
|
if t.path != "" {
|
||||||
out, err := os.OpenFile(t.path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.wg.Add(1)
|
||||||
go t.backgroundWriter(out)
|
go t.backgroundWriter(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -350,7 +350,7 @@ func (t *trace) Close() error {
|
|||||||
t.done = true
|
t.done = true
|
||||||
t.mx.Unlock()
|
t.mx.Unlock()
|
||||||
|
|
||||||
<-t.closed
|
t.wg.Wait()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user