@@ -55,6 +55,18 @@ func GetMetadataFromMetadataReport(revision string, instance registry.ServiceIns
5555}
5656
5757func GetMetadataFromRpc (revision string , instance registry.ServiceInstance ) (* info.MetadataInfo , error ) {
58+ return GetMetadataFromRpcWithContext (context .Background (), revision , instance )
59+ }
60+
61+ // GetMetadataFromRpcWithContext fetches metadata through the metadata service
62+ // while preserving the caller's context for the underlying RPC invocation.
63+ func GetMetadataFromRpcWithContext (ctx context.Context , revision string , instance registry.ServiceInstance ) (* info.MetadataInfo , error ) {
64+ if ctx == nil {
65+ ctx = context .Background ()
66+ }
67+ if err := ctx .Err (); err != nil {
68+ return nil , err
69+ }
5870 url , err := buildStandardMetadataServiceURL (instance )
5971 if err != nil {
6072 return nil , err
@@ -74,27 +86,28 @@ func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) (*in
7486 defer func () {
7587 invoker .Destroy ()
7688 }()
77- return remoteService .getMetadataInfo (context . Background () , revision )
89+ return remoteService .getMetadataInfo (ctx , revision )
7890}
7991
8092// remoteMetadataService is the internal interface for fetching MetadataInfo via RPC.
81- // The context parameter is accepted for future cancellation support but is not yet propagated.
8293type remoteMetadataService interface {
83- getMetadataInfo (_ context.Context , revision string ) (* info.MetadataInfo , error )
94+ getMetadataInfo (ctx context.Context , revision string ) (* info.MetadataInfo , error )
8495}
8596
8697type triMetadataServiceV2 struct {
8798 invoker base.Invoker
8899}
89100
90101// getMetadataInfo fetches metadata via RPC using the Triple protocol (Protobuf).
91- // TODO(context-propagation): ctx is not yet forwarded to the invoker; cancellation is not respected.
92- func (m * triMetadataServiceV2 ) getMetadataInfo (_ context.Context , revision string ) (* info.MetadataInfo , error ) {
102+ func (m * triMetadataServiceV2 ) getMetadataInfo (ctx context.Context , revision string ) (* info.MetadataInfo , error ) {
93103 const methodName = "GetMetadataInfo"
94104 req := & tripleapi.MetadataRequest {Revision : revision }
95105 metadataInfo := & tripleapi.MetadataInfoV2 {}
96106 inv , _ := generateInvocation (m .invoker .GetURL (), methodName , req , metadataInfo , constant .CallUnary )
97- res := m .invoker .Invoke (context .Background (), inv )
107+ if rpcInv , ok := inv .(* invocation.RPCInvocation ); ok {
108+ rpcInv .SetContext (ctx )
109+ }
110+ res := m .invoker .Invoke (ctx , inv )
98111 if res .Error () != nil {
99112 logger .Errorf ("[Metadata][RPC] could not get the metadata info from remote provider, err=%v" , res .Error ())
100113 return nil , perrors .Wrapf (res .Error (), "remote metadata call failed" )
@@ -160,15 +173,17 @@ type remoteMetadataServiceV1 struct {
160173}
161174
162175// getMetadataInfo fetches metadata via RPC using the dubbo:// protocol (Hessian2 serialization).
163- // TODO(context-propagation): ctx is not yet forwarded to the invoker; cancellation is not respected.
164- func (m * remoteMetadataServiceV1 ) getMetadataInfo (_ context.Context , revision string ) (* info.MetadataInfo , error ) {
176+ func (m * remoteMetadataServiceV1 ) getMetadataInfo (ctx context.Context , revision string ) (* info.MetadataInfo , error ) {
165177 const methodName = "getMetadataInfo"
166178 // Use interface{} as reply parameter to accept any type (MetadataInfo or string)
167179 // This avoids panic when Java returns String instead of MetadataInfo
168180 var rawResult any
169181 inv , _ := generateInvocation (m .invoker .GetURL (), methodName , revision , & rawResult , constant .CallUnary )
182+ if rpcInv , ok := inv .(* invocation.RPCInvocation ); ok {
183+ rpcInv .SetContext (ctx )
184+ }
170185
171- res := m .invoker .Invoke (context . Background () , inv )
186+ res := m .invoker .Invoke (ctx , inv )
172187 if res .Error () != nil {
173188 logger .Errorf ("[Metadata][RPC] RPC call failed to %s, err=%v" , m .invoker .GetURL ().Location , res .Error ())
174189 return nil , perrors .Wrapf (res .Error (), "RPC call failed to %s" , m .invoker .GetURL ().Location )
0 commit comments