Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

latency: don't wrap when all the latencies are zero #7506

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions benchmark/latency/latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,18 @@ var (
Longhaul = Network{1000 * 1024, 200 * time.Millisecond, 9000}
)

func (n *Network) isLocal() bool {
return *n == Local
}

// 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 (n *Network) Conn(c net.Conn) (net.Conn, error) {
if n.isLocal() {
return c, nil
}
start := now()
nc := &conn{Conn: c, network: n, readBuf: new(bytes.Buffer)}
if err := nc.sync(); err != nil {
Expand Down Expand Up @@ -246,6 +253,9 @@ func (c *conn) sync() error {
// Listener returns a net.Listener that wraps l and injects n's latency in its
// connections.
func (n *Network) Listener(l net.Listener) net.Listener {
if n.isLocal() {
return l
}
return &listener{Listener: l, network: n}
}

Expand All @@ -265,6 +275,9 @@ func (l *listener) Accept() (net.Conn, error) {
// 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 (n *Network) Dialer(d Dialer) Dialer {
if n.isLocal() {
return d
}
return func(network, address string) (net.Conn, error) {
conn, err := d(network, address)
if err != nil {
Expand All @@ -278,6 +291,9 @@ func (n *Network) Dialer(d Dialer) Dialer {
// in its connections. n's Latency is also injected to the connection's
// creation.
func (n *Network) TimeoutDialer(d TimeoutDialer) TimeoutDialer {
if n.isLocal() {
return d
}
return func(network, address string, timeout time.Duration) (net.Conn, error) {
conn, err := d(network, address, timeout)
if err != nil {
Expand All @@ -291,6 +307,9 @@ func (n *Network) TimeoutDialer(d TimeoutDialer) TimeoutDialer {
// in its connections. n's Latency is also injected to the connection's
// creation.
func (n *Network) ContextDialer(d ContextDialer) ContextDialer {
if n.isLocal() {
return d
}
return func(ctx context.Context, network, address string) (net.Conn, error) {
conn, err := d(ctx, network, address)
if err != nil {
Expand Down
Loading