Add with block and fail on non-temp dial error

This guarantees that grpc requests will fail quickly
when the service is not started or does not have permission.
Without the fail on non-temp error the withblock will
cause the client to wait until the timeout before failing.

Fixes #989

Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
Derek McGowan 2017-06-14 15:08:03 -07:00
parent 6bbed2c125
commit 64bc516bbe
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB
2 changed files with 15 additions and 11 deletions

View File

@ -65,9 +65,11 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
}
gopts := []grpc.DialOption{
grpc.WithBlock(),
grpc.WithInsecure(),
grpc.WithTimeout(100 * time.Second),
grpc.WithDialer(dialer),
grpc.FailOnNonTempDialError(true),
}
if copts.defaultns != "" {
unary, stream := newNSInterceptors(copts.defaultns)

View File

@ -67,13 +67,9 @@ func TestMain(m *testing.M) {
}
}
client, err := New(address)
client, err := waitForDaemonStart(ctx, address)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := waitForDaemonStart(ctx, client); err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, "immediate fail!", err)
os.Exit(1)
}
@ -110,20 +106,26 @@ func TestMain(m *testing.M) {
os.Exit(status)
}
func waitForDaemonStart(ctx context.Context, client *Client) error {
func waitForDaemonStart(ctx context.Context, address string) (*Client, error) {
var (
client *Client
serving bool
err error
)
for i := 0; i < 20; i++ {
if client == nil {
client, err = New(address)
}
if err == nil {
serving, err = client.IsServing(ctx)
if serving {
return nil
return client, nil
}
}
time.Sleep(100 * time.Millisecond)
}
return fmt.Errorf("containerd did not start within 2s: %v", err)
return nil, fmt.Errorf("containerd did not start within 2s: %v", err)
}
func TestNewClient(t *testing.T) {