Files
Mike Cohen 6f8e5a4d7f Added progress reporting to offline collector (#2423)
* When copying large files it is useful to get some kind of feedback.
* Also implemented less strict escaping for zip filenames - previously
  this was reusing the same rules as the filestore which mutated
  things like the .db extension.
* Updated pathspec() function to accept an accessor
* Start client's CA Pool from the OS root store, then adds built in
  certs. (Issue #2330)
* Updated KapeFiles to latest.
2023-02-07 04:07:27 +10:00

60 lines
1.0 KiB
Go

package utils
import (
"context"
"io"
"sync"
"time"
)
type ProgressReporter interface {
Report(progress string)
}
type DurationProgressWriter struct {
cb func(byte_count int, d time.Duration)
mu sync.Mutex
count int
started time.Time
writer io.Writer
}
func (self *DurationProgressWriter) Write(buf []byte) (int, error) {
n, err := self.writer.Write(buf)
self.mu.Lock()
self.count += n
self.mu.Unlock()
return n, err
}
func NewDurationProgressWriter(
ctx context.Context, cb func(byte_count int, d time.Duration),
writer io.Writer, period time.Duration) (*DurationProgressWriter, func()) {
self := &DurationProgressWriter{
cb: cb,
started: GetTime().Now(),
writer: writer,
}
sub_ctx, cancel := context.WithCancel(ctx)
go func() {
defer cancel()
for {
select {
case <-sub_ctx.Done():
return
case <-time.After(period):
self.mu.Lock()
duration := GetTime().Now().Sub(self.started)
cb(self.count, duration)
self.mu.Unlock()
}
}
}()
return self, cancel
}