Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Server return with Attachment #2648

Merged
merged 11 commits into from
Apr 26, 2024
Merged

fix: Server return with Attachment #2648

merged 11 commits into from
Apr 26, 2024

Conversation

YarBor
Copy link
Contributor

@YarBor YarBor commented Mar 31, 2024

fix: Server return with Attachment (#2641)

in this pr, every call will return all attachment-data , whether built-in data or user-attached data. in triple unray call

but in this issue
i have no idea to fix it in an elegant way

I tried adding new fields like (userAttachmentKey or whatever) but it's not very nice to separate the built-in data from the user-attached data. I also thought about modifying the key of user-attached data before transfer and restoring it after transfer, but that doesn't seem feasible in this question (called in curl mode).

there may need some good idea?

but in this issue
i have no idea to fix it in an elegant way

I tried adding new fields like (userAttachmentKey or whatever) but it's not very nice to separate the built-in data from the user-attached data. I also thought about modifying the key of user-attached data before transfer and restoring it after transfer, but that doesn't seem feasible in this question (called in curl mode).

there may need some good idea?

add new api

One possible point to discuss is that in the additional data received by both parties, the first letter of the key will be changed to a capital letter.

client-side-call

func rpcCaller(){
        header := http.Header{"testKey1": []string{"testVal1"}, "testKey2": []string{"testVal2"}}
        // to store outgoing data ,and reserve the location for the receive field.
        // header will be copy , and header's key will change to be lowwer. 
	ctx, _ := triple_protocol.NewOutgoingContext(context.Background(), header )

        res, err := cli.Greet(ctx, &greet.GreetRequest{Name: "triple"})
	// stream, err := cli.GreetServerStream(ctx, &greet.GreetServerStreamRequest{Name: "triple"})
        // stream, err := cli.GreetStream(ctx)
        // stream, err := cli.GreetClientStream(ctx)

        /* 
             do something and close connection.
        */ 

        // to get return data , and key is lowwer , like grpc.
      	data, err := triple_protocol.FromIncomingContext(ctx)
}

server-side

func (impl *Impl) ****call(ctx context.Context, *** api.Req) (*** , errno){
	data, _ := triple.FromIncomingContext(ctx)

        // do something

        triple.SetOutgoingData(ctx , http.Header{"OutgoingDataKey1" , []string{"OutgoingDataVal1"}})
	/* OR */ 
        triple.AppendToOutgoingContext(ctx, "OutgoingContextKey2", "OutgoingDataVal2")
}

new api applies to all 4 calling methods.

Signed-off-by: YarBor <yarbor.ww@gmail.com>
YarBor added 4 commits April 10, 2024 14:08
Signed-off-by: YarBor <yarbor.ww@gmail.com>
Signed-off-by: YarBor <yarbor.ww@gmail.com>
rewrite logic,Put the api into protocol/triple/triple_protocol layer
used triple/triple_protocol/header.go's logic

Signed-off-by: YarBor <yarbor.ww@gmail.com>
Signed-off-by: YarBor <yarbor.ww@gmail.com>
@YarBor YarBor changed the title fix: Server return with Attachment (#2641) fix: Server return with Attachment Apr 12, 2024
}

// NewOutgoingContext sets headers entirely. If there are existing headers, they would be replaced.
// It is used for passing headers to server-side.
// It is like grpc.NewOutgoingContext.
// Please refer to https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md#sending-metadata.
func NewOutgoingContext(ctx context.Context, header http.Header) context.Context {
return context.WithValue(ctx, headerOutgoingKey{}, header)
func NewOutgoingContext(ctx context.Context, data interface{}) (context.Context, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to modify the args of this function?

  • I think http.Header is clear and efficient enough for users to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change it back.

}

// AppendToOutgoingContext merges kv pairs from user and existing headers.
// It is used for passing headers to server-side.
// It is like grpc.AppendToOutgoingContext.
// Please refer to https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md#sending-metadata.
func AppendToOutgoingContext(ctx context.Context, kv ...string) context.Context {
func AppendToOutgoingContext(ctx context.Context, kv ...string) (context.Context, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the reason of returning error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this needs to be discussed. Should there be constraints on the attachment key? When transmitting and returning within the framework, the first letter of the attachment key will be changed to uppercase. On the receiving side, we cannot restore the original key.

func ExtractFromOutgoingContext(ctx context.Context) http.Header {
headerRaw := ctx.Value(headerOutgoingKey{})
if headerRaw == nil {
func ExtractFromOutgoingContext(ctx context.Context) map[string][]string {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i will change it back.

// Please refer to https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md#receiving-metadata-1.
func FromIncomingContext(ctx context.Context) (http.Header, bool) {
header, ok := ctx.Value(headerIncomingKey{}).(http.Header)
func FromIncomingContext(ctx context.Context) (map[string][]string, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i will change it back.

@@ -181,3 +231,11 @@ func SendHeader(ctx context.Context, header http.Header) error {
mergeHeaders(conn.RequestHeader(), header)
return conn.Send(nil)
}

func outGoingKeyCheck(key string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is this function used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think it should be check.

client/client.go Outdated
@@ -59,7 +59,8 @@ func (conn *Connection) call(ctx context.Context, reqs []interface{}, resp inter
if err != nil {
return nil, err
}
return conn.refOpts.invoker.Invoke(ctx, inv), nil
res := conn.refOpts.invoker.Invoke(ctx, inv)
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems there is no need to change statement style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the change here is a mistake. This was a change while testing, I will change it back to how it was.

@@ -136,36 +143,50 @@ func (ti *TripleInvoker) Invoke(ctx context.Context, invocation protocol.Invocat
return &result
}

func mergeAttachmentToOutgoing(ctx context.Context, attachments map[string]interface{}) (context.Context, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you extract attachments merge logic from parseInvocation. Why do not extract entire logics about processing attachments?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think u are right.

YarBor added 2 commits April 15, 2024 09:33
Signed-off-by: YarBor <yarbor.ww@gmail.com>
Signed-off-by: YarBor <yarbor.ww@gmail.com>
@codecov-commenter
Copy link

codecov-commenter commented Apr 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 47.31%. Comparing base (1bd6abc) to head (1bd6abc).

❗ Current head 1bd6abc differs from pull request most recent head 57227f3. Consider uploading reports for the commit 57227f3 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2648   +/-   ##
=======================================
  Coverage   47.31%   47.31%           
=======================================
  Files         341      341           
  Lines       25165    25165           
=======================================
  Hits        11908    11908           
  Misses      12106    12106           
  Partials     1151     1151           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

YarBor added 2 commits April 17, 2024 19:22
…all 4 calling methods.

Signed-off-by: YarBor <yarbor.ww@gmail.com>
Signed-off-by: YarBor <yarbor.ww@gmail.com>
Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@YarBor YarBor requested a review from DMwangnima April 24, 2024 00:57
Copy link
Contributor

@chickenlj chickenlj left a comment

Choose a reason for hiding this comment

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

LGTM.

@chickenlj chickenlj merged commit 72f80ae into apache:main Apr 26, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants