Documentation
¶
Overview ¶
Package lossyconn provides an in-memory packet connection simulator.
It supports configurable delay, bandwidth limits, packet loss, packet size and lifetime limits, duplication, corruption, reordering, and bounded queues for network testing. Outgoing profiles can be configured and scheduled per destination.
Index ¶
- type Address
- type Config
- type ConnectionManager
- type LinkProfile
- type LossyConn
- func (conn *LossyConn) ClearPeerProfile(addr net.Addr) error
- func (conn *LossyConn) Close() error
- func (conn *LossyConn) LocalAddr() net.Addr
- func (conn *LossyConn) Read(b []byte) (int, error)
- func (conn *LossyConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)
- func (conn *LossyConn) RemoteAddr() net.Addr
- func (conn *LossyConn) ResetStats()
- func (conn *LossyConn) SchedulePeerProfile(addr net.Addr, at time.Time, profile LinkProfile) error
- func (conn *LossyConn) SetDeadline(t time.Time) error
- func (conn *LossyConn) SetDelayDeviation(deviation float64) error
- func (conn *LossyConn) SetLinkState(up bool)
- func (conn *LossyConn) SetPeerLinkState(addr net.Addr, up bool) error
- func (conn *LossyConn) SetPeerProfile(addr net.Addr, profile LinkProfile) error
- func (conn *LossyConn) SetReadDeadline(t time.Time) error
- func (conn *LossyConn) SetWriteDeadline(t time.Time) error
- func (conn *LossyConn) Stats() Stats
- func (conn *LossyConn) String() string
- func (conn *LossyConn) Write(p []byte) (n int, err error)
- func (conn *LossyConn) WriteTo(p []byte, addr net.Addr) (n int, err error)
- type Packet
- type Stats
- type TimedSender
- func (h *TimedSender) Close() error
- func (h *TimedSender) Len() int
- func (h *TimedSender) Less(i, j int) bool
- func (h *TimedSender) Pop() interface{}
- func (h *TimedSender) Push(x interface{})
- func (h *TimedSender) Send(conn *LossyConn, packet Packet, delay time.Duration)
- func (h *TimedSender) Swap(i, j int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Address ¶
type Address struct {
// contains filtered or unexported fields
}
Address is the opaque local address of a simulated connection.
func NewAddress ¶
func NewAddress() *Address
NewAddress creates a simulated address using cryptographically random bytes.
type Config ¶ added in v1.1.0
type Config struct {
// One-way base delay.
Delay time.Duration
// Standard deviation of normally distributed delay variation, in milliseconds.
DelayDeviation float64
// Maximum outgoing bandwidth in bytes per second. Zero disables shaping.
BandwidthBytesPerSec uint64
// Maximum outgoing payload size in bytes. Zero disables the limit.
MaxPacketSize int
// Maximum time a packet may remain in transit. Zero disables expiration.
PacketLifetime time.Duration
// Added delay per packet already waiting in the outgoing queue.
QueueDelayPerPacket time.Duration
// Maximum congestion delay. Required when QueueDelayPerPacket is positive.
MaxQueueDelay time.Duration
// Probability of independently dropping each outgoing packet, in [0, 1].
RandomLossRate float64
// Probability of starting a burst-loss event for an outgoing packet, in [0, 1].
BurstLossRate float64
// Number of consecutive packets dropped when a burst-loss event starts.
BurstLength int
// Probability of duplicating an outgoing packet, in [0, 1].
DuplicateRate float64
// Probability of corrupting an outgoing packet payload, in [0, 1].
CorruptionRate float64
// Probability of adding an extra delay to an outgoing packet, in [0, 1].
ReorderRate float64
// Maximum extra delay used when reordering a packet.
MaxReorderDelay time.Duration
// Maximum number of received packets buffered by each connection. Zero uses the default.
QueueSize int
// Maximum number of packets waiting in the outgoing queue. Zero uses the default.
MaxPendingPackets int
// Seed for deterministic simulation. Zero selects a time-based seed.
Seed int64
}
Config controls link simulation behavior.
type ConnectionManager ¶
type ConnectionManager struct {
// contains filtered or unexported fields
}
ConnectionManager indexes simulated connections by local address.
func NewConnectionManager ¶
func NewConnectionManager() *ConnectionManager
NewConnectionManager creates an empty connection manager.
func (*ConnectionManager) Delete ¶
func (mgr *ConnectionManager) Delete(conn *LossyConn)
Delete removes conn from the manager.
func (*ConnectionManager) Get ¶
func (mgr *ConnectionManager) Get(addr string) *LossyConn
Get returns the connection registered for addr, or nil if none exists.
func (*ConnectionManager) Set ¶
func (mgr *ConnectionManager) Set(conn *LossyConn)
Set registers conn by its local address.
type LinkProfile ¶ added in v1.1.0
LinkProfile describes the outgoing link to one peer. Config contains the link behavior; connection-wide queue limits and seed are ignored.
type LossyConn ¶
type LossyConn struct {
// contains filtered or unexported fields
}
LossyConn is an in-memory net.PacketConn with configurable network effects.
func NewLossyConn ¶
NewLossyConn creates a connection with a packet-loss probability and delay.
loss must be in [0, 1]. delay is the one-way delay in milliseconds.
Use NewWithConfig to configure additional network effects.
func NewLossyPacketConn
deprecated
added in
v1.1.0
func NewWithConfig ¶ added in v1.1.0
NewWithConfig creates a configurable lossy packet connection. Invalid probabilities, negative delay and packet limits, and negative outgoing queue limits return an error. Non-positive receive queue limits and zero outgoing limits select the default capacity of 4096 packets.
func (*LossyConn) ClearPeerProfile ¶ added in v1.1.0
ClearPeerProfile removes the profile and scheduled changes for addr.
func (*LossyConn) Close ¶
Close closes the connection, unblocks blocked reads, stops its sender, and discards packets that have not yet been delivered.
func (*LossyConn) ReadFrom ¶
ReadFrom reads one packet, copying its payload into p and returning its source address. The returned byte count is between zero and len(p); if p is too small, the remainder of the packet is discarded. If no packet is available before a configured read deadline, the method returns a net.Error with Timeout() == true.
func (*LossyConn) RemoteAddr ¶
RemoteAddr returns nil because a simulated connection can send to multiple peers.
func (*LossyConn) ResetStats ¶ added in v1.1.0
func (conn *LossyConn) ResetStats()
ResetStats resets all counters, including the observed maximum delay.
func (*LossyConn) SchedulePeerProfile ¶ added in v1.1.0
SchedulePeerProfile schedules a complete outgoing link profile for addr. The profile takes effect for writes started at or after at.
func (*LossyConn) SetDeadline ¶
SetDeadline sets both read and write deadlines. It is equivalent to calling SetReadDeadline and SetWriteDeadline with the same absolute time.
A deadline applies to current and future operations. Reads already in progress are woken when the deadline changes. A read with no available packet waits until data arrives, the connection closes, or the deadline expires. WriteTo checks its deadline before scheduling a packet. Setting a later deadline allows subsequent operations to proceed.
A zero time disables deadlines.
func (*LossyConn) SetDelayDeviation ¶
SetDelayDeviation sets the delay variation's standard deviation in milliseconds. The value must be finite and non-negative. The configured base delay is unchanged.
func (*LossyConn) SetLinkState ¶ added in v1.1.0
SetLinkState enables or disables the default outgoing link profile. Explicit peer profiles are not affected.
func (*LossyConn) SetPeerLinkState ¶ added in v1.1.0
SetPeerLinkState changes the up/down state of addr's outgoing link. Scheduled profiles take precedence once their activation time is reached.
func (*LossyConn) SetPeerProfile ¶ added in v1.1.0
func (conn *LossyConn) SetPeerProfile(addr net.Addr, profile LinkProfile) error
SetPeerProfile sets the outgoing link profile for addr. The profile replaces the default link settings rather than merging with them.
func (*LossyConn) SetReadDeadline ¶
SetReadDeadline sets the deadline for current and future ReadFrom calls. A zero time disables the read deadline.
func (*LossyConn) SetWriteDeadline ¶
SetWriteDeadline sets the deadline checked by current and future WriteTo calls. WriteTo does not block while waiting for delivery. A zero time disables the write deadline.
func (*LossyConn) Write ¶
Write implements net.Conn Write. LossyConn is packet-oriented, so stream writes are unsupported.
type Packet ¶
type Packet struct {
// contains filtered or unexported fields
}
Packet is a payload scheduled for delivery to a simulated connection.
type Stats ¶ added in v1.1.0
type Stats struct {
// Packets discarded by independent random loss.
DropRandom uint64
// Packets discarded by burst loss.
DropBurst uint64
// Packets discarded because the receive queue was full.
DropQueue uint64
// Packets discarded because their payload exceeded MaxPacketSize.
DropOversize uint64
// Packets discarded after their configured lifetime elapsed in transit.
DropExpired uint64
// Packets discarded while the outgoing link was disabled.
DropLinkDown uint64
// Additional packet copies scheduled by simulated duplication.
DuplicatedPackets uint64
// Accepted outgoing packets whose payload was changed by simulated corruption.
CorruptedPackets uint64
// Writes rejected because the outgoing queue was full or its sender was closed.
DropPendingQueue uint64
// Packets accepted by WriteTo, including simulated losses.
SentPackets uint64
// Packets accepted into the receive queue.
RecvPackets uint64
// Payload bytes accepted by WriteTo.
SentBytes uint64
// Payload bytes accepted into the receive queue.
RecvBytes uint64
// Largest observed delivery delay.
MaxDelay time.Duration
// Packets given an extra delay for reordering.
Reordered uint64
// Packets given additional congestion delay from the pending send queue.
QueueDelayedPackets uint64
// Packets scheduled for delivery after a non-zero delay.
DelayedSends uint64
}
Stats is a snapshot of a connection's packet, byte, loss, and delay counters.
type TimedSender ¶
type TimedSender struct {
// contains filtered or unexported fields
}
TimedSender delivers packets to connections at their scheduled times. Call Close when the sender is no longer needed.
func NewTimedSender ¶
func NewTimedSender() *TimedSender
NewTimedSender creates an unbounded sender. Call Close when finished.
func (*TimedSender) Close ¶ added in v1.1.0
func (h *TimedSender) Close() error
Close stops the sender and discards packets that have not yet been delivered. It is safe to call Close more than once.
func (*TimedSender) Len ¶
func (h *TimedSender) Len() int
func (*TimedSender) Less ¶
func (h *TimedSender) Less(i, j int) bool
func (*TimedSender) Pop ¶
func (h *TimedSender) Pop() interface{}
func (*TimedSender) Push ¶
func (h *TimedSender) Push(x interface{})
func (*TimedSender) Send ¶
func (h *TimedSender) Send(conn *LossyConn, packet Packet, delay time.Duration)
Send schedules packet for delivery after delay. Calls made after Close are ignored.
func (*TimedSender) Swap ¶
func (h *TimedSender) Swap(i, j int)