Skip to content

Commit

Permalink
Merge pull request #1811 from justxuewei/fix/triple-attachment
Browse files Browse the repository at this point in the history
fix(adasvc): fix getting attachment issues for triple
  • Loading branch information
justxuewei authored Apr 6, 2022
2 parents b84027f + 8ead2da commit c7fb54a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
13 changes: 10 additions & 3 deletions cluster/cluster/adaptivesvc/cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion filter/adaptivesvc/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 7 additions & 11 deletions protocol/invocation/rpcinvocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,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
}
Expand Down

0 comments on commit c7fb54a

Please sign in to comment.