I’m working on my first project in Go Lang and facing an issue that I still cannot solve. The application is in hexagonal architecture using gRPC. When a specific method is called in gRPC, it works properly on the first usage but no longer works in the next requests. If I make 5 requests, none of them reach the handler, only the first one.
In the gRPC port the code is defined this way – take note that all variables are set and the first call works properly.
// defining the command to send to the command bus
protoMessage := productv1.CreateProductCommand{
Id: productID,
ProductInfoId: in.Product.ProductInfoId,
Product: in.Product,
}
// creating properly the message and validating the possible errors
msg, err := messageManager.CreateMessage(ctx, &protoMessage)
if err != nil {
logger.WithContext(ctx).Error(fmt.Sprintf("CreateProduct: error create message %v", err))
return nil, err
}
// and finally, sending the message for the command bus
err = s.commandBus.Send(ctx, finalMessage)
if err != nil {
logger.WithContext(ctx).Error(fmt.Sprintf("CreateProduct: error send message %v", err))
return nil, err
}
The handler is a command that when the command bus processes it, executes its job. Again: on the first call, it works.
func (h CreateProductHandler) Handle(ctx context.Context, c interface{}) error {
logger.WithContext(ctx).Info(fmt.Sprintf("%s invoked with command = %v", h.HandlerName(), c))
eventId := uuid.NewString()
errs := make([]*statuspb.Status, 0)
if h.eventBus == nil || h.productRepo == nil || h.productInfoRepo == nil {
err := errors.New("CreateProductHandler not properly initialized. Use NewCreateProductHandler")
return err
}
cmd := c.(*productv1.CreateProductCommand)
// after this cmd variable, comes the logic for product register, that is working as expected.
The return clauses are in case of an error where the error itself is returned or nil. Maybe this return nil is a problem???
Even though I said that the handler is reaching only on the first call and it appears to be correct, here is how this command handler is registered.
productv1, err := product.NewApplicationFromSettings(settings)
if err != nil {
return nil, err
}
cqrsFacade, err := cqrs.NewFacade(newFacadeConfig(
settings,
redisstream.JSONMarshaler{},
*publisher,
*subscriber,
watermillLogger,
router,
func(commandBus *cqrs.CommandBus, eventBus *cqrs.EventBus) []cqrs.CommandHandler {
commandHandlers := []cqrs.CommandHandler{}
// previous command handlers...
commandHandlers = append(commandHandlers, productv1.CommandHandlers(commandBus, eventBus)...)
// other command handlers....
return commandHandlers
},
So, here is the problem: all variables as set, all commands are working perfectly, only this productv1 is working at the first call, and not to the following. It seems like something is stuck, I don’t know.
If I re-run the application, all “stuck” commands are performed one after another, until the queue is cleared.
Ps: this issue only occurs by running in the server (even the server and local machine both running docker). In my local machine, this error doesn’t exist.
I have been stuck on this for over 3 days and I have no more ideas.
Called gRPC method several times.