Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions cmd/zb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net"
"os"
"path/filepath"
"sync"

jsonv2 "github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
Expand Down Expand Up @@ -173,16 +172,14 @@ func (g *globalConfig) reusePolicy() *zbstorerpc.ReusePolicy {
return &zbstorerpc.ReusePolicy{PublicKeys: g.TrustedPublicKeys}
}

func (g *globalConfig) storeClient(opts *zbstorerpc.CodecOptions) (_ *jsonrpc.Client, wait func()) {
var wg sync.WaitGroup
c := jsonrpc.NewClient(func(ctx context.Context) (jsonrpc.ClientCodec, error) {
func (g *globalConfig) storeClient(opts *zbstorerpc.CodecOptions) *jsonrpc.Client {
return jsonrpc.NewClient(func(ctx context.Context) (jsonrpc.ClientCodec, error) {
conn, err := (&net.Dialer{}).DialContext(ctx, "unix", g.StoreSocket)
if err != nil {
return nil, err
}
return zbstorerpc.NewCodec(conn, opts), nil
})
return c, wg.Wait
}

// defaultVarDir returns "/opt/zb/var/zb" on Unix-like systems or `C:\zb\var\zb` on Windows systems.
Expand Down
14 changes: 4 additions & 10 deletions cmd/zb/derivation.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,10 @@ func runDerivationShow(ctx context.Context, g *globalConfig, opts *derivationSho
}

di := new(zbstorerpc.DeferredImporter)
storeClient, waitStoreClient := g.storeClient(&zbstorerpc.CodecOptions{
storeClient := g.storeClient(&zbstorerpc.CodecOptions{
Importer: di,
})
defer func() {
storeClient.Close()
waitStoreClient()
}()
defer storeClient.Close()
eval, err := opts.newEval(g, storeClient, di)
if err != nil {
return err
Expand Down Expand Up @@ -358,13 +355,10 @@ func newDerivationEnvCommand(g *globalConfig) *cobra.Command {

func runDerivationEnv(ctx context.Context, g *globalConfig, opts *derivationEnvOptions) error {
di := new(zbstorerpc.DeferredImporter)
storeClient, waitStoreClient := g.storeClient(&zbstorerpc.CodecOptions{
storeClient := g.storeClient(&zbstorerpc.CodecOptions{
Importer: di,
})
defer func() {
storeClient.Close()
waitStoreClient()
}()
defer storeClient.Close()
eval, err := opts.newEval(g, storeClient, di)
if err != nil {
return err
Expand Down
14 changes: 4 additions & 10 deletions cmd/zb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,10 @@ func newEvalCommand(g *globalConfig) *cobra.Command {

func runEval(ctx context.Context, g *globalConfig, opts *evalOptions) error {
di := new(zbstorerpc.DeferredImporter)
storeClient, waitStoreClient := g.storeClient(&zbstorerpc.CodecOptions{
storeClient := g.storeClient(&zbstorerpc.CodecOptions{
Importer: di,
})
defer func() {
storeClient.Close()
waitStoreClient()
}()
defer storeClient.Close()
eval, err := opts.newEval(g, storeClient, di)
if err != nil {
return err
Expand Down Expand Up @@ -250,13 +247,10 @@ func newBuildCommand(g *globalConfig) *cobra.Command {

func runBuild(ctx context.Context, g *globalConfig, opts *buildOptions) error {
di := new(zbstorerpc.DeferredImporter)
storeClient, waitStoreClient := g.storeClient(&zbstorerpc.CodecOptions{
storeClient := g.storeClient(&zbstorerpc.CodecOptions{
Importer: di,
})
defer func() {
storeClient.Close()
waitStoreClient()
}()
defer storeClient.Close()
eval, err := opts.newEval(g, storeClient, di)
if err != nil {
return err
Expand Down
21 changes: 6 additions & 15 deletions cmd/zb/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,8 @@ func newStoreObjectInfoCommand(g *globalConfig) *cobra.Command {
}

func runStoreObjectInfo(ctx context.Context, g *globalConfig, opts *storeObjectInfoOptions) error {
storeClient, waitStoreClient := g.storeClient(nil)
defer func() {
storeClient.Close()
waitStoreClient()
}()
storeClient := g.storeClient(nil)
defer storeClient.Close()

const errNotExist = "does not exist"

Expand Down Expand Up @@ -196,13 +193,10 @@ func runStoreObjectExport(ctx context.Context, g *globalConfig, opts *storeObjec
toOutput := zbstorerpc.ImportFunc(func(header jsonrpc.Header, body io.Reader) error {
return zbstore.ReceiveExport(nopReceiver{}, io.TeeReader(body, opts.output))
})
storeClient, waitStoreClient := g.storeClient(&zbstorerpc.CodecOptions{
storeClient := g.storeClient(&zbstorerpc.CodecOptions{
Importer: toOutput,
})
defer func() {
storeClient.Close()
waitStoreClient()
}()
defer storeClient.Close()

req := &zbstorerpc.ExportRequest{
Paths: make([]zbstore.Path, len(opts.paths)),
Expand Down Expand Up @@ -254,11 +248,8 @@ func newStoreObjectImportCommand(g *globalConfig) *cobra.Command {
}

func runStoreObjectImport(ctx context.Context, g *globalConfig, opts *storeObjectImportOptions) error {
storeClient, waitStoreClient := g.storeClient(nil)
defer func() {
storeClient.Close()
waitStoreClient()
}()
storeClient := g.storeClient(nil)
defer storeClient.Close()

inputPaths := opts.paths
if len(inputPaths) == 0 {
Expand Down
Loading