k8s.io/apiserver@v0.31.1/pkg/util/flowcontrol/fairqueuing/testing/promise/counting.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 promise
    18  
    19  import (
    20  	"sync"
    21  
    22  	"k8s.io/apimachinery/pkg/util/runtime"
    23  	"k8s.io/apiserver/pkg/util/flowcontrol/counter"
    24  	promiseifc "k8s.io/apiserver/pkg/util/flowcontrol/fairqueuing/promise"
    25  )
    26  
    27  // countingPromise implements the WriteOnce interface.
    28  // This implementation is based on a condition variable.
    29  // This implementation tracks active goroutines:
    30  // the given counter is decremented for a goroutine waiting for this
    31  // varible to be set and incremented when such a goroutine is
    32  // unblocked.
    33  type countingPromise struct {
    34  	lock          sync.Locker
    35  	cond          sync.Cond
    36  	activeCounter counter.GoRoutineCounter // counter of active goroutines
    37  	waitingCount  int                      // number of goroutines idle due to this being unset
    38  	isSet         bool
    39  	value         interface{}
    40  }
    41  
    42  var _ promiseifc.WriteOnce = &countingPromise{}
    43  
    44  // NewCountingWriteOnce creates a WriteOnce that uses locking and counts goroutine activity.
    45  //
    46  // The final three arguments are like those for a regular WriteOnce factory:
    47  // - an optional initial value,
    48  // - an optional "done" channel,
    49  // - the value that is Set after the "done" channel becomes selectable.
    50  // Note that for this implementation, the reaction to `doneCh`
    51  // becoming selectable does not wait for a Get.
    52  // If `doneCh != nil` then the caller promises to close it reasonably promptly
    53  // (to the degree allowed by the Go runtime scheduler), and increment the
    54  // goroutine counter before that.
    55  // The WriteOnce's Get method must be called without the lock held.
    56  // The WriteOnce's Set method must be called with the lock held.
    57  func NewCountingWriteOnce(activeCounter counter.GoRoutineCounter, lock sync.Locker, initial interface{}, doneCh <-chan struct{}, doneVal interface{}) promiseifc.WriteOnce {
    58  	p := &countingPromise{
    59  		lock:          lock,
    60  		cond:          *sync.NewCond(lock),
    61  		activeCounter: activeCounter,
    62  		isSet:         initial != nil,
    63  		value:         initial,
    64  	}
    65  	if doneCh != nil {
    66  		activeCounter.Add(1) // count start of the following goroutine
    67  		go func() {
    68  			defer activeCounter.Add(-1) // count completion of this goroutine
    69  			defer runtime.HandleCrash()
    70  			activeCounter.Add(-1) // count suspension for channel receive
    71  			<-doneCh
    72  			// Whatever goroutine unblocked the preceding receive MUST
    73  			// have already accounted for this activation.
    74  			lock.Lock()
    75  			defer lock.Unlock()
    76  			p.Set(doneVal)
    77  		}()
    78  	}
    79  	return p
    80  }
    81  
    82  func (p *countingPromise) Get() interface{} {
    83  	p.lock.Lock()
    84  	defer p.lock.Unlock()
    85  	if !p.isSet {
    86  		p.waitingCount++
    87  		p.activeCounter.Add(-1)
    88  		p.cond.Wait()
    89  	}
    90  	return p.value
    91  }
    92  
    93  func (p *countingPromise) Set(value interface{}) bool {
    94  	if p.isSet {
    95  		return false
    96  	}
    97  	p.isSet = true
    98  	p.value = value
    99  	if p.waitingCount > 0 {
   100  		p.activeCounter.Add(p.waitingCount)
   101  		p.waitingCount = 0
   102  		p.cond.Broadcast()
   103  	}
   104  	return true
   105  }