From 9ceda78057ba1341c98d532605465d71dd2e8916 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Wed, 5 Jun 2024 22:04:59 -0700 Subject: [PATCH] remote: ensure that client connection is not established twice Because remote driver implements Info() by calling Client() internally, two instances on Client are created backed by separate TCP connection. This hack avoids it and improves performance. Signed-off-by: Tonis Tiigi --- driver/remote/driver.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/driver/remote/driver.go b/driver/remote/driver.go index 56b1078d..eaf47e85 100644 --- a/driver/remote/driver.go +++ b/driver/remote/driver.go @@ -7,6 +7,7 @@ import ( "net" "os" "strings" + "sync" "time" "github.com/docker/buildx/driver" @@ -14,6 +15,7 @@ import ( "github.com/docker/buildx/util/progress" "github.com/moby/buildkit/client" "github.com/moby/buildkit/client/connhelper" + "github.com/moby/buildkit/util/tracing/delegated" "github.com/pkg/errors" ) @@ -25,6 +27,11 @@ type Driver struct { // https://github.com/docker/docs/blob/main/content/build/drivers/remote.md *tlsOpts defaultLoad bool + + // remote driver caches the client because its Bootstap/Info methods reuse it internally + clientOnce sync.Once + client *client.Client + err error } type tlsOpts struct { @@ -78,12 +85,18 @@ func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error { } func (d *Driver) Client(ctx context.Context, opts ...client.ClientOpt) (*client.Client, error) { - opts = append([]client.ClientOpt{ - client.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { - return d.Dial(ctx) - }), - }, opts...) - return client.New(ctx, "", opts...) + d.clientOnce.Do(func() { + opts = append([]client.ClientOpt{ + client.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { + return d.Dial(ctx) + }), + client.WithTracerDelegate(delegated.DefaultExporter), + }, opts...) + c, err := client.New(ctx, "", opts...) + d.client = c + d.err = err + }) + return d.client, d.err } func (d *Driver) Dial(ctx context.Context) (net.Conn, error) {