Skip to content

Commit 8f218ec

Browse files
Guy BaronGuy Baron
Guy Baron
authored and
Guy Baron
committed
fix wrongly set correlation ids when sending a command (#220)
* 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
1 parent 6bf5d1f commit 8f218ec

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

gbus/abstractions.go

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const (
1818
CMD Semantics = "cmd"
1919
//EVT represenst a messge with event semantics in grabbit
2020
EVT Semantics = "evt"
21+
//REPLY represenst a messge with reply semantics in grabbit
22+
REPLY Semantics = "reply"
2123
)
2224

2325
//Bus interface provides the majority of functionality to Send, Reply and Publish messages to the Bus

gbus/saga/invocation.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,21 @@ type sagaInvocation struct {
3434
startedByRPCID string
3535
}
3636

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

39-
message.CorrelationID = si.inboundMsg.ID
4039
message.SagaID = si.sagaID
4140

42-
if !isEvent {
43-
//support saga-to-saga communication
44-
if si.inboundMsg.SagaID != "" {
45-
message.SagaCorrelationID = si.inboundMsg.SagaID
46-
}
41+
if semantics == gbus.REPLY {
42+
message.CorrelationID = si.inboundMsg.ID
43+
message.SagaCorrelationID = si.inboundMsg.SagaID
44+
45+
} else if semantics == gbus.CMD {
4746
//if the saga is potentially invoking itself then set the SagaCorrelationID to reflect that
4847
//https://github.com/wework/grabbit/issues/64
4948
if targetService == si.hostingSvc {
5049
message.SagaCorrelationID = message.SagaID
5150
}
52-
5351
}
54-
5552
}
5653
func (si *sagaInvocation) HostingSvc() string {
5754
return si.hostingSvc
@@ -63,13 +60,13 @@ func (si *sagaInvocation) InvokingSvc() string {
6360

6461
func (si *sagaInvocation) Reply(ctx context.Context, message *gbus.BusMessage) error {
6562
_, targetService := si.decoratedInvocation.Routing()
66-
si.setCorrelationIDs(message, false, targetService)
63+
si.setCorrelationIDs(message, targetService, gbus.REPLY)
6764
return si.decoratedInvocation.Reply(ctx, message)
6865
}
6966

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

72-
si.setCorrelationIDs(message, false, si.startedBy)
69+
si.setCorrelationIDs(message, si.startedBy, gbus.REPLY)
7370

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

9390
func (si *sagaInvocation) Send(ctx context.Context, toService string,
9491
command *gbus.BusMessage, policies ...gbus.MessagePolicy) error {
95-
si.setCorrelationIDs(command, false, toService)
92+
si.setCorrelationIDs(command, toService, gbus.CMD)
9693
return si.decoratedBus.Send(ctx, toService, command, policies...)
9794
}
9895

9996
func (si *sagaInvocation) Publish(ctx context.Context, exchange, topic string,
10097
event *gbus.BusMessage, policies ...gbus.MessagePolicy) error {
101-
si.setCorrelationIDs(event, true, "")
98+
si.setCorrelationIDs(event, "", gbus.EVT)
10299
return si.decoratedBus.Publish(ctx, exchange, topic, event, policies...)
103100
}
104101

0 commit comments

Comments
 (0)