From f4b09ff457a3369067e4a160157864cc2768b32d Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 7 Jun 2022 11:44:34 +0200 Subject: [PATCH] don't create a trace file here, write trace to io.Writer Arguably, it's not a library's responsibility to create a trace file, and to decide on the compression algorithm. The design is cleaner if we just dump the trace to an io.WriteCloser, and let the application decide how to handle the concrete implementation. --- trace.go | 38 +++++++------------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) diff --git a/trace.go b/trace.go index 5bca5b0..9d44e30 100644 --- a/trace.go +++ b/trace.go @@ -1,11 +1,9 @@ package rcmgr import ( - "compress/gzip" "context" "encoding/json" "io" - "os" "sync" "time" @@ -13,7 +11,7 @@ import ( ) type trace struct { - path string + out io.WriteCloser ctx context.Context cancel func() @@ -24,9 +22,9 @@ type trace struct { pend []interface{} } -func WithTrace(path string) Option { +func WithTrace(out io.WriteCloser) Option { return func(r *resourceManager) error { - r.trace = &trace{path: path} + r.trace = &trace{out: out} return nil } } @@ -83,14 +81,11 @@ func (t *trace) push(evt TraceEvt) { t.pend = append(t.pend, evt) } -func (t *trace) background(out io.WriteCloser) { +func (t *trace) background() { defer close(t.closed) - defer out.Close() + defer t.out.Close() - gzOut := gzip.NewWriter(out) - defer gzOut.Close() - - jsonOut := json.NewEncoder(gzOut) + jsonOut := json.NewEncoder(t.out) ticker := time.NewTicker(time.Second) defer ticker.Stop() @@ -121,15 +116,6 @@ func (t *trace) background(out io.WriteCloser) { t.mx.Unlock() return } - - if err := gzOut.Flush(); err != nil { - log.Warnf("error flushing rcmgr trace: %s", err) - t.mx.Lock() - t.done = true - t.mx.Unlock() - return - } - case <-t.ctx.Done(): getEvents() @@ -141,11 +127,6 @@ func (t *trace) background(out io.WriteCloser) { log.Warnf("error writing rcmgr trace: %s", err) return } - - if err := gzOut.Flush(); err != nil { - log.Warnf("error flushing rcmgr trace: %s", err) - } - return } } @@ -169,12 +150,7 @@ func (t *trace) Start(limits Limiter) error { t.ctx, t.cancel = context.WithCancel(context.Background()) t.closed = make(chan struct{}) - out, err := os.OpenFile(t.path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) - if err != nil { - return nil - } - - go t.background(out) + go t.background() t.push(TraceEvt{ Type: traceStartEvt,