Skip to content

Commit

Permalink
fix wrongly set correlation ids when sending a command (#220)
Browse files Browse the repository at this point in the history
* adding the SagaCorrelationID header only for replies

Fixing a bug where grabbit would set the message.CorrelationID and the message.SagaCorrelationID header even
if the message was a command and not a reply.

* fixing minor documentation issue
  • Loading branch information
Guy Baron authored Oct 22, 2019
1 parent ce8face commit 211f219
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
2 changes: 2 additions & 0 deletions gbus/abstractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
CMD Semantics = "cmd"
//EVT represenst a messge with event semantics in grabbit
EVT Semantics = "evt"
//REPLY represenst a messge with reply semantics in grabbit
REPLY Semantics = "reply"
)

//Bus interface provides the majority of functionality to Send, Reply and Publish messages to the Bus
Expand Down
24 changes: 10 additions & 14 deletions gbus/saga/invocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,21 @@ type sagaInvocation struct {
startedByRPCID string
}

func (si *sagaInvocation) setCorrelationIDs(message *gbus.BusMessage, isEvent bool, targetService string) {
func (si *sagaInvocation) setCorrelationIDs(message *gbus.BusMessage, targetService string, semantics gbus.Semantics) {

message.CorrelationID = si.inboundMsg.ID
message.SagaID = si.sagaID

if !isEvent {
//support saga-to-saga communication
if si.inboundMsg.SagaID != "" {
message.SagaCorrelationID = si.inboundMsg.SagaID
}
if semantics == gbus.REPLY {
message.CorrelationID = si.inboundMsg.ID
message.SagaCorrelationID = si.inboundMsg.SagaID

} else if semantics == gbus.CMD {
//if the saga is potentially invoking itself then set the SagaCorrelationID to reflect that
//https://github.com/wework/grabbit/issues/64

if targetService == si.hostingSvc {
message.SagaCorrelationID = message.SagaID
}

}

}
func (si *sagaInvocation) HostingSvc() string {
return si.hostingSvc
Expand All @@ -64,13 +60,13 @@ func (si *sagaInvocation) InvokingSvc() string {

func (si *sagaInvocation) Reply(ctx context.Context, message *gbus.BusMessage) error {
_, targetService := si.decoratedInvocation.Routing()
si.setCorrelationIDs(message, false, targetService)
si.setCorrelationIDs(message, targetService, gbus.REPLY)
return si.decoratedInvocation.Reply(ctx, message)
}

func (si *sagaInvocation) ReplyToInitiator(ctx context.Context, message *gbus.BusMessage) error {

si.setCorrelationIDs(message, false, si.startedBy)
si.setCorrelationIDs(message, si.startedBy, gbus.REPLY)

//override the correlation ids to those of the message creating the saga
message.SagaCorrelationID = si.startedBySaga
Expand All @@ -93,13 +89,13 @@ func (si *sagaInvocation) Ctx() context.Context {

func (si *sagaInvocation) Send(ctx context.Context, toService string,
command *gbus.BusMessage, policies ...gbus.MessagePolicy) error {
si.setCorrelationIDs(command, false, toService)
si.setCorrelationIDs(command, toService, gbus.CMD)
return si.decoratedBus.Send(ctx, toService, command, policies...)
}

func (si *sagaInvocation) Publish(ctx context.Context, exchange, topic string,
event *gbus.BusMessage, policies ...gbus.MessagePolicy) error {
si.setCorrelationIDs(event, true, "")
si.setCorrelationIDs(event, "", gbus.EVT)
return si.decoratedBus.Publish(ctx, exchange, topic, event, policies...)
}

Expand Down

0 comments on commit 211f219

Please sign in to comment.