Skip to content

Commit

Permalink
Merge pull request #2501 from tonistiigi/remote-client-cache
Browse files Browse the repository at this point in the history
remote: ensure that client connection is not established twice
  • Loading branch information
tonistiigi authored Jul 2, 2024
2 parents 6627f31 + 9ceda78 commit 4be2259
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions driver/remote/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"net"
"os"
"strings"
"sync"
"time"

"github.com/docker/buildx/driver"
util "github.com/docker/buildx/driver/remote/util"
"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"
)

Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4be2259

Please sign in to comment.