Skip to content

Commit 97e73c9

Browse files
committed
Add WithTime as client Opt
This also sets the default timeout to 10s instead of 60s. Signed-off-by: Michael Crosby <[email protected]>
1 parent 6f13ff3 commit 97e73c9

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
8282
return nil, err
8383
}
8484
}
85+
if copts.timeout == 0 {
86+
copts.timeout = 10 * time.Second
87+
}
8588
rt := fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS)
8689
if copts.defaultRuntime != "" {
8790
rt = copts.defaultRuntime
@@ -115,7 +118,7 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
115118
)
116119
}
117120
connector := func() (*grpc.ClientConn, error) {
118-
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
121+
ctx, cancel := context.WithTimeout(context.Background(), copts.timeout)
119122
defer cancel()
120123
conn, err := grpc.DialContext(ctx, dialer.DialAddress(address), gopts...)
121124
if err != nil {

client_opts.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package containerd
1818

1919
import (
20+
"time"
21+
2022
"github.com/containerd/containerd/images"
2123
"github.com/containerd/containerd/platforms"
2224
"github.com/containerd/containerd/remotes"
@@ -28,6 +30,7 @@ type clientOpts struct {
2830
defaultRuntime string
2931
services *services
3032
dialOptions []grpc.DialOption
33+
timeout time.Duration
3134
}
3235

3336
// ClientOpt allows callers to set options on the containerd client
@@ -71,6 +74,14 @@ func WithServices(opts ...ServicesOpt) ClientOpt {
7174
}
7275
}
7376

77+
// WithTimeout sets the connection timeout for the client
78+
func WithTimeout(d time.Duration) ClientOpt {
79+
return func(c *clientOpts) error {
80+
c.timeout = d
81+
return nil
82+
}
83+
}
84+
7485
// RemoteOpt allows the caller to set distribution options for a remote
7586
type RemoteOpt func(*Client, *RemoteContext) error
7687

0 commit comments

Comments
 (0)