github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/unresolved_addr.go (about)

     1  // Copyright 2015 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package util
    12  
    13  import (
    14  	"fmt"
    15  	"net"
    16  )
    17  
    18  // TestAddr is an address to use for test servers. Listening on port 0
    19  // causes the kernel to allocate an unused port.
    20  var TestAddr = NewUnresolvedAddr("tcp", "127.0.0.1:0")
    21  
    22  // IsolatedTestAddr is initialized in testaddr_*.go
    23  
    24  // IsolatedTestAddr is an address to use for tests that need extra
    25  // isolation by using more addresses than 127.0.0.1 (support for this
    26  // is platform-specific and only enabled on Linux). Both TestAddr and
    27  // IsolatedTestAddr guarantee that the chosen port is not in use when
    28  // allocated, but IsolatedTestAddr draws from a larger pool of
    29  // addresses so that when tests are run in a tight loop the system is
    30  // less likely to run out of available ports or give a port to one
    31  // test immediately after it was closed by another.
    32  //
    33  // IsolatedTestAddr should be used for tests that open and close a
    34  // large number of sockets, or tests which stop a server and rely on
    35  // seeing a "connection refused" error afterwards. It cannot be used
    36  // with tests that operate in secure mode since our test certificates
    37  // are only valid for 127.0.0.1.
    38  var IsolatedTestAddr *UnresolvedAddr
    39  
    40  // MakeUnresolvedAddr populates an UnresolvedAddr from a network and raw
    41  // address string.
    42  func MakeUnresolvedAddr(network, addr string) UnresolvedAddr {
    43  	return UnresolvedAddr{
    44  		NetworkField: network,
    45  		AddressField: addr,
    46  	}
    47  }
    48  
    49  // NewUnresolvedAddr creates a new UnresolvedAddr from a network and raw
    50  // address string.
    51  func NewUnresolvedAddr(network, addr string) *UnresolvedAddr {
    52  	return &UnresolvedAddr{
    53  		NetworkField: network,
    54  		AddressField: addr,
    55  	}
    56  }
    57  
    58  // Note that we make *UnresolvedAddr implement the net.Addr interface, not
    59  // UnresolvedAddr. This is done because assigning a non-empty struct to an
    60  // interface requires an allocation, while assigning a pointer to an interface
    61  // is allocation free. Using an *UnresolvedAddr makes it both clear that an
    62  // allocation is occurring and allows us to avoid an allocation when an
    63  // UnresolvedAddr is a field of a struct (e.g. NodeDescriptor.Address).
    64  var _ net.Addr = &UnresolvedAddr{}
    65  
    66  // Network returns the address's network name.
    67  func (a *UnresolvedAddr) Network() string {
    68  	return a.NetworkField
    69  }
    70  
    71  // IsEmpty returns true if the address has no network or address specified.
    72  func (a UnresolvedAddr) IsEmpty() bool {
    73  	return a == (UnresolvedAddr{})
    74  }
    75  
    76  // String returns the address's string form.
    77  func (a UnresolvedAddr) String() string {
    78  	return a.AddressField
    79  }
    80  
    81  // Resolve attempts to resolve a into a net.Addr.
    82  func (a UnresolvedAddr) Resolve() (net.Addr, error) {
    83  	switch a.NetworkField {
    84  	case "tcp", "tcp4", "tcp6":
    85  		return net.ResolveTCPAddr(a.NetworkField, a.AddressField)
    86  	case "udp", "udp4", "udp6":
    87  		return net.ResolveUDPAddr(a.NetworkField, a.AddressField)
    88  	case "unix", "unixgram", "unixpacket":
    89  		return net.ResolveUnixAddr(a.NetworkField, a.AddressField)
    90  	}
    91  	return nil, fmt.Errorf("network %s not supported", a.NetworkField)
    92  }