github.com/pingcap/chaos@v0.0.0-20190710112158-c86faf4b3719/pkg/util/net/net.go (about)

     1  package net
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  // SlowOptions is used to delay the network packets.
     9  type SlowOptions struct {
    10  	Mean         time.Duration
    11  	Variance     time.Duration
    12  	Distribution string
    13  }
    14  
    15  // DefaultSlowOptions returns a default options.
    16  func DefaultSlowOptions() SlowOptions {
    17  	return SlowOptions{
    18  		Mean:         50 * time.Millisecond,
    19  		Variance:     10 * time.Millisecond,
    20  		Distribution: "normal",
    21  	}
    22  }
    23  
    24  // Net is used to control the network.
    25  type Net interface {
    26  	// Drop runs on the node and drops traffic from srcNode.
    27  	Drop(ctx context.Context, node string, srcNode string) error
    28  	// Heal runs on the node and ends all traffic drops and restores network to fast operations.
    29  	Heal(ctx context.Context, node string) error
    30  	// Slow runs on the node and delays the network packets with options.
    31  	Slow(ctx context.Context, node string, opts SlowOptions) error
    32  	// Flaky runs on the node and introduces randomized packet loss.
    33  	Flaky(ctx context.Context, node string) error
    34  	// Fast runs on the node and removes packet loss and delays.
    35  	Fast(ctx context.Context, node string) error
    36  }
    37  
    38  // Noop implements Net interface but does nothing.
    39  type Noop struct {
    40  	Net
    41  }
    42  
    43  // Drop drops traffic from node.
    44  func (Noop) Drop(ctx context.Context, node string, srcNode string) error { return nil }
    45  
    46  // Heal ends all traffic drops and restores network to fast operations.
    47  func (Noop) Heal(ctx context.Context, node string) error { return nil }
    48  
    49  // Slow delays the network packets with opetions.
    50  func (Noop) Slow(ctx context.Context, node string, opts SlowOptions) error { return nil }
    51  
    52  // Flaky introduces randomized packet loss.
    53  func (Noop) Flaky(ctx context.Context, node string) error { return nil }
    54  
    55  // Fast removes packet loss and delays.
    56  func (Noop) Fast(ctx context.Context, node string) error { return nil }