Golang gRPC context to check if cancelled or if deadline is exceeded while running function code












0














I am writing a gRPC golang application and am looking for the best way to check if the deadline is exceeded or if the client cancelled the call.



On the server side I created a channel called done. When done is called by either the go-routines the server returns with either the error or the success.



On the code below I have two go-routines. The first one runs the actual program and if successful calls the done channel without and error, the second waits for the context.Done() and if called calls the done channel with the error.



I am wanting to know if there is a more efficient way to do this without two go-routines.



I don't want to check periodically ctx.Done() or ctx.Cancelled because I feel that would be a waste of time but the method below would use more cpu resources.



Any tips/advise would be great.



package main

import (
"context"
"log"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

pb "mygit/controller/pb"
)

// ping controller
func (s *server) PingController(ctx context.Context, in *pb.PingControllerRequest) (*pb.PingControllerResponse, error) {

done := make(chan error) // make a done bool
var retOut pb.PingControllerResponse

// run function in go routine and call done when finished without being cancelled
go func(i *pb.PingControllerRequest) {
log.Println(in.Client)
// other business logic would go here
retOut = pb.PingControllerResponse{Pong: "PONG"}
done <- nil
}(in)

// if context is done (due to error) return with done
go func(c context.Context) {
<-c.Done()
done <- status.Error(codes.Canceled, c.Err().Error())
}(ctx)

// wait for done channel to be called
var doneErr = <-done

// if there was a context error return with error
if doneErr != nil {
return nil, doneErr
}

// no context error return with response
return &retOut, nil
}









share|improve this question









New contributor




Varcorb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Welcome to Code Review. The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    – Zeta
    11 hours ago
















0














I am writing a gRPC golang application and am looking for the best way to check if the deadline is exceeded or if the client cancelled the call.



On the server side I created a channel called done. When done is called by either the go-routines the server returns with either the error or the success.



On the code below I have two go-routines. The first one runs the actual program and if successful calls the done channel without and error, the second waits for the context.Done() and if called calls the done channel with the error.



I am wanting to know if there is a more efficient way to do this without two go-routines.



I don't want to check periodically ctx.Done() or ctx.Cancelled because I feel that would be a waste of time but the method below would use more cpu resources.



Any tips/advise would be great.



package main

import (
"context"
"log"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

pb "mygit/controller/pb"
)

// ping controller
func (s *server) PingController(ctx context.Context, in *pb.PingControllerRequest) (*pb.PingControllerResponse, error) {

done := make(chan error) // make a done bool
var retOut pb.PingControllerResponse

// run function in go routine and call done when finished without being cancelled
go func(i *pb.PingControllerRequest) {
log.Println(in.Client)
// other business logic would go here
retOut = pb.PingControllerResponse{Pong: "PONG"}
done <- nil
}(in)

// if context is done (due to error) return with done
go func(c context.Context) {
<-c.Done()
done <- status.Error(codes.Canceled, c.Err().Error())
}(ctx)

// wait for done channel to be called
var doneErr = <-done

// if there was a context error return with error
if doneErr != nil {
return nil, doneErr
}

// no context error return with response
return &retOut, nil
}









share|improve this question









New contributor




Varcorb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Welcome to Code Review. The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    – Zeta
    11 hours ago














0












0








0







I am writing a gRPC golang application and am looking for the best way to check if the deadline is exceeded or if the client cancelled the call.



On the server side I created a channel called done. When done is called by either the go-routines the server returns with either the error or the success.



On the code below I have two go-routines. The first one runs the actual program and if successful calls the done channel without and error, the second waits for the context.Done() and if called calls the done channel with the error.



I am wanting to know if there is a more efficient way to do this without two go-routines.



I don't want to check periodically ctx.Done() or ctx.Cancelled because I feel that would be a waste of time but the method below would use more cpu resources.



Any tips/advise would be great.



package main

import (
"context"
"log"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

pb "mygit/controller/pb"
)

// ping controller
func (s *server) PingController(ctx context.Context, in *pb.PingControllerRequest) (*pb.PingControllerResponse, error) {

done := make(chan error) // make a done bool
var retOut pb.PingControllerResponse

// run function in go routine and call done when finished without being cancelled
go func(i *pb.PingControllerRequest) {
log.Println(in.Client)
// other business logic would go here
retOut = pb.PingControllerResponse{Pong: "PONG"}
done <- nil
}(in)

// if context is done (due to error) return with done
go func(c context.Context) {
<-c.Done()
done <- status.Error(codes.Canceled, c.Err().Error())
}(ctx)

// wait for done channel to be called
var doneErr = <-done

// if there was a context error return with error
if doneErr != nil {
return nil, doneErr
}

// no context error return with response
return &retOut, nil
}









share|improve this question









New contributor




Varcorb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am writing a gRPC golang application and am looking for the best way to check if the deadline is exceeded or if the client cancelled the call.



On the server side I created a channel called done. When done is called by either the go-routines the server returns with either the error or the success.



On the code below I have two go-routines. The first one runs the actual program and if successful calls the done channel without and error, the second waits for the context.Done() and if called calls the done channel with the error.



I am wanting to know if there is a more efficient way to do this without two go-routines.



I don't want to check periodically ctx.Done() or ctx.Cancelled because I feel that would be a waste of time but the method below would use more cpu resources.



Any tips/advise would be great.



package main

import (
"context"
"log"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

pb "mygit/controller/pb"
)

// ping controller
func (s *server) PingController(ctx context.Context, in *pb.PingControllerRequest) (*pb.PingControllerResponse, error) {

done := make(chan error) // make a done bool
var retOut pb.PingControllerResponse

// run function in go routine and call done when finished without being cancelled
go func(i *pb.PingControllerRequest) {
log.Println(in.Client)
// other business logic would go here
retOut = pb.PingControllerResponse{Pong: "PONG"}
done <- nil
}(in)

// if context is done (due to error) return with done
go func(c context.Context) {
<-c.Done()
done <- status.Error(codes.Canceled, c.Err().Error())
}(ctx)

// wait for done channel to be called
var doneErr = <-done

// if there was a context error return with error
if doneErr != nil {
return nil, doneErr
}

// no context error return with response
return &retOut, nil
}






performance go concurrency






share|improve this question









New contributor




Varcorb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Varcorb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago





















New contributor




Varcorb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 12 hours ago









Varcorb

1012




1012




New contributor




Varcorb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Varcorb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Varcorb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    Welcome to Code Review. The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    – Zeta
    11 hours ago














  • 1




    Welcome to Code Review. The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    – Zeta
    11 hours ago








1




1




Welcome to Code Review. The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
– Zeta
11 hours ago




Welcome to Code Review. The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
– Zeta
11 hours ago















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Varcorb is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210542%2fgolang-grpc-context-to-check-if-cancelled-or-if-deadline-is-exceeded-while-runni%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








Varcorb is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Varcorb is a new contributor. Be nice, and check out our Code of Conduct.













Varcorb is a new contributor. Be nice, and check out our Code of Conduct.












Varcorb is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210542%2fgolang-grpc-context-to-check-if-cancelled-or-if-deadline-is-exceeded-while-runni%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre