Debian Codegraph @

pkg/util/flowcontrol/apf_context.go

raw
1 /*
2 Copyright 2021 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 "context"
21 "sync"
22 )
23
24 type priorityAndFairnessKeyType int
25
26 const (
27 // priorityAndFairnessInitializationSignalKey is a key under which
28 // initialization signal function for watch requests is stored
29 // in the context.
30 priorityAndFairnessInitializationSignalKey priorityAndFairnessKeyType = iota
31 )
32
33 // WithInitializationSignal creates a copy of parent context with
34 // priority and fairness initialization signal value.
35 func WithInitializationSignal(ctx context.Context, signal InitializationSignal) context.Context {
36 return context.WithValue(ctx, priorityAndFairnessInitializationSignalKey, signal)
37 }
38
39 // initializationSignalFrom returns an initialization signal function
40 // which when called signals that watch initialization has already finished
41 // to priority and fairness dispatcher.
42 func initializationSignalFrom(ctx context.Context) (InitializationSignal, bool) {
43 signal, ok := ctx.Value(priorityAndFairnessInitializationSignalKey).(InitializationSignal)
44 return signal, ok && signal != nil
45 }
46
47 // WatchInitialized sends a signal to priority and fairness dispatcher
48 // that a given watch request has already been initialized.
49 func WatchInitialized(ctx context.Context) {
50 if signal, ok := initializationSignalFrom(ctx); ok {
51 signal.Signal()
52 }
53 }
54
55 // RequestDelegated informs the priority and fairness dispatcher that
56 // a given request has been delegated to an aggregated API
57 // server. No-op when priority and fairness is disabled.
58 func RequestDelegated(ctx context.Context) {
59 // The watch initialization signal doesn't traverse request
60 // boundaries, so we generously fire it as soon as we know
61 // that the request won't be serviced locally. Safe to call
62 // for non-watch requests.
63 WatchInitialized(ctx)
64 }
65
66 // InitializationSignal is an interface that allows sending and handling
67 // initialization signals.
68 type InitializationSignal interface {
69 // Signal notifies the dispatcher about finished initialization.
70 Signal()
71 // Wait waits for the initialization signal.
72 Wait()
73 }
74
75 type initializationSignal struct {
76 once sync.Once
77 done chan struct{}
78 }
79
80 func NewInitializationSignal() InitializationSignal {
81 return &initializationSignal{
82 once: sync.Once{},
83 done: make(chan struct{}),
84 }
85 }
86
87 func (i *initializationSignal) Signal() {
88 i.once.Do(func() { close(i.done) })
89 }
90
91 func (i *initializationSignal) Wait() {
92 <-i.done
93 }