github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/lib/net/rrdialer/api.go (about)

     1  /*
     2  	Package rrdialer implements a dialer which provides improved behaviour for
     3  	hostnames with multiple IP addresses (aka. round-robin DNS).
     4  
     5  	Unlike the default net.Dialer which divides the timeout between the multiple
     6  	endpoints (IP addresses), the round-robin dialer tracks the historic
     7  	performance of each endpoint to dial the fastest endpoint first and will
     8  	concurrently dial other endpoints if unusually long connection times are
     9  	observed.
    10  */
    11  package rrdialer
    12  
    13  import (
    14  	"context"
    15  	"net"
    16  	"sync"
    17  	"time"
    18  
    19  	"github.com/Cloud-Foundations/Dominator/lib/log"
    20  )
    21  
    22  type Dialer struct {
    23  	dirname   string
    24  	logger    log.DebugLogger
    25  	rawDialer *net.Dialer
    26  	waitGroup sync.WaitGroup
    27  }
    28  
    29  // New creates a new Dialer. The underlying raw dialer used to dial each
    30  // endpoint is given by dialer. The directory in which endpoint statitics are
    31  // written is given by cacheDir. If this is the empty string then the ".cache"
    32  // subdirectory of the home directory is used. Log messages are written to
    33  // logger. If the debug level is 3 or greater then all endpoints are dialed.
    34  func New(dialer *net.Dialer, cacheDir string,
    35  	logger log.DebugLogger) (*Dialer, error) {
    36  	return newDialer(dialer, cacheDir, logger)
    37  }
    38  
    39  // Dial connects to the address on the named network.
    40  func (d *Dialer) Dial(network, address string) (net.Conn, error) {
    41  	return d.dialContext(context.Background(), network, address)
    42  }
    43  
    44  // DialContext connects to the address on the named network using the provided
    45  // context.
    46  func (d *Dialer) DialContext(ctx context.Context, network,
    47  	address string) (net.Conn, error) {
    48  	return d.dialContext(ctx, network, address)
    49  }
    50  
    51  // WaitForBackgroundResults will wait up to timeout for other endpoint
    52  // connection attempts to complete, so that their performance statistics can be
    53  // saved. It is recommended to call this just before the main function returns.
    54  func (d *Dialer) WaitForBackgroundResults(timeout time.Duration) {
    55  	d.waitForBackgroundResults(timeout)
    56  }