latency

package
v1.11.3 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2018 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package latency provides wrappers for net.Conn, net.Listener, and net.Dialers, designed to interoperate to inject real-world latency into network connections.

Index

Constants

This section is empty.

Variables

View Source
var (
	//Local simulates local network.
	Local = Network{0, 0, 0}
	//LAN simulates local area network network.
	LAN = Network{100 * 1024, 2 * time.Millisecond, 1500}
	//WAN simulates wide area network.
	WAN = Network{20 * 1024, 30 * time.Millisecond, 1500}
	//Longhaul simulates bad network.
	Longhaul = Network{1000 * 1024, 200 * time.Millisecond, 9000}
)

Functions

This section is empty.

Types

type ContextDialer

type ContextDialer func(ctx context.Context, network, address string) (net.Conn, error)

ContextDialer is a function matching the signature of net.Dialer.DialContext.

type Dialer

type Dialer func(network, address string) (net.Conn, error)

Dialer is a function matching the signature of net.Dial.

type Network

type Network struct {
	Kbps    int           // Kilobits per second; if non-positive, infinite
	Latency time.Duration // One-way latency (sending); if non-positive, no delay
	MTU     int           // Bytes per packet; if non-positive, infinite
}

Network represents a network with the given bandwidth, latency, and MTU (Maximum Transmission Unit) configuration, and can produce wrappers of net.Listeners, net.Conn, and various forms of dialing functions. The Listeners and Dialers/Conns on both sides of connections must come from this package, but need not be created from the same Network. Latency is computed when sending (in Write), and is injected when receiving (in Read). This allows senders' Write calls to be non-blocking, as in real-world applications.

Note: Latency is injected by the sender specifying the absolute time data should be available, and the reader delaying until that time arrives to provide the data. This package attempts to counter-act the effects of clock drift and existing network latency by measuring the delay between the sender's transmission time and the receiver's reception time during startup. No attempt is made to measure the existing bandwidth of the connection.

func (*Network) Conn

func (n *Network) Conn(c net.Conn) (net.Conn, error)

Conn returns a net.Conn that wraps c and injects n's latency into that connection. This function also imposes latency for connection creation. If n's Latency is lower than the measured latency in c, an error is returned.

func (*Network) ContextDialer

func (n *Network) ContextDialer(d ContextDialer) ContextDialer

ContextDialer returns a ContextDialer that wraps d and injects n's latency in its connections. n's Latency is also injected to the connection's creation.

func (*Network) Dialer

func (n *Network) Dialer(d Dialer) Dialer

Dialer returns a Dialer that wraps d and injects n's latency in its connections. n's Latency is also injected to the connection's creation.

func (*Network) Listener

func (n *Network) Listener(l net.Listener) net.Listener

Listener returns a net.Listener that wraps l and injects n's latency in its connections.

func (*Network) TimeoutDialer

func (n *Network) TimeoutDialer(d TimeoutDialer) TimeoutDialer

TimeoutDialer returns a TimeoutDialer that wraps d and injects n's latency in its connections. n's Latency is also injected to the connection's creation.

type TimeoutDialer

type TimeoutDialer func(network, address string, timeout time.Duration) (net.Conn, error)

TimeoutDialer is a function matching the signature of net.DialTimeout.

Jump to

Keyboard shortcuts

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