k8s.io/apiserver@v0.31.1/pkg/endpoints/request/server_shutdown_signal.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 request 18 19 import ( 20 "context" 21 ) 22 23 // The serverShutdownSignalKeyType type is unexported to prevent collisions 24 type serverShutdownSignalKeyType int 25 26 // serverShutdownSignalKey is the context key for storing the 27 // watch termination interface instance for a WATCH request. 28 const serverShutdownSignalKey serverShutdownSignalKeyType = iota 29 30 // ServerShutdownSignal is associated with the request context so 31 // the request handler logic has access to signals rlated to 32 // the server shutdown events 33 type ServerShutdownSignal interface { 34 // Signaled when the apiserver is not receiving any new request 35 ShuttingDown() <-chan struct{} 36 } 37 38 // ServerShutdownSignalFrom returns the ServerShutdownSignal instance 39 // associated with the request context. 40 // If there is no ServerShutdownSignal asscoaied with the context, 41 // nil is returned. 42 func ServerShutdownSignalFrom(ctx context.Context) ServerShutdownSignal { 43 ev, _ := ctx.Value(serverShutdownSignalKey).(ServerShutdownSignal) 44 return ev 45 } 46 47 // WithServerShutdownSignal returns a new context that stores 48 // the ServerShutdownSignal interface instance. 49 func WithServerShutdownSignal(parent context.Context, window ServerShutdownSignal) context.Context { 50 if ServerShutdownSignalFrom(parent) != nil { 51 return parent // Avoid double registering. 52 } 53 54 return context.WithValue(parent, serverShutdownSignalKey, window) 55 }