Documentation ¶
Overview ¶
Package trafficshape provides tools for simulating latency and bandwidth at the network layer.
Index ¶
- Variables
- type Action
- type Bandwidth
- type Bucket
- func (b *Bucket) Capacity() int64
- func (b *Bucket) Close() error
- func (b *Bucket) Fill(fn func(int64) (int64, error)) (int64, error)
- func (b *Bucket) FillThrottle(fn func(int64) (int64, error)) (int64, error)
- func (b *Bucket) FillThrottleLocked(fn func(int64) (int64, error)) (int64, error)
- func (b *Bucket) SetCapacity(capacity int64)
- type Buckets
- type ChangeBandwidth
- type CloseConnection
- type ConfigRequest
- type Conn
- func (c *Conn) CheckExistenceAndValidity(URLRegex string) bool
- func (c *Conn) Close() error
- func (c *Conn) GetCurrentThrottle(start int64) *ThrottleContext
- func (c *Conn) GetNextActionFromByte(start int64) *NextActionInfo
- func (c *Conn) GetNextActionFromIndex(ind int64) *NextActionInfo
- func (c *Conn) GetWrappedConn() net.Conn
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) Read(b []byte) (int, error)
- func (c *Conn) ReadFrom(r io.Reader) (int64, error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetWriteDeadline(t time.Time) error
- func (c *Conn) Write(b []byte) (int, error)
- func (c *Conn) WriteDefaultBuckets(b []byte) (int, error)
- func (c *Conn) WriteTo(w io.Writer) (int64, error)
- type Context
- type Default
- type ErrForceClose
- type Halt
- type Handler
- type Listener
- func (l *Listener) Accept() (net.Conn, error)
- func (l *Listener) Close() error
- func (l *Listener) Defaults() *Default
- func (l *Listener) GetTrafficShapedConn(oc net.Conn) *Conn
- func (l *Listener) Latency() time.Duration
- func (l *Listener) ReadBitrate() int64
- func (l *Listener) SetDefaults(defaults *Default)
- func (l *Listener) SetLatency(latency time.Duration)
- func (l *Listener) SetReadBitrate(bitrate int64)
- func (l *Listener) SetWriteBitrate(bitrate int64)
- func (l *Listener) WriteBitrate() int64
- type NextActionInfo
- type Shape
- type Throttle
- type ThrottleContext
- type Trafficshape
Constants ¶
This section is empty.
Variables ¶
var DefaultBitrate int64 = 500000000000 // 500Gbps (unlimited)
DefaultBitrate represents the bitrate that will be for all url regexs for which a shape has not been specified.
var ( // ErrBucketOverflow is an error that indicates the bucket has been overflown // by the user. This error is only returned iff fill > capacity. ErrBucketOverflow = errors.New("trafficshape: bucket overflow") )
Functions ¶
This section is empty.
Types ¶
type Action ¶
type Action interface {
// contains filtered or unexported methods
}
Action represents an arbitrary event that needs to be executed while writing back to the client.
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
Bucket is a generic leaky bucket that drains at a configurable interval and fills at user defined rate. The bucket may be used concurrently.
func (*Bucket) Fill ¶
Fill calls fn with the available capacity remaining (capacity-fill) and fills the bucket with the number of tokens returned by fn. If the remaining capacity is 0, Fill returns 0, nil. If the remaining capacity is < 0, Fill returns 0, ErrBucketOverflow.
If fn returns an error, it will be returned by Fill along with the remaining capacity.
fn is provided the remaining capacity as a soft maximum, fn is allowed to use more than the remaining capacity without incurring spillage, though this will cause subsequent calls to Fill to return ErrBucketOverflow until the next drain.
If the bucket is closed when Fill is called, fn will not be executed and Fill will return with an error.
func (*Bucket) FillThrottle ¶
FillThrottle calls fn with the available capacity remaining (capacity-fill) and fills the bucket with the number of tokens returned by fn. If the remaining capacity is <= 0, FillThrottle will wait for the next drain before running fn.
If fn returns an error, it will be returned by FillThrottle along with the number of tokens processed by fn.
fn is provided the remaining capacity as a soft maximum, fn is allowed to use more than the remaining capacity without incurring spillage.
If the bucket is closed when FillThrottle is called, or while waiting for the next drain, fn will not be executed and FillThrottle will return with an error.
func (*Bucket) FillThrottleLocked ¶
FillThrottleLocked is like FillThrottle, except that it uses a lock to protect the critical section between accessing the fill value and updating it.
func (*Bucket) SetCapacity ¶
SetCapacity sets the capacity for the bucket and resets the fill to zero.
type Buckets ¶
Buckets contains the read and write buckets for a url_regex.
func NewBuckets ¶
NewBuckets returns a *Buckets with the specified up and down bandwidths.
type ChangeBandwidth ¶
ChangeBandwidth represents the event of changing the current bandwidth. It is used as an endpoint of a Throttle. It implements the Action interface.
type CloseConnection ¶
CloseConnection is an event that represents the closing of a connection with a client. It implements the Action interface.
type ConfigRequest ¶
type ConfigRequest struct {
Trafficshape *Trafficshape `json:"trafficshape"`
}
ConfigRequest represents a request to configure the global traffic shape.
type Conn ¶
type Conn struct { Context *Context // Shapes represents the traffic shape map inherited from the listener. Shapes *urlShapes GlobalBuckets map[string]*Bucket // LocalBuckets represents a map from the url_regexes to their dedicated buckets. LocalBuckets map[string]*Buckets Established time.Time // Established is the time that the connection is established. DefaultBandwidth Bandwidth Listener *Listener ReadBucket *Bucket // Shared by listener. WriteBucket *Bucket // Shared by listener. // contains filtered or unexported fields }
Conn wraps a net.Conn and simulates connection latency and bandwidth charateristics.
func (*Conn) CheckExistenceAndValidity ¶
CheckExistenceAndValidity checks that the current url regex is present in the map, and that the connection was established before the url shape map was last updated. We do not allow the updated url shape map to traffic shape older connections. Important: Assumes you have acquired the required locks and will release them youself.
func (*Conn) Close ¶
Close closes the connection. Any blocked Read or Write operations will be unblocked and return errors.
func (*Conn) GetCurrentThrottle ¶
func (c *Conn) GetCurrentThrottle(start int64) *ThrottleContext
GetCurrentThrottle uses binary search to determine if the current byte offset ('start') lies within a throttle interval. If so, also returns the bandwidth specified for that interval.
func (*Conn) GetNextActionFromByte ¶
func (c *Conn) GetNextActionFromByte(start int64) *NextActionInfo
GetNextActionFromByte takes in a byte offset and uses binary search to determine the upcoming action, i.e the first action after the byte that still has a non zero count.
func (*Conn) GetNextActionFromIndex ¶
func (c *Conn) GetNextActionFromIndex(ind int64) *NextActionInfo
GetNextActionFromIndex takes in an index and returns the first action after the index that has a non zero count, if there is one.
func (*Conn) GetWrappedConn ¶
GetWrappedConn returns the undrelying trafficshaped net.Conn.
func (*Conn) Read ¶
Read reads bytes from connection into b, optionally simulating connection latency and throttling read throughput based on desired bandwidth constraints.
func (*Conn) ReadFrom ¶
ReadFrom reads data from r until EOF or error, optionally simulating connection latency and throttling read throughput based on desired bandwidth constraints.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the remote network address.
func (*Conn) SetDeadline ¶
SetDeadline sets the read and write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
A deadline is an absolute time after which I/O operations fail with a timeout (see type Error) instead of blocking. The deadline applies to all future and pending I/O, not just the immediately following call to Read or Write. After a deadline has been exceeded, the connection can be refreshed by setting a deadline in the future.
An idle timeout can be implemented by repeatedly extending the deadline after successful Read or Write calls.
A zero value for t means I/O operations will not time out.
Note that if a TCP connection has keep-alive turned on, which is the default unless overridden by Dialer.KeepAlive or ListenConfig.KeepAlive, then a keep-alive failure may also return a timeout error. On Unix systems a keep-alive failure on I/O can be detected using errors.Is(err, syscall.ETIMEDOUT).
func (*Conn) SetReadDeadline ¶
SetReadDeadline sets the deadline for future Read calls and any currently-blocked Read call. A zero value for t means Read will not time out.
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline sets the deadline for future Write calls and any currently-blocked Write call. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means Write will not time out.
func (*Conn) Write ¶
Write writes bytes from b to the connection, while enforcing throttles and performing actions. It uses and updates the Context in the connection.
func (*Conn) WriteDefaultBuckets ¶
WriteDefaultBuckets writes bytes from b to the connection, optionally simulating connection latency and throttling write throughput based on desired bandwidth constraints. It uses the WriteBucket inherited from the listener.
type Context ¶
type Context struct { Shaping bool RangeStart int64 URLRegex string Buckets *Buckets GlobalBucket *Bucket ThrottleContext *ThrottleContext NextActionInfo *NextActionInfo ByteOffset int64 HeaderLen int64 HeaderBytesWritten int64 }
Context represents the current information that is needed while writing back to the client. Only if Shaping is true, that is we are currently writing back a response that matches a certain url_regex will the other values be set correctly. If so, the Buckets represent the buckets to be used for the current url_regex. NextActionInfo tells us whether there is an upcoming action that needs to be performed, and ThrottleContext tells us whether we are currently in a throttle interval (according to the RangeStart). Note, the ThrottleContext is only used once in the start to determine the beginning bandwidth. It need not be updated after that. This is because the subsequent throttles are captured in the upcoming ChangeBandwidth actions. Byte Offset represents the absolute byte offset of response data that we are currently writing back. It does not account for the header data.
type Default ¶
Default encloses information about the default traffic shaping parameters: bandwidth and latency.
type ErrForceClose ¶
type ErrForceClose struct {
// contains filtered or unexported fields
}
ErrForceClose is an error that communicates the need to close the connection.
func (*ErrForceClose) Error ¶
func (efc *ErrForceClose) Error() string
type Halt ¶
type Halt struct { Byte int64 `json:"byte"` Duration int64 `json:"duration"` Count int64 `json:"count"` }
Halt is the event that represents a period of time to sleep while writing. It implements the Action interface.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler configures a trafficshape.Listener.
func NewHandler ¶
NewHandler returns an http.Handler to configure traffic shaping.
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP configures latency and bandwidth constraints.
The "latency" query string parameter accepts a duration string in any format supported by time.ParseDuration. The "up" and "down" query string parameters accept integers as bits per second to be used for read and write throughput.
type Listener ¶
type Listener struct { net.Listener ReadBucket *Bucket WriteBucket *Bucket GlobalBuckets map[string]*Bucket Shapes *urlShapes // contains filtered or unexported fields }
Listener wraps a net.Listener and simulates connection latency and bandwidth constraints.
func NewListener ¶
NewListener returns a new bandwidth constrained listener. Defaults to DefaultBitrate (uncapped).
func (*Listener) Close ¶
Close closes the read and write buckets along with the underlying listener.
func (*Listener) Defaults ¶
Defaults returns the default traffic shaping parameters for the listener.
func (*Listener) GetTrafficShapedConn ¶
GetTrafficShapedConn takes in a normal connection and returns a traffic shaped connection.
func (*Listener) ReadBitrate ¶
ReadBitrate returns the bitrate in bits per second for reads.
func (*Listener) SetDefaults ¶
SetDefaults sets the default traffic shaping parameters for the listener.
func (*Listener) SetLatency ¶
SetLatency sets the initial latency for connections.
func (*Listener) SetReadBitrate ¶
SetReadBitrate sets the bitrate in bits per second for reads.
func (*Listener) SetWriteBitrate ¶
SetWriteBitrate sets the bitrate in bits per second for writes.
func (*Listener) WriteBitrate ¶
WriteBitrate returns the bitrate in bits per second for writes.
type NextActionInfo ¶
NextActionInfo represents whether there is an upcoming action. Only if ActionNext is true will the Index and ByteOffset be set correctly.
type Shape ¶
type Shape struct { URLRegex string `json:"url_regex"` MaxBandwidth int64 `json:"max_global_bandwidth"` Throttles []*Throttle `json:"throttles"` Halts []*Halt `json:"halts"` CloseConnections []*CloseConnection `json:"close_connections"` // Actions are populated after processing Throttles, Halts and CloseConnections. // Actions is sorted in the order of byte offset. Actions []Action // WriteBucket is initialized by us using MaxBandwidth. WriteBucket *Bucket }
Shape encloses the traffic shape of a particular url regex.
type Throttle ¶
type Throttle struct { Bytes string `json:"bytes"` Bandwidth int64 `json:"bandwidth"` ByteStart int64 ByteEnd int64 }
Throttle represents a byte interval with a specific bandwidth.
type ThrottleContext ¶
ThrottleContext represents whether we are currently in a throttle interval for a particular url_regex. If ThrottleNow is true, only then will the current throttle 'Bandwidth' be set correctly.
type Trafficshape ¶
Trafficshape contains global shape of traffic, i.e information about shape of each url specified and the default traffic shaping parameters.