Golang gRPC context to check if cancelled or if deadline is exceeded while running function code
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
New contributor
add a comment |
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
New contributor
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
add a comment |
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
New contributor
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
performance go concurrency
New contributor
New contributor
edited 1 hour ago
New contributor
asked 12 hours ago
Varcorb
1012
1012
New contributor
New contributor
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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