Answer the question
In order to leave comments, you need to log in
Grpc procedures to call synchronously or asynchronously?
For academic purposes, I wrote down a simple api with registration, authentication and several CRUDs.
Immediately after registration (adding a user to the database), you need to send an email with a confirmation code.
I decided to put it into a microservice generated through gRPC and protofiles.
The procedure in the microservice receives the email text and the address to whom to send as input. Generates an email and sends it to the Mailgun service.
In the application code, immediately after registration, this method is called:
func sendCode(code int, email string) error {
conn, err := grpc.Dial(os.Getenv("MAILER_SERVICE_PORT"), grpc.WithInsecure())
if err != nil {
return err
}
c := pb.NewMailerClient(conn)
_, err = c.SendEmail(context.Background(), &pb.EmailRequest{Email: email, Code: strconv.Itoa(code)})
if err != nil {
return err
}
return nil
}
Answer the question
In order to leave comments, you need to log in
For gRPC, there is no good practice in this regard, it's just a remote action call.
If you expect everything to be done quickly, do it synchronously.
If you expect it *may* take a long time, do it asynchronously.
Email mailings are indeed often done through queues, but in your case, nothing prevents you from simply raising a goroutine (or several) at the start of the application, which will read emails from the channel and send them asynchronously, and then write to the channel from where you need to.
Yes, and by themselves, such actions as mailings and so on are usually performed asynchronously, so there is no need to wait for them to return a response to the request.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question