This is an automated email from the ASF dual-hosted git repository.

justxuewei pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 8ead2da10 fix: fix getting attachment issue for triple
     new c7fb54a22 Merge pull request #1811 from 
justxuewei/fix/triple-attachment
8ead2da10 is described below

commit 8ead2da1044a73ab166727354f54e7ff134b4fbb
Author: Xuewei Niu <[email protected]>
AuthorDate: Tue Apr 5 15:01:34 2022 +0800

    fix: fix getting attachment issue for triple
---
 cluster/cluster/adaptivesvc/cluster_invoker.go | 13 ++++++++++---
 filter/adaptivesvc/filter.go                   | 11 ++++++++++-
 protocol/invocation/rpcinvocation.go           | 18 +++++++-----------
 3 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/cluster/cluster/adaptivesvc/cluster_invoker.go 
b/cluster/cluster/adaptivesvc/cluster_invoker.go
index d54cd15ee..64fa272bb 100644
--- a/cluster/cluster/adaptivesvc/cluster_invoker.go
+++ b/cluster/cluster/adaptivesvc/cluster_invoker.go
@@ -76,9 +76,16 @@ func (ivk *adaptiveServiceClusterInvoker) Invoke(ctx 
context.Context, invocation
        }
 
        // update metrics
-       remainingIface := 
result.Attachment(constant.AdaptiveServiceRemainingKey, "")
-       remainingStr, ok := remainingIface.(string)
-       if !ok {
+       var remainingStr string
+       remainingIface := 
result.Attachment(constant.AdaptiveServiceRemainingKey, nil)
+       if remainingIface != nil {
+               if str, strOK := remainingIface.(string); strOK {
+                       remainingStr = str
+               } else if strArr, strArrOK := remainingIface.([]string); 
strArrOK && len(strArr) > 0 {
+                       remainingStr = strArr[0]
+               }
+       }
+       if remainingStr == "" {
                logger.Errorf("[adasvc cluster] The %s field type of value %v 
should be string.",
                        constant.AdaptiveServiceRemainingKey, remainingIface)
                return result
diff --git a/filter/adaptivesvc/filter.go b/filter/adaptivesvc/filter.go
index 9a5e3709f..2af36db11 100644
--- a/filter/adaptivesvc/filter.go
+++ b/filter/adaptivesvc/filter.go
@@ -97,7 +97,16 @@ func (f *adaptiveServiceProviderFilter) Invoke(ctx 
context.Context, invoker prot
 
 func (f *adaptiveServiceProviderFilter) OnResponse(_ context.Context, result 
protocol.Result, invoker protocol.Invoker,
        invocation protocol.Invocation) protocol.Result {
-       if result.Attachment(constant.AdaptiveServiceEnabledKey, "") != 
constant.AdaptiveServiceIsEnabled {
+       var asEnabled string
+       asEnabledIface := result.Attachment(constant.AdaptiveServiceEnabledKey, 
nil)
+       if asEnabledIface != nil {
+               if str, strOK := asEnabledIface.(string); strOK {
+                       asEnabled = str
+               } else if strArr, strArrOK := asEnabledIface.([]string); 
strArrOK && len(strArr) > 0 {
+                       asEnabled = strArr[0]
+               }
+       }
+       if asEnabled != constant.AdaptiveServiceIsEnabled {
                // the adaptive service is enabled on the invocation
                return result
        }
diff --git a/protocol/invocation/rpcinvocation.go 
b/protocol/invocation/rpcinvocation.go
index f5119c7db..d680c6c1d 100644
--- a/protocol/invocation/rpcinvocation.go
+++ b/protocol/invocation/rpcinvocation.go
@@ -186,24 +186,20 @@ func (r *RPCInvocation) GetAttachment(key string) 
(string, bool) {
        if r.attachments == nil {
                return "", false
        }
-       if value, ok := r.attachments[key]; ok {
-               if str, ok := value.(string); ok {
+       if value, existed := r.attachments[key]; existed {
+               if str, strOK := value.(string); strOK {
                        return str, true
+               } else if strArr, strArrOK := value.([]string); strArrOK && 
len(strArr) > 0 {
+                       // For triple protocol, the attachment value is wrapped 
by an array.
+                       return strArr[0], true
                }
        }
        return "", false
 }
 
 func (r *RPCInvocation) GetAttachmentWithDefaultValue(key string, defaultValue 
string) string {
-       r.lock.RLock()
-       defer r.lock.RUnlock()
-       if r.attachments == nil {
-               return defaultValue
-       }
-       if value, ok := r.attachments[key]; ok {
-               if str, ok := value.(string); ok {
-                       return str
-               }
+       if value, ok := r.GetAttachment(key); ok {
+               return value
        }
        return defaultValue
 }

Reply via email to