k8s.io/apiserver@v0.31.1/pkg/server/routine/routine.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 routine
    18  
    19  import (
    20  	"context"
    21  	"net/http"
    22  
    23  	"k8s.io/apiserver/pkg/endpoints/request"
    24  )
    25  
    26  type taskKeyType int
    27  
    28  const taskKey taskKeyType = iota
    29  
    30  type Task struct {
    31  	Func func()
    32  }
    33  
    34  func WithTask(parent context.Context, t *Task) context.Context {
    35  	return request.WithValue(parent, taskKey, t)
    36  }
    37  
    38  // AppendTask appends a task executed after completion of existing task.
    39  // It is a no-op if there is no existing task.
    40  func AppendTask(ctx context.Context, t *Task) bool {
    41  	if existTask := TaskFrom(ctx); existTask != nil && existTask.Func != nil {
    42  		existFunc := existTask.Func
    43  		existTask.Func = func() {
    44  			existFunc()
    45  			t.Func()
    46  		}
    47  		return true
    48  	}
    49  	return false
    50  }
    51  
    52  func TaskFrom(ctx context.Context) *Task {
    53  	t, _ := ctx.Value(taskKey).(*Task)
    54  	return t
    55  }
    56  
    57  // WithRoutine returns an http.Handler that executes preparation of long running requests (i.e. watches)
    58  // in a separate Goroutine and then serves the long running request in the main Goroutine. Doing so allows
    59  // freeing stack memory used in preparation Goroutine for better memory efficiency.
    60  func WithRoutine(handler http.Handler, longRunning request.LongRunningRequestCheck) http.Handler {
    61  	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    62  		ctx := req.Context()
    63  		requestInfo, _ := request.RequestInfoFrom(ctx)
    64  		if !longRunning(req, requestInfo) {
    65  			handler.ServeHTTP(w, req)
    66  			return
    67  		}
    68  
    69  		req = req.WithContext(WithTask(ctx, &Task{}))
    70  		panicCh := make(chan any, 1)
    71  		go func() {
    72  			defer func() {
    73  				if r := recover(); r != nil {
    74  					panicCh <- r
    75  				}
    76  				close(panicCh)
    77  			}()
    78  			handler.ServeHTTP(w, req)
    79  		}()
    80  
    81  		if p, ok := <-panicCh; ok {
    82  			panic(p)
    83  		}
    84  
    85  		ctx = req.Context()
    86  		if t := TaskFrom(ctx); t != nil && t.Func != nil {
    87  			t.Func()
    88  		}
    89  
    90  	})
    91  }