Add WithTime as client Opt

This also sets the default timeout to 10s instead of 60s.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2018-08-16 14:57:19 -04:00
parent 6f13ff3ea4
commit 97e73c9348
2 changed files with 15 additions and 1 deletions

View File

@ -82,6 +82,9 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
return nil, err return nil, err
} }
} }
if copts.timeout == 0 {
copts.timeout = 10 * time.Second
}
rt := fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS) rt := fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS)
if copts.defaultRuntime != "" { if copts.defaultRuntime != "" {
rt = copts.defaultRuntime rt = copts.defaultRuntime
@ -115,7 +118,7 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
) )
} }
connector := func() (*grpc.ClientConn, 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() defer cancel()
conn, err := grpc.DialContext(ctx, dialer.DialAddress(address), gopts...) conn, err := grpc.DialContext(ctx, dialer.DialAddress(address), gopts...)
if err != nil { if err != nil {

View File

@ -17,6 +17,8 @@
package containerd package containerd
import ( import (
"time"
"github.com/containerd/containerd/images" "github.com/containerd/containerd/images"
"github.com/containerd/containerd/platforms" "github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/remotes" "github.com/containerd/containerd/remotes"
@ -28,6 +30,7 @@ type clientOpts struct {
defaultRuntime string defaultRuntime string
services *services services *services
dialOptions []grpc.DialOption dialOptions []grpc.DialOption
timeout time.Duration
} }
// ClientOpt allows callers to set options on the containerd client // 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 // RemoteOpt allows the caller to set distribution options for a remote
type RemoteOpt func(*Client, *RemoteContext) error type RemoteOpt func(*Client, *RemoteContext) error