github.com/demonoid81/containerd@v1.3.4/pkg/ttrpcutil/client_windows.go (about)

     1  // +build windows
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package ttrpcutil
    20  
    21  import (
    22  	"context"
    23  	"net"
    24  	"os"
    25  	"time"
    26  
    27  	winio "github.com/Microsoft/go-winio"
    28  	"github.com/pkg/errors"
    29  )
    30  
    31  func ttrpcDial(address string, timeout time.Duration) (net.Conn, error) {
    32  	ctx, cancel := context.WithTimeout(context.Background(), timeout)
    33  	defer cancel()
    34  
    35  	// If there is nobody serving the pipe we limit the timeout for this case to
    36  	// 5 seconds because any shim that would serve this endpoint should serve it
    37  	// within 5 seconds.
    38  	serveTimer := time.NewTimer(5 * time.Second)
    39  	defer serveTimer.Stop()
    40  	for {
    41  		c, err := winio.DialPipeContext(ctx, address)
    42  		if err != nil {
    43  			if os.IsNotExist(err) {
    44  				select {
    45  				case <-serveTimer.C:
    46  					return nil, errors.Wrap(os.ErrNotExist, "pipe not found before timeout")
    47  				default:
    48  					// Wait 10ms for the shim to serve and try again.
    49  					time.Sleep(10 * time.Millisecond)
    50  					continue
    51  				}
    52  			} else if err == context.DeadlineExceeded {
    53  				return nil, errors.Wrapf(err, "timed out waiting for npipe %s", address)
    54  			}
    55  			return nil, err
    56  		}
    57  		return c, nil
    58  	}
    59  }