...

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 2021 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package netem
    17  
    18  import (
    19  	"math"
    20  
    21  	chaosdaemon "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    22  )
    23  
    24  // MergeNetem merges two Netem protos into a new one.
    25  // REMEMBER to assign the return value, i.e. merged = utils.MergeNetm(merged, em)
    26  // For each field it takes the bigger value of the two.
    27  // Its main use case is merging netem of different types, e.g. delay and loss.
    28  // It returns nil if both inputs are nil.
    29  // Otherwise it returns a new Netem with merged values.
    30  func MergeNetem(a, b *chaosdaemon.Netem) *chaosdaemon.Netem {
    31  	if a == nil && b == nil {
    32  		return nil
    33  	}
    34  	// NOTE: because proto getters check nil, we are good here even if one of them is nil.
    35  	// But we just assign empty value to make IDE and linters happy.
    36  	if a == nil {
    37  		a = &chaosdaemon.Netem{}
    38  	}
    39  	if b == nil {
    40  		b = &chaosdaemon.Netem{}
    41  	}
    42  	return &chaosdaemon.Netem{
    43  		Time:          maxu32(a.GetTime(), b.GetTime()),
    44  		Jitter:        maxu32(a.GetJitter(), b.GetJitter()),
    45  		DelayCorr:     maxf32(a.GetDelayCorr(), b.GetDelayCorr()),
    46  		Limit:         maxu32(a.GetLimit(), b.GetLimit()),
    47  		Loss:          maxf32(a.GetLoss(), b.GetLoss()),
    48  		LossCorr:      maxf32(a.GetLossCorr(), b.GetLossCorr()),
    49  		Gap:           maxu32(a.GetGap(), b.GetGap()),
    50  		Duplicate:     maxf32(a.GetDuplicate(), b.GetDuplicate()),
    51  		DuplicateCorr: maxf32(a.GetDuplicateCorr(), b.GetDuplicateCorr()),
    52  		Reorder:       maxf32(a.GetReorder(), b.GetReorder()),
    53  		ReorderCorr:   maxf32(a.GetReorderCorr(), b.GetReorderCorr()),
    54  		Corrupt:       maxf32(a.GetCorrupt(), b.GetCorrupt()),
    55  		CorruptCorr:   maxf32(a.GetCorruptCorr(), b.GetCorruptCorr()),
    56  	}
    57  }
    58  
    59  func maxu32(a, b uint32) uint32 {
    60  	if a > b {
    61  		return a
    62  	}
    63  	return b
    64  }
    65  
    66  func maxf32(a, b float32) float32 {
    67  	return float32(math.Max(float64(a), float64(b)))
    68  }
    69