Skip to content

Commit abdf7e9

Browse files
committed
Handle a PublishSuccess message to support batched events
1 parent fd705d6 commit abdf7e9

5 files changed

Lines changed: 60 additions & 2 deletions

File tree

internal/app/protocol/protocol.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@ type DataMessage struct {
3535
SubId string
3636
Payload app.Payload
3737
}
38+
39+
type PublishResultMessage struct {
40+
ID string
41+
Failures []FailedEvent
42+
}
43+
44+
type FailedEvent struct {
45+
Success bool
46+
Index int
47+
RawMessage []byte
48+
}

internal/app/router/router.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func (router *MessageHandler) Handle(ctx context.Context, msg app.Message) error
3434
case protocol.SuccessMessage:
3535
return router.pending.Fulfill(ctx, msg.ID, nil)
3636

37+
case protocol.PublishResultMessage:
38+
// TODO: When publishing a batch of events, need to be able to return an error per failed event or nil
39+
return router.pending.Fulfill(ctx, msg.ID, nil)
40+
3741
case protocol.DataMessage:
3842
return router.receive_use_case.Execute(ctx, msg)
3943
}

internal/infrastructure/codec/codec.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ func (codec Codec) Decode(payload app.Payload) (app.Message, error) {
4040
case protocol.TypeKeepAlive:
4141
return protocol.KeepAliveMessage{}, nil
4242

43-
case protocol.TypePublishSuccess, protocol.TypeSubscribeSuccess, protocol.TypeUnsubscribeSuccess:
43+
case protocol.TypePublishSuccess:
44+
event := events.PublishResult{}
45+
err := json.Unmarshal(payload, &event)
46+
return event.ToProtocol(), err
47+
case /*protocol.TypePublishSuccess,*/ protocol.TypeSubscribeSuccess, protocol.TypeUnsubscribeSuccess:
4448
event := events.SuccessEvent{}
4549
err := json.Unmarshal(payload, &event)
4650
return event.ToProtocol(), err

internal/infrastructure/codec/codec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestDecode(t *testing.T) {
4040
{
4141
name: "publish_success message",
4242
payload: []byte(`{"type":"publish_success","id":"sub-1"}`),
43-
expect_msg: protocol.SuccessMessage{ID: "sub-1"},
43+
expect_msg: protocol.PublishResultMessage{ID: "sub-1", Failures: []protocol.FailedEvent{}},
4444
},
4545
{
4646
name: "unsubscribe_success message",

internal/infrastructure/events/events.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,42 @@ func (event DataEvent) ToProtocol() protocol.DataMessage {
6666
Payload: app.Payload(event.Data),
6767
}
6868
}
69+
70+
type PublishResult struct {
71+
ID string `json:"id"`
72+
Type string `json:"type"`
73+
Successful []Success `json:"successful"`
74+
Failed []Failure `json:"failed"`
75+
}
76+
77+
type Success struct {
78+
ID string `json:"identifier"`
79+
Index int `json:"index"`
80+
}
81+
82+
// Best guess, could not find a schema for a failed event item.
83+
// Raw is used to store the object in its entirety to be able to see the original object
84+
type Failure struct {
85+
ID string `json:"identifier"`
86+
Index int `json:"index"`
87+
Raw []byte
88+
}
89+
90+
func (event PublishResult) ToProtocol() protocol.PublishResultMessage {
91+
result := make([]protocol.FailedEvent, len(event.Failed))
92+
index := 0
93+
94+
for _, item := range event.Failed {
95+
result[index] = protocol.FailedEvent{
96+
Success: false,
97+
Index: item.Index,
98+
RawMessage: item.Raw,
99+
}
100+
index += 1
101+
}
102+
103+
return protocol.PublishResultMessage{
104+
ID: event.ID,
105+
Failures: result,
106+
}
107+
}

0 commit comments

Comments
 (0)