...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/netem/utils.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/netem

     1  // Copyright 2020 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package netem
    15  
    16  import (
    17  	"math"
    18  
    19  	chaosdaemon "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    20  )
    21  
    22  // MergeNetem merges two Netem protos into a new one.
    23  // REMEMBER to assign the return value, i.e. merged = utils.MergeNetm(merged, em)
    24  // For each field it takes the bigger value of the two.
    25  // Its main use case is merging netem of different types, e.g. delay and loss.
    26  // It returns nil if both inputs are nil.
    27  // Otherwise it returns a new Netem with merged values.
    28  func MergeNetem(a, b *chaosdaemon.Netem) *chaosdaemon.Netem {
    29  	if a == nil && b == nil {
    30  		return nil
    31  	}
    32  	// NOTE: because proto getters check nil, we are good here even if one of them is nil.
    33  	// But we just assign empty value to make IDE and linters happy.
    34  	if a == nil {
    35  		a = &chaosdaemon.Netem{}
    36  	}
    37  	if b == nil {
    38  		b = &chaosdaemon.Netem{}
    39  	}
    40  	return &chaosdaemon.Netem{
    41  		Time:          maxu32(a.GetTime(), b.GetTime()),
    42  		Jitter:        maxu32(a.GetJitter(), b.GetJitter()),
    43  		DelayCorr:     maxf32(a.GetDelayCorr(), b.GetDelayCorr()),
    44  		Limit:         maxu32(a.GetLimit(), b.GetLimit()),
    45  		Loss:          maxf32(a.GetLoss(), b.GetLoss()),
    46  		LossCorr:      maxf32(a.GetLossCorr(), b.GetLossCorr()),
    47  		Gap:           maxu32(a.GetGap(), b.GetGap()),
    48  		Duplicate:     maxf32(a.GetDuplicate(), b.GetDuplicate()),
    49  		DuplicateCorr: maxf32(a.GetDuplicateCorr(), b.GetDuplicateCorr()),
    50  		Reorder:       maxf32(a.GetReorder(), b.GetReorder()),
    51  		ReorderCorr:   maxf32(a.GetReorderCorr(), b.GetReorderCorr()),
    52  		Corrupt:       maxf32(a.GetCorrupt(), b.GetCorrupt()),
    53  		CorruptCorr:   maxf32(a.GetCorruptCorr(), b.GetCorruptCorr()),
    54  	}
    55  }
    56  
    57  func maxu32(a, b uint32) uint32 {
    58  	if a > b {
    59  		return a
    60  	}
    61  	return b
    62  }
    63  
    64  func maxf32(a, b float32) float32 {
    65  	return float32(math.Max(float64(a), float64(b)))
    66  }
    67