Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions protocol/triple/triple_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"sync"
)

Expand Down Expand Up @@ -161,20 +163,28 @@ func mergeAttachmentToOutgoing(ctx context.Context, inv base.Invocation) (contex
if timeout, ok := inv.GetAttachment(constant.TimeoutKey); ok {
ctx = context.WithValue(ctx, tri.TimeoutKey{}, timeout)
}
header := cloneOutgoingHeader(tri.ExtractFromOutgoingContext(ctx))
for key, valRaw := range inv.Attachments() {
lowerKey := strings.ToLower(key)
if str, ok := valRaw.(string); ok {
ctx = tri.AppendToOutgoingContext(ctx, key, str)
header[lowerKey] = []string{str}
continue
}
if strs, ok := valRaw.([]string); ok {
for _, str := range strs {
ctx = tri.AppendToOutgoingContext(ctx, key, str)
}
header[lowerKey] = append([]string(nil), strs...)
continue
}
return ctx, fmt.Errorf("triple attachments value with key = %s is invalid, which should be string or []string", key)
}
return ctx, nil
return tri.NewOutgoingContext(ctx, header), nil
}

func cloneOutgoingHeader(header http.Header) http.Header {
cloned := make(http.Header, len(header))
for key, vals := range header {
cloned[strings.ToLower(key)] = append([]string(nil), vals...)
}
return cloned
}

// parseInvocation retrieves information from invocation.
Expand Down Expand Up @@ -202,18 +212,8 @@ func parseInvocation(ctx context.Context, url *common.URL, invocation base.Invoc
return callType, inRaw, method, nil
}

// parseAttachments retrieves attachments from users passed-in and URL, then injects them into ctx
// parseAttachments injects pre-defined URL attachments into invocation.
func parseAttachments(ctx context.Context, url *common.URL, invocation base.Invocation) {
// retrieve users passed-in attachment
attaRaw := ctx.Value(constant.AttachmentKey)
if attaRaw != nil {
if userAtta, ok := attaRaw.(map[string]any); ok {
for key, val := range userAtta {
invocation.SetAttachment(key, val)
}
}
}
Comment on lines -207 to -215
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为啥删了

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attachment 的注入入口已经前移到了 client 层。现在用户通过 context 传入的 attachment,会在 client.generateInvocation 阶段统一合并进 invocation.Attachments(),Triple invoker 发送请求时只需要从 invocation 读取并写入 outgoing header,不需要再在 parseAttachments 里重复从 context 回灌一次。

// set pre-defined attachments
for _, key := range triAttachmentKeys {
if val := url.GetParam(key, ""); len(val) > 0 {
invocation.SetAttachment(key, val)
Expand Down
Loading
Loading