diff --git a/client.go b/client.go index 788a381d6..bafc1b09e 100644 --- a/client.go +++ b/client.go @@ -82,6 +82,9 @@ func New(address string, opts ...ClientOpt) (*Client, error) { return nil, err } } + if copts.timeout == 0 { + copts.timeout = 10 * time.Second + } rt := fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS) if copts.defaultRuntime != "" { rt = copts.defaultRuntime @@ -115,7 +118,7 @@ func New(address string, opts ...ClientOpt) (*Client, error) { ) } connector := func() (*grpc.ClientConn, error) { - ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), copts.timeout) defer cancel() conn, err := grpc.DialContext(ctx, dialer.DialAddress(address), gopts...) if err != nil { diff --git a/client_opts.go b/client_opts.go index 6e6198739..d6adcb2e9 100644 --- a/client_opts.go +++ b/client_opts.go @@ -17,6 +17,8 @@ package containerd import ( + "time" + "github.com/containerd/containerd/images" "github.com/containerd/containerd/platforms" "github.com/containerd/containerd/remotes" @@ -28,6 +30,7 @@ type clientOpts struct { defaultRuntime string services *services dialOptions []grpc.DialOption + timeout time.Duration } // ClientOpt allows callers to set options on the containerd client @@ -71,6 +74,14 @@ func WithServices(opts ...ServicesOpt) ClientOpt { } } +// WithTimeout sets the connection timeout for the client +func WithTimeout(d time.Duration) ClientOpt { + return func(c *clientOpts) error { + c.timeout = d + return nil + } +} + // RemoteOpt allows the caller to set distribution options for a remote type RemoteOpt func(*Client, *RemoteContext) error