github.com/m3db/m3@v1.5.0/src/dbnode/network/server/tchannelthrift/endpoint.go (about)

     1  // Copyright (c) 2021  Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package tchannelthrift
    22  
    23  import "context"
    24  
    25  // NewContextWithEndpoint creates a new context.Context with the Endpoint set
    26  // as a value.
    27  func NewContextWithEndpoint(ctx context.Context, endpoint Endpoint) context.Context {
    28  	return context.WithValue(ctx, EndpointContextKey, endpoint)
    29  }
    30  
    31  // EndpointFromContext returns the Endpoint within the context
    32  // or Unknown if not available.
    33  func EndpointFromContext(ctx context.Context) Endpoint {
    34  	var (
    35  		endpoint Endpoint
    36  		ok       bool
    37  	)
    38  	val := ctx.Value(EndpointContextKey)
    39  	if val != nil {
    40  		endpoint, ok = val.(Endpoint)
    41  		if !ok {
    42  			endpoint = Unknown
    43  		}
    44  	}
    45  
    46  	return endpoint
    47  }
    48  
    49  // String returns the string value of Endpoint enum.
    50  func (e Endpoint) String() string {
    51  	switch e {
    52  	case AggregateRaw:
    53  		return "AggregateRaw"
    54  	case Fetch:
    55  		return "Fetch"
    56  	case FetchBatchRaw:
    57  		return "FetchBatchRaw"
    58  	case FetchBatchRawV2:
    59  		return "FetchBatchRawV2"
    60  	case FetchTagged:
    61  		return "FetchTagged"
    62  	case Query:
    63  		return "Query"
    64  	case Unknown:
    65  		fallthrough
    66  	default:
    67  		return "Unknown"
    68  	}
    69  }