github.com/cloudwego/kitex@v0.9.0/client/context.go (about) 1 /* 2 * Copyright 2021 CloudWeGo 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 client 18 19 import ( 20 "context" 21 22 "github.com/cloudwego/kitex/client/callopt" 23 ) 24 25 type ctxKeyType int 26 27 const ( 28 ctxCallOptionKey ctxKeyType = iota 29 ctxCallOptionInfoKey 30 ) 31 32 // NewCtxWithCallOptions returns a new context associated with the given call-Options. 33 func NewCtxWithCallOptions(ctx context.Context, opts []callopt.Option) context.Context { 34 if ctx == nil || len(opts) == 0 { 35 return ctx 36 } 37 return context.WithValue(ctx, ctxCallOptionKey, opts) 38 } 39 40 // CallOptionsFromCtx retrieves call-Options from the given context. 41 func CallOptionsFromCtx(ctx context.Context) (res []callopt.Option) { 42 if ctx == nil { 43 return 44 } 45 res, _ = ctx.Value(ctxCallOptionKey).([]callopt.Option) 46 return 47 } 48 49 // CallOptionInfoFromCtx retrieves the call Options debug information from the given context. 50 func CallOptionInfoFromCtx(ctx context.Context) (res string) { 51 if ctx != nil { 52 res, _ = ctx.Value(ctxCallOptionInfoKey).(string) 53 } 54 return 55 }