[#347] zfscmd + zfs: define .Start() semantics, apply to call sites in pkg zfs

fixes #347
This commit is contained in:
Christian Schwarz
2020-08-29 19:18:00 +02:00
parent fecc9416ab
commit 0f3da73ef1
3 changed files with 41 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/pkg/errors"
@@ -218,14 +219,23 @@ func ZFSListFilesystemVersions(ctx context.Context, fs *DatasetPath, options Lis
promTimer := prometheus.NewTimer(prom.ZFSListFilesystemVersionDuration.WithLabelValues(fs.ToString()))
defer promTimer.ObserveDuration()
// Note: we don't create a separate trace.Task here because our loop that consumes
// the goroutine's output doesn't use ctx.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go ZFSListChan(ctx, listResults,
[]string{"name", "guid", "createtxg", "creation", "userrefs"},
fs,
"-r", "-d", "1",
"-t", options.typesFlagArgs(),
"-s", "createtxg", fs.ToString())
// make sure the goroutine doesn't outlive this function call
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
defer cancel() // on exit, cancel list process before waiting for it
go func() {
defer wg.Done()
ZFSListChan(ctx, listResults,
[]string{"name", "guid", "createtxg", "creation", "userrefs"},
fs,
"-r", "-d", "1",
"-t", options.typesFlagArgs(),
"-s", "createtxg", fs.ToString())
}()
res = make([]FilesystemVersion, 0)
for listResult := range listResults {