k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/util/checker/checker_map.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package checker
    18  
    19  // Checker is a generic execution that can be stopped at any time.
    20  type Checker interface {
    21  	Stop()
    22  }
    23  
    24  // Map is a map of Checkers.
    25  type Map map[string]Checker
    26  
    27  // NewMap creates new checker map.
    28  func NewMap() Map {
    29  	return make(map[string]Checker)
    30  }
    31  
    32  // Dispose stops all checkers and cleans up the map.
    33  func (cm Map) Dispose() {
    34  	for _, c := range cm {
    35  		c.Stop()
    36  	}
    37  	cm = make(map[string]Checker)
    38  }
    39  
    40  // Add adds checker to the checker map.
    41  func (cm Map) Add(key string, c Checker) {
    42  	if old, exists := cm[key]; exists {
    43  		old.Stop()
    44  	}
    45  	cm[key] = c
    46  }
    47  
    48  // DeleteAndStop stops checker and deletes it if exists.
    49  func (cm Map) DeleteAndStop(key string) bool {
    50  	if old, exists := cm[key]; exists {
    51  		old.Stop()
    52  		delete(cm, key)
    53  		return true
    54  	}
    55  	return false
    56  }