k8s.io/apiserver@v0.31.1/pkg/endpoints/request/received_time.go (about)

     1  /*
     2  Copyright 2020 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  	"time"
    22  )
    23  
    24  type requestReceivedTimestampKeyType int
    25  
    26  // requestReceivedTimestampKey is the ReceivedTimestamp (the time the request reached the apiserver)
    27  // key for the context.
    28  const requestReceivedTimestampKey requestReceivedTimestampKeyType = iota
    29  
    30  // WithReceivedTimestamp returns a copy of parent context in which the ReceivedTimestamp
    31  // (the time the request reached the apiserver) is set.
    32  //
    33  // If the specified ReceivedTimestamp is zero, no value is set and the parent context is returned as is.
    34  func WithReceivedTimestamp(parent context.Context, receivedTimestamp time.Time) context.Context {
    35  	if receivedTimestamp.IsZero() {
    36  		return parent
    37  	}
    38  	return WithValue(parent, requestReceivedTimestampKey, receivedTimestamp)
    39  }
    40  
    41  // ReceivedTimestampFrom returns the value of the ReceivedTimestamp key from the specified context.
    42  func ReceivedTimestampFrom(ctx context.Context) (time.Time, bool) {
    43  	info, ok := ctx.Value(requestReceivedTimestampKey).(time.Time)
    44  	return info, ok
    45  }