Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
client: bypass grpc dialer backoff strategy
Browse files Browse the repository at this point in the history
This would bypass the grpc dialer backoff strategy and handle dial timeout
internally. Because we do not have a large number of concurrent
dialers, it is not reasonable to have such aggressive backoffs which
would kill kata containers boot up speed. For more information, see
https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md

Fixes: #320

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Aug 10, 2018
1 parent 9140c9c commit 730b977
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions protocols/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,13 @@ func agentDialer(addr *url.URL, enableYamux bool) dialer {
}
}

// unix addr are parsed by grpc
func unixDialer(sock string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", sock, timeout)
dialFunc := func() (net.Conn, error) {
return net.DialTimeout("unix", sock, timeout)
}

timeoutErr := grpcStatus.Errorf(codes.DeadlineExceeded, "timed out connecting to unix socket %s", sock)
return commonDialer(timeout, dialFunc, timeoutErr)
}

func parseGrpcVsockAddr(sock string) (uint32, uint32, error) {
Expand All @@ -205,12 +209,12 @@ func parseGrpcVsockAddr(sock string) (uint32, uint32, error) {
return uint32(cid), uint32(port), nil
}

func vsockDialer(sock string, timeout time.Duration) (net.Conn, error) {
cid, port, err := parseGrpcVsockAddr(sock)
if err != nil {
return nil, err
}

// This would bypass the grpc dialer backoff strategy and handle dial timeout
// internally. Because we do not have a large number of concurrent dialers,
// it is not reasonable to have such aggressive backoffs which would kill kata
// containers boot up speed. For more information, see
// https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md
func commonDialer(timeout time.Duration, dialFunc func() (net.Conn, error), timeoutErrMsg error) (net.Conn, error) {
t := time.NewTimer(timeout)
cancel := make(chan bool)
ch := make(chan net.Conn)
Expand All @@ -223,7 +227,7 @@ func vsockDialer(sock string, timeout time.Duration) (net.Conn, error) {
default:
}

conn, err := vsock.Dial(cid, port)
conn, err := dialFunc()
if err == nil {
// Send conn back iff timer is not fired
// Otherwise there might be no one left reading it
Expand All @@ -239,7 +243,6 @@ func vsockDialer(sock string, timeout time.Duration) (net.Conn, error) {

var conn net.Conn
var ok bool
timeoutErrMsg := grpcStatus.Errorf(codes.DeadlineExceeded, "timed out connecting to vsock %d:%d", cid, port)
select {
case conn, ok = <-ch:
if !ok {
Expand All @@ -249,3 +252,18 @@ func vsockDialer(sock string, timeout time.Duration) (net.Conn, error) {

return conn, nil
}

func vsockDialer(sock string, timeout time.Duration) (net.Conn, error) {
cid, port, err := parseGrpcVsockAddr(sock)
if err != nil {
return nil, err
}

dialFunc := func() (net.Conn, error) {
return vsock.Dial(cid, port)
}

timeoutErr := grpcStatus.Errorf(codes.DeadlineExceeded, "timed out connecting to vsock %d:%d", cid, port)

return commonDialer(timeout, dialFunc, timeoutErr)
}

0 comments on commit 730b977

Please sign in to comment.