grpc-go如何通過(guò)context傳遞額外數(shù)據(jù)
使用 ctx.Value 從 context 讀取數(shù)據(jù)
// ValueFromIncomingContext returns the metadata value corresponding to the metadata // key from the incoming metadata if it exists. Key must be lower-case. // // # Experimental // // Notice: This API is EXPERIMENTAL and may be changed or removed in a // later release. func ValueFromIncomingContext(ctx context.Context, key string) []string { md, ok := ctx.Value(mdIncomingKey{}).(MD) if !ok { return nil } if v, ok := md[key]; ok { return copyOf(v) } for k, v := range md { // We need to manually convert all keys to lower case, because MD is a // map, and there's no guarantee that the MD attached to the context is // created using our helper functions. if strings.ToLower(k) == key { return copyOf(v) } } return nil }
使用 ctx.Value 往 context 寫(xiě)入數(shù)據(jù)
// AppendToOutgoingContext returns a new context with the provided kv merged // with any existing metadata in the context. Please refer to the documentation // of Pairs for a description of kv. func AppendToOutgoingContext(ctx context.Context, kv ...string) context.Context { if len(kv)%2 == 1 { panic(fmt.Sprintf("metadata: AppendToOutgoingContext got an odd number of input pairs for metadata: %d", len(kv))) } md, _ := ctx.Value(mdOutgoingKey{}).(rawMD) added := make([][]string, len(md.added)+1) copy(added, md.added) kvCopy := make([]string, 0, len(kv)) for i := 0; i < len(kv); i += 2 { kvCopy = append(kvCopy, strings.ToLower(kv[i]), kv[i+1]) } added[len(added)-1] = kvCopy return context.WithValue(ctx, mdOutgoingKey{}, rawMD{md: md.md, added: added}) }
metadata 是 grpc 內(nèi)置的,用來(lái)往 RPC 服務(wù)傳遞 http 頭數(shù)據(jù),分 in 和 out 兩種,對(duì)應(yīng)的 key 都為一個(gè)空 struct,分別為:mdIncomingKey 和 mdOutgoingKey 。
服務(wù)端的 ctx 和 md 直接打印出來(lái),如下樣子:
fmt.Println(ctx) fmt.Println(md) context.Background.WithValue(type transport.connectionKey, val <not Stringer>).WithValue(type peer.peerKey, val <not Stringer>).WithDeadline(2024-02-19 10:02:43.212614653 +0800 CST m=+41018.106555206 [1.999790196s]).WithValue(type metadata.mdIncomingKey, val <not Stringer>).WithValue(type grpc.streamKey, val <not Stringer>).WithValue(type baggage.baggageContextKeyType, val <not Stringer>).WithValue(type trace.traceContextKeyType, val <not Stringer>).WithValue(type trace.traceContextKeyType, val <not Stringer>).WithCancel map[:authority:[add.rpc] append:[append-value] content-type:[application/grpc] extra:[extra-value] grpc-accept-encoding:[gzip] noncestr:[abc] signature:[0123456789] timestamp:[2021-07-01 00:00:00] traceparent:[00-89415f99d44e6f8f6e14e3fe8f13ad20-bf33b29c4362ca6a-00] user-agent:[grpc-go/1.59.0]] signature: [0123456789]
注意 md 中的值會(huì)被加上中括號(hào)“[]”。
到此這篇關(guān)于grpc-go通過(guò)context傳遞額外數(shù)據(jù)的文章就介紹到這了,更多相關(guān)grpc-go context傳遞額外數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
go mock server的簡(jiǎn)易實(shí)現(xiàn)示例
這篇文章主要為大家介紹了go mock server的簡(jiǎn)易實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Go 循環(huán)結(jié)構(gòu)for循環(huán)使用教程全面講解
這篇文章主要為大家介紹了Go 循環(huán)結(jié)構(gòu)for循環(huán)使用全面講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10golang之?dāng)?shù)據(jù)驗(yàn)證validator的實(shí)現(xiàn)
這篇文章主要介紹了golang之?dāng)?shù)據(jù)驗(yàn)證validator的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10通過(guò)Golang實(shí)現(xiàn)linux命令ls命令(命令行工具構(gòu)建)
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Golang實(shí)現(xiàn)一個(gè)linux命令ls命令(命令行工具構(gòu)建),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解一下2023-01-01分析Go錯(cuò)誤處理優(yōu)化go?recover機(jī)制缺陷
這篇文章主要為大家介紹了分析Go錯(cuò)誤處理優(yōu)化go?recover機(jī)制缺陷示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Golang打印復(fù)雜結(jié)構(gòu)體兩種方法詳解
在?Golang?語(yǔ)言開(kāi)發(fā)中,我們經(jīng)常會(huì)使用結(jié)構(gòu)體類(lèi)型,如果我們使用的結(jié)構(gòu)體類(lèi)型的變量包含指針類(lèi)型的字段,我們?cè)谟涗浫罩镜臅r(shí)候,指針類(lèi)型的字段的值是指針地址,將會(huì)給我們?debug?代碼造成不便2022-10-10Go語(yǔ)言中?Print?Printf和Println?的區(qū)別解析
這篇文章主要介紹了Go語(yǔ)言中?Print?Printf和Println?的區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03