k8s.io/apiserver@v0.31.1/pkg/util/flowcontrol/max_seats.go (about)

     1  /*
     2  Copyright 2023 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 flowcontrol
    18  
    19  import (
    20  	"sync"
    21  )
    22  
    23  // MaxSeatsTracker is used to track max seats allocatable per priority level from the work estimator
    24  type MaxSeatsTracker interface {
    25  	// GetMaxSeats returns the maximum seats a request should occupy for a given priority level.
    26  	GetMaxSeats(priorityLevelName string) uint64
    27  
    28  	// SetMaxSeats configures max seats for a priority level.
    29  	SetMaxSeats(priorityLevelName string, maxSeats uint64)
    30  
    31  	// ForgetPriorityLevel removes max seats tracking for a priority level.
    32  	ForgetPriorityLevel(priorityLevelName string)
    33  }
    34  
    35  type maxSeatsTracker struct {
    36  	sync.RWMutex
    37  
    38  	maxSeats map[string]uint64
    39  }
    40  
    41  func NewMaxSeatsTracker() MaxSeatsTracker {
    42  	return &maxSeatsTracker{
    43  		maxSeats: make(map[string]uint64),
    44  	}
    45  }
    46  
    47  func (m *maxSeatsTracker) GetMaxSeats(plName string) uint64 {
    48  	m.RLock()
    49  	defer m.RUnlock()
    50  
    51  	return m.maxSeats[plName]
    52  }
    53  
    54  func (m *maxSeatsTracker) SetMaxSeats(plName string, maxSeats uint64) {
    55  	m.Lock()
    56  	defer m.Unlock()
    57  
    58  	m.maxSeats[plName] = maxSeats
    59  }
    60  
    61  func (m *maxSeatsTracker) ForgetPriorityLevel(plName string) {
    62  	m.Lock()
    63  	defer m.Unlock()
    64  
    65  	delete(m.maxSeats, plName)
    66  }