github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/googledirectpath/utils.go (about)

     1  /*
     2   *
     3   * Copyright 2021 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package googledirectpath
    20  
    21  import (
    22  	"bytes"
    23  	"fmt"
    24  	"io/ioutil"
    25  	"net/url"
    26  	"sync"
    27  	"time"
    28  
    29  	http "github.com/hxx258456/ccgo/gmhttp"
    30  )
    31  
    32  func getFromMetadata(timeout time.Duration, urlStr string) ([]byte, error) {
    33  	parsedURL, err := url.Parse(urlStr)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	client := &http.Client{Timeout: timeout}
    38  	req := &http.Request{
    39  		Method: http.MethodGet,
    40  		URL:    parsedURL,
    41  		Header: http.Header{"Metadata-Flavor": {"Google"}},
    42  	}
    43  	resp, err := client.Do(req)
    44  	if err != nil {
    45  		return nil, fmt.Errorf("failed communicating with metadata server: %v", err)
    46  	}
    47  	defer resp.Body.Close()
    48  	if resp.StatusCode != http.StatusOK {
    49  		return nil, fmt.Errorf("metadata server returned resp with non-OK: %v", resp)
    50  	}
    51  	body, err := ioutil.ReadAll(resp.Body)
    52  	if err != nil {
    53  		return nil, fmt.Errorf("failed reading from metadata server: %v", err)
    54  	}
    55  	return body, nil
    56  }
    57  
    58  var (
    59  	zone     string
    60  	zoneOnce sync.Once
    61  )
    62  
    63  // Defined as var to be overridden in tests.
    64  var getZone = func(timeout time.Duration) string {
    65  	zoneOnce.Do(func() {
    66  		qualifiedZone, err := getFromMetadata(timeout, zoneURL)
    67  		if err != nil {
    68  			logger.Warningf("could not discover instance zone: %v", err)
    69  			return
    70  		}
    71  		i := bytes.LastIndexByte(qualifiedZone, '/')
    72  		if i == -1 {
    73  			logger.Warningf("could not parse zone from metadata server: %s", qualifiedZone)
    74  			return
    75  		}
    76  		zone = string(qualifiedZone[i+1:])
    77  	})
    78  	return zone
    79  }
    80  
    81  var (
    82  	ipv6Capable     bool
    83  	ipv6CapableOnce sync.Once
    84  )
    85  
    86  // Defined as var to be overridden in tests.
    87  var getIPv6Capable = func(timeout time.Duration) bool {
    88  	ipv6CapableOnce.Do(func() {
    89  		_, err := getFromMetadata(timeout, ipv6URL)
    90  		if err != nil {
    91  			logger.Warningf("could not discover ipv6 capability: %v", err)
    92  			return
    93  		}
    94  		ipv6Capable = true
    95  	})
    96  	return ipv6Capable
    97  }