github.com/google/cadvisor@v0.49.1/container/containerd/pkg/dialer/dialer.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  /*
    15     Copyright The containerd Authors.
    16  
    17     Licensed under the Apache License, Version 2.0 (the "License");
    18     you may not use this file except in compliance with the License.
    19     You may obtain a copy of the License at
    20  
    21         http://www.apache.org/licenses/LICENSE-2.0
    22  
    23     Unless required by applicable law or agreed to in writing, software
    24     distributed under the License is distributed on an "AS IS" BASIS,
    25     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    26     See the License for the specific language governing permissions and
    27     limitations under the License.
    28  */
    29  
    30  package dialer
    31  
    32  import (
    33  	"context"
    34  	"net"
    35  	"time"
    36  
    37  	"github.com/pkg/errors"
    38  )
    39  
    40  type dialResult struct {
    41  	c   net.Conn
    42  	err error
    43  }
    44  
    45  // ContextDialer returns a GRPC net.Conn connected to the provided address
    46  func ContextDialer(ctx context.Context, address string) (net.Conn, error) {
    47  	if deadline, ok := ctx.Deadline(); ok {
    48  		return timeoutDialer(address, time.Until(deadline))
    49  	}
    50  	return timeoutDialer(address, 0)
    51  }
    52  
    53  // Dialer returns a GRPC net.Conn connected to the provided address
    54  // Deprecated: use ContextDialer and grpc.WithContextDialer.
    55  var Dialer = timeoutDialer
    56  
    57  func timeoutDialer(address string, timeout time.Duration) (net.Conn, error) {
    58  	var (
    59  		stopC = make(chan struct{})
    60  		synC  = make(chan *dialResult)
    61  	)
    62  	go func() {
    63  		defer close(synC)
    64  		for {
    65  			select {
    66  			case <-stopC:
    67  				return
    68  			default:
    69  				c, err := dialer(address, timeout)
    70  				if isNoent(err) {
    71  					<-time.After(10 * time.Millisecond)
    72  					continue
    73  				}
    74  				synC <- &dialResult{c, err}
    75  				return
    76  			}
    77  		}
    78  	}()
    79  	select {
    80  	case dr := <-synC:
    81  		return dr.c, dr.err
    82  	case <-time.After(timeout):
    83  		close(stopC)
    84  		go func() {
    85  			dr := <-synC
    86  			if dr != nil && dr.c != nil {
    87  				dr.c.Close()
    88  			}
    89  		}()
    90  		return nil, errors.Errorf("dial %s: timeout", address)
    91  	}
    92  }