-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for watch initialization in P&F
- Loading branch information
Showing
8 changed files
with
187 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
staging/src/k8s.io/apiserver/pkg/util/flowcontrol/apf_context.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package flowcontrol | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
type priorityAndFairnessKeyType int | ||
|
||
const ( | ||
// priorityAndFairnessInitializationSignalKey is a key under which | ||
// initialization signal function for watch requests is stored | ||
// in the context. | ||
priorityAndFairnessInitializationSignalKey priorityAndFairnessKeyType = iota | ||
) | ||
|
||
// WithInitializationSignal creates a copy of parent context with | ||
// priority and fairness initialization signal value. | ||
func WithInitializationSignal(ctx context.Context, signal InitializationSignal) context.Context { | ||
return context.WithValue(ctx, priorityAndFairnessInitializationSignalKey, signal) | ||
} | ||
|
||
// initializationSignalFrom returns an initialization signal function | ||
// which when called signals that watch initialization has already finished | ||
// to priority and fairness dispatcher. | ||
func initializationSignalFrom(ctx context.Context) (InitializationSignal, bool) { | ||
signal, ok := ctx.Value(priorityAndFairnessInitializationSignalKey).(InitializationSignal) | ||
return signal, ok && signal != nil | ||
} | ||
|
||
// WatchInitialized sends a signal to priority and fairness dispatcher | ||
// that a given watch request has already been initialized. | ||
func WatchInitialized(ctx context.Context) { | ||
if signal, ok := initializationSignalFrom(ctx); ok { | ||
klog.Errorf("QQQ: Marking watch initialized") | ||
signal.Signal() | ||
} | ||
} | ||
|
||
// InitializationSignal is an interface that allows sending and handling | ||
// initialization signals. | ||
type InitializationSignal interface { | ||
// Signal notifies the dispatcher about finished initialization. | ||
Signal() | ||
// Wait waits for the initialization signal. | ||
Wait() | ||
} | ||
|
||
type initializationSignal struct { | ||
once sync.Once | ||
done chan struct{} | ||
} | ||
|
||
func NewInitializationSignal() InitializationSignal { | ||
return &initializationSignal{ | ||
once: sync.Once{}, | ||
done: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (i *initializationSignal) Signal() { | ||
i.once.Do(func() { close(i.done) }) | ||
} | ||
|
||
func (i *initializationSignal) Wait() { | ||
<-i.done | ||
} |