trpc.group/trpc-go/trpc-go@v1.0.3/codec/message.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package codec 15 16 import ( 17 "context" 18 "net" 19 "sync" 20 "time" 21 22 "trpc.group/trpc-go/trpc-go/errs" 23 ) 24 25 // ContextKey is trpc context key type, the specific value is judged 26 // by interface, the interface will both judge value and type. Defining 27 // a new type can avoid string value conflict. 28 type ContextKey string 29 30 // MetaData is request penetrate message. 31 type MetaData map[string][]byte 32 33 var msgPool = sync.Pool{ 34 New: func() interface{} { 35 return &msg{} 36 }, 37 } 38 39 // Clone returns a copied meta data. 40 func (m MetaData) Clone() MetaData { 41 if m == nil { 42 return nil 43 } 44 md := MetaData{} 45 for k, v := range m { 46 md[k] = v 47 } 48 return md 49 } 50 51 // CommonMeta is common meta message. 52 type CommonMeta map[interface{}]interface{} 53 54 // Clone returns a copied common meta message. 55 func (c CommonMeta) Clone() CommonMeta { 56 if c == nil { 57 return nil 58 } 59 cm := CommonMeta{} 60 for k, v := range c { 61 cm[k] = v 62 } 63 return cm 64 } 65 66 // trpc context key data 67 const ( 68 ContextKeyMessage = ContextKey("TRPC_MESSAGE") 69 // ServiceSectionLength is the length of service section, 70 // service name example: trpc.app.server.service 71 ServiceSectionLength = 4 72 ) 73 74 // Msg defines core message data for multi protocol, business protocol 75 // should set this message when packing and unpacking data. 76 type Msg interface { 77 // Context returns rpc context 78 Context() context.Context 79 80 // WithRemoteAddr sets upstream address for server, 81 // or downstream address for client. 82 WithRemoteAddr(addr net.Addr) 83 84 // WithLocalAddr sets server local address. 85 WithLocalAddr(addr net.Addr) 86 87 // RemoteAddr returns upstream address for server, 88 // or downstream address for client. 89 RemoteAddr() net.Addr 90 91 // LocalAddr returns server local address. 92 LocalAddr() net.Addr 93 94 // WithNamespace sets server namespace. 95 WithNamespace(string) 96 97 // Namespace returns server namespace. 98 Namespace() string 99 100 // WithEnvName sets server environment. 101 WithEnvName(string) 102 103 // EnvName returns server environment. 104 EnvName() string 105 106 // WithSetName sets server set name. 107 WithSetName(string) 108 109 // SetName returns server set name. 110 SetName() string 111 112 // WithEnvTransfer sets environment message for transfer. 113 WithEnvTransfer(string) 114 115 // EnvTransfer returns environment message for transfer. 116 EnvTransfer() string 117 118 // WithRequestTimeout sets the upstream timeout for server, 119 // or downstream timeout for client. 120 WithRequestTimeout(time.Duration) 121 122 // RequestTimeout returns the upstream timeout for server, 123 // or downstream timeout for client. 124 RequestTimeout() time.Duration 125 126 // WithSerializationType sets serialization type. 127 WithSerializationType(int) 128 129 // SerializationType returns serialization type. 130 SerializationType() int 131 132 // WithCompressType sets compress type. 133 WithCompressType(int) 134 135 // CompressType returns compress type. 136 CompressType() int 137 138 // WithServerRPCName sets server handler method name. 139 WithServerRPCName(string) 140 141 // WithClientRPCName sets client rpc name for downstream. 142 WithClientRPCName(string) 143 144 // ServerRPCName returns method name of current server handler name, 145 // such as /trpc.app.server.service/method. 146 ServerRPCName() string 147 148 // ClientRPCName returns method name of downstream interface. 149 ClientRPCName() string 150 151 // WithCallerServiceName sets caller service name. 152 WithCallerServiceName(string) 153 154 // WithCalleeServiceName sets callee service name. 155 WithCalleeServiceName(string) 156 157 // WithCallerApp sets caller app. For server this app is upstream app, 158 // but for client, is its own app. 159 WithCallerApp(string) 160 161 // WithCallerServer sets caller server. For server this server is upstream server, 162 // but for client, is its own server. 163 WithCallerServer(string) 164 165 // WithCallerService sets caller service, For server this service is upstream service, 166 // but for client, is its own service. 167 WithCallerService(string) 168 169 // WithCallerMethod sets caller method, For server this mothod is upstream mothod, 170 // but for client, is its own method. 171 WithCallerMethod(string) 172 173 // WithCalleeApp sets callee app. For server, this app is its own app, 174 // but for client, is downstream's app. 175 WithCalleeApp(string) 176 177 // WithCalleeServer sets callee server. For server, this server is its own server, 178 // but for client, is downstream's server. 179 WithCalleeServer(string) 180 181 // WithCalleeService sets callee service. For server, this service is its own service, 182 // but for client, is downstream's service. 183 WithCalleeService(string) 184 185 // WithCalleeMethod sets callee method. For server, this method is its own method, 186 // but for client, is downstream's method. 187 WithCalleeMethod(string) 188 189 // CallerServiceName returns caller service name, such as trpc.app.server.service. 190 // For server, this name is upstream's service name, but for client, is its own service name. 191 CallerServiceName() string 192 193 // CallerApp returns caller app. For server, this app is upstream's app, 194 // but for client, is its own app. 195 CallerApp() string 196 197 // CallerServer returns caller server. For server, this is upstream's server, 198 // but for client, is its own server. 199 CallerServer() string 200 201 // CallerService returns caller service. For server, this service is upstream's service, 202 // but for client, is its own service. 203 CallerService() string 204 205 // CallerMethod returns caller method. For server, this method is upstream's method, 206 // but for client, is its own method. 207 CallerMethod() string 208 209 // CalleeServiceName returns callee service name. For server, this name is its own service name, 210 // but for client, is downstream's service name. 211 CalleeServiceName() string 212 213 // CalleeApp returns callee app. For server, this app is its own app, 214 // but for client, is downstream's app. 215 CalleeApp() string 216 217 // CalleeServer returns callee server. For server, this server name is its own name, 218 // but for client, is downstream's server name. 219 CalleeServer() string 220 221 // CalleeService returns callee service. For server, this service is its own service, 222 // but for client, is downstream's service. 223 CalleeService() string 224 225 // CalleeMethod returns callee method. For server, this method is its own method, 226 // but for client, is downstream's method. 227 CalleeMethod() string 228 229 // CalleeContainerName sets callee container name. 230 CalleeContainerName() string 231 232 // WithCalleeContainerName return callee container name. 233 WithCalleeContainerName(string) 234 235 // WithServerMetaData sets server meta data. 236 WithServerMetaData(MetaData) 237 238 // ServerMetaData returns server meta data. 239 ServerMetaData() MetaData 240 241 // WithFrameHead sets frame head. 242 WithFrameHead(interface{}) 243 244 // FrameHead returns frame head. 245 FrameHead() interface{} 246 247 // WithServerReqHead sets server request head. 248 WithServerReqHead(interface{}) 249 250 // ServerReqHead returns server request head. 251 ServerReqHead() interface{} 252 253 // WithServerRspHead sets server response head, this head will return to upstream. 254 WithServerRspHead(interface{}) 255 256 // ServerRspHead returns server response head, this head will return to upstream. 257 ServerRspHead() interface{} 258 259 // WithDyeing sets dyeing mark. 260 WithDyeing(bool) 261 262 // Dyeing returns dyeing mark. 263 Dyeing() bool 264 265 // WithDyeingKey sets dyeing key. 266 WithDyeingKey(string) 267 268 // DyeingKey returns dyeing key. 269 DyeingKey() string 270 271 // WithServerRspErr sets response error for server. 272 WithServerRspErr(error) 273 274 // ServerRspErr returns response error for server. 275 ServerRspErr() *errs.Error 276 277 // WithClientMetaData sets client meta data. 278 WithClientMetaData(MetaData) 279 280 // ClientMetaData returns client meta data. 281 ClientMetaData() MetaData 282 283 // WithClientReqHead sets client request head. 284 WithClientReqHead(interface{}) 285 286 // ClientReqHead returns client request head. 287 ClientReqHead() interface{} 288 289 // WithClientRspErr sets response error for client. 290 WithClientRspErr(error) 291 292 // ClientRspErr returns response error for client. 293 ClientRspErr() error 294 295 // WithClientRspHead sets response head for client. 296 WithClientRspHead(interface{}) 297 298 // ClientRspHead returns response head for client. 299 ClientRspHead() interface{} 300 301 // WithLogger sets logger into context. 302 WithLogger(interface{}) 303 304 // Logger returns logger from context. 305 Logger() interface{} 306 307 // WithRequestID sets request id. 308 WithRequestID(uint32) 309 310 // RequestID returns request id. 311 RequestID() uint32 312 313 // WithStreamID sets stream id. 314 WithStreamID(uint32) 315 316 // StreamID return stream id. 317 StreamID() uint32 318 319 // StreamFrame sets stream frame. 320 StreamFrame() interface{} 321 322 // WithStreamFrame returns stream frame. 323 WithStreamFrame(interface{}) 324 325 // WithCalleeSetName sets callee set name. 326 WithCalleeSetName(string) 327 328 // CalleeSetName returns callee set name. 329 CalleeSetName() string 330 331 // WithCommonMeta sets common meta data. 332 WithCommonMeta(CommonMeta) 333 334 // CommonMeta returns common meta data. 335 CommonMeta() CommonMeta 336 337 // WithCallType sets call type. 338 WithCallType(RequestType) 339 340 // CallType returns call type. 341 CallType() RequestType 342 } 343 344 // CopyMsg copy src Msg to dst. 345 // All fields of src msg will be copied to dst msg. 346 func CopyMsg(dst, src Msg) { 347 if dst == nil || src == nil { 348 return 349 } 350 dst.WithFrameHead(src.FrameHead()) 351 dst.WithRequestTimeout(src.RequestTimeout()) 352 dst.WithSerializationType(src.SerializationType()) 353 dst.WithCompressType(src.CompressType()) 354 dst.WithStreamID(src.StreamID()) 355 dst.WithDyeing(src.Dyeing()) 356 dst.WithDyeingKey(src.DyeingKey()) 357 dst.WithServerRPCName(src.ServerRPCName()) 358 dst.WithClientRPCName(src.ClientRPCName()) 359 dst.WithServerMetaData(src.ServerMetaData().Clone()) 360 dst.WithClientMetaData(src.ClientMetaData().Clone()) 361 dst.WithCallerServiceName(src.CallerServiceName()) 362 dst.WithCalleeServiceName(src.CalleeServiceName()) 363 dst.WithCalleeContainerName(src.CalleeContainerName()) 364 dst.WithServerRspErr(src.ServerRspErr()) 365 dst.WithClientRspErr(src.ClientRspErr()) 366 dst.WithServerReqHead(src.ServerReqHead()) 367 dst.WithServerRspHead(src.ServerRspHead()) 368 dst.WithClientReqHead(src.ClientReqHead()) 369 dst.WithClientRspHead(src.ClientRspHead()) 370 dst.WithLocalAddr(src.LocalAddr()) 371 dst.WithRemoteAddr(src.RemoteAddr()) 372 dst.WithLogger(src.Logger()) 373 dst.WithCallerApp(src.CallerApp()) 374 dst.WithCallerServer(src.CallerServer()) 375 dst.WithCallerService(src.CallerService()) 376 dst.WithCallerMethod(src.CallerMethod()) 377 dst.WithCalleeApp(src.CalleeApp()) 378 dst.WithCalleeServer(src.CalleeServer()) 379 dst.WithCalleeService(src.CalleeService()) 380 dst.WithCalleeMethod(src.CalleeMethod()) 381 dst.WithNamespace(src.Namespace()) 382 dst.WithSetName(src.SetName()) 383 dst.WithEnvName(src.EnvName()) 384 dst.WithEnvTransfer(src.EnvTransfer()) 385 dst.WithRequestID(src.RequestID()) 386 dst.WithStreamFrame(src.StreamFrame()) 387 dst.WithCalleeSetName(src.CalleeSetName()) 388 dst.WithCommonMeta(src.CommonMeta().Clone()) 389 dst.WithCallType(src.CallType()) 390 }