Skip to content

Commit 76ecda8

Browse files
authored
Merge pull request moby#3586 from marxarelli/review/client-session-connection-retry
buildctl: Provide --wait option
2 parents 0b86652 + ef61bbe commit 76ecda8

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

client/client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
sdktrace "go.opentelemetry.io/otel/sdk/trace"
2626
"go.opentelemetry.io/otel/trace"
2727
"google.golang.org/grpc"
28+
"google.golang.org/grpc/codes"
2829
"google.golang.org/grpc/credentials"
2930
"google.golang.org/grpc/credentials/insecure"
3031
)
@@ -184,6 +185,19 @@ func (c *Client) Dialer() session.Dialer {
184185
return grpchijack.Dialer(c.ControlClient())
185186
}
186187

188+
func (c *Client) Wait(ctx context.Context) error {
189+
opts := []grpc.CallOption{grpc.WaitForReady(true)}
190+
_, err := c.ControlClient().Info(ctx, &controlapi.InfoRequest{}, opts...)
191+
if err != nil {
192+
if code := grpcerrors.Code(err); code == codes.Unimplemented {
193+
// only buildkit v0.11+ supports the info api, but an unimplemented
194+
// response error is still a response so we can ignore it
195+
return nil
196+
}
197+
}
198+
return err
199+
}
200+
187201
func (c *Client) Close() error {
188202
return c.conn.Close()
189203
}

cmd/buildctl/common/common.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,25 @@ func ResolveClient(c *cli.Context) (*client.Client, error) {
9090
}
9191

9292
timeout := time.Duration(c.GlobalInt("timeout"))
93-
ctx, cancel := context.WithTimeout(ctx, timeout*time.Second)
94-
defer cancel()
93+
if timeout > 0 {
94+
ctx2, cancel := context.WithTimeout(ctx, timeout*time.Second)
95+
ctx = ctx2
96+
defer cancel()
97+
}
98+
99+
cl, err := client.New(ctx, c.GlobalString("addr"), opts...)
100+
if err != nil {
101+
return nil, err
102+
}
103+
104+
wait := c.GlobalBool("wait")
105+
if wait {
106+
if err := cl.Wait(ctx); err != nil {
107+
return nil, err
108+
}
109+
}
95110

96-
return client.New(ctx, c.GlobalString("addr"), opts...)
111+
return cl, nil
97112
}
98113

99114
func ParseTemplate(format string) (*template.Template, error) {

cmd/buildctl/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ func main() {
8787
Usage: "timeout backend connection after value seconds",
8888
Value: 5,
8989
},
90+
cli.BoolFlag{
91+
Name: "wait",
92+
Usage: "block RPCs until the connection becomes available",
93+
},
9094
}
9195

9296
app.Commands = []cli.Command{

docs/reference/buildctl.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ GLOBAL OPTIONS:
3030
--tlskey value client key
3131
--tlsdir value directory containing CA certificate, client certificate, and client key
3232
--timeout value timeout backend connection after value seconds (default: 5)
33+
--wait block RPCs until the connection becomes available
3334
--help, -h show help
3435
--version, -v print the version
3536
```

0 commit comments

Comments
 (0)