netutil

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2019 License: MIT Imports: 7 Imported by: 0

README

go-netutil

CircleCI

netutil.RRDialer has the same interface of net.Dial. With this package, You can request to a specific IP address with priority as you like.

In golang, net.LookupIP returns IP addresses after sortBy6724, that is defined in src/net/addrselect.go. Because of this, address for net.Dial might connect to a fixed ip address in some case. For example, source IP and destination IP are in the same local area network like "10.0.0.0/16" or "192.168.0.0/24". In my case, in a AWS VPC network, http.Client always connect to a specific IP address of ELB from an EC2 instance. This will ruin the effects of DNS round robin.

You can change the priority of each address with this package.

netutil.DefaultRRDialer sort a list of address by random priority.

Installation

go get github.com/ReSTARTR/go-netutil

Usage

netutil.RRDialer

netutil.DefaultRRDialer uses netutil.DefaultSort for sorting IP addresses. netutil.DefaultSort sorts IPs randomly.

import (
	"net/http"
	"github.com/ReSTARTR/go-netutil"
)

func main() {
	req, _ := http.NewRequest("GET",  "https://example.com/foo/bar", nil)
	client := http.Client{
		Transport: &http.Transport {
			Dial: netutil.DefaultRRDialer.Dial,
		},
	}
	res, _ := client.Do(req)
	_ = res
}

You can set a specific netutil.RRDialer.Sort function.

dialer := netutil.Dialer{
	Sort: func(ips []net.IP) ([]net.IP) {
		sort.Sort(ByFoo(ips))
		return ips
	},
}
client := http.Client{
	Transport: &http.Transport {
		Dial: dialer.Dial,
	},
}
client.Do(req)

Contribution

  • Fork (https://github.com/ReSTARTR/go-netutil/fork)
  • Create a feature branch
  • Commit your changes
  • Rebase your local changes against the master branch
  • Run test suite with the make test command and confirm that it passes
  • Create a new Pull Request

Author

ReSTARTR

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRRDialer = &RRDialer{
	dialer: &net.Dialer{
		Timeout:   30 * time.Second,
		KeepAlive: 30 * time.Second,
	},
}

DefaultRRDialer has a dialer that includes the same as DefaultTransport use

Functions

func RandomSort

func RandomSort(ips []net.IP)

RandomSort sorts ips at random.

Types

type RRDialer

type RRDialer struct {
	Sort func([]net.IP)
	// contains filtered or unexported fields
}

RRDialer contains options for connection to an address with a specific IP.

TODO: connection pool

func (*RRDialer) Dial

func (d *RRDialer) Dial(network, address string) (net.Conn, error)

Dial connects to the address with an specific IP on the named network.

func (*RRDialer) DialContext

func (d *RRDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

DialContext connects to the address with an specific IP on the named network using the provided context.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL