Skip to content

Commit

Permalink
example cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
roblaszczak committed Jan 8, 2025
1 parent 6b0af5b commit ffc518c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion _examples/basic/5-cqrs-protobuf/go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module main.go

require (
github.com/ThreeDotsLabs/watermill v1.4.2-0.20241216112750-9d5e2da13339
github.com/ThreeDotsLabs/watermill v1.4.2
github.com/ThreeDotsLabs/watermill-amqp/v3 v3.0.0
github.com/golang/protobuf v1.5.4
)
Expand Down
2 changes: 2 additions & 0 deletions _examples/basic/5-cqrs-protobuf/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/ThreeDotsLabs/watermill v1.4.1 h1:gjP6yZH+otMPjV0KsV07pl9TeMm9UQV/gqi
github.com/ThreeDotsLabs/watermill v1.4.1/go.mod h1:lBnrLbxOjeMRgcJbv+UiZr8Ylz8RkJ4m6i/VN/Nk+to=
github.com/ThreeDotsLabs/watermill v1.4.2-0.20241216112750-9d5e2da13339 h1:Q6joJUSSelwcxHEngdu3PGy4UYqk7BMXKC3Rzer3Xuk=
github.com/ThreeDotsLabs/watermill v1.4.2-0.20241216112750-9d5e2da13339/go.mod h1:lBnrLbxOjeMRgcJbv+UiZr8Ylz8RkJ4m6i/VN/Nk+to=
github.com/ThreeDotsLabs/watermill v1.4.2 h1:lX/J79HyUipxZ2VetC7vMPqlw29xreHMxzhPlcZnYoQ=
github.com/ThreeDotsLabs/watermill v1.4.2/go.mod h1:lBnrLbxOjeMRgcJbv+UiZr8Ylz8RkJ4m6i/VN/Nk+to=
github.com/ThreeDotsLabs/watermill-amqp/v3 v3.0.0 h1:r5idq2qkd3M345iv3C3zAX+lFlEu7iW8QESNnuuv4eY=
github.com/ThreeDotsLabs/watermill-amqp/v3 v3.0.0/go.mod h1:+8tCh6VCuBcQWhfETCwzRINKQ1uyeg9moH3h7jMKxQk=
github.com/cenkalti/backoff/v3 v3.2.2 h1:cfUAAO3yvKMYKPrvhDuHSwQnhZNk/RMHKdZqKTxfm6M=
Expand Down
25 changes: 11 additions & 14 deletions _examples/basic/5-cqrs-protobuf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ type OrderBeerHandler struct {
eventBus *cqrs.EventBus
}

func (o OrderBeerHandler) HandlerName() string {
return "OrderBeerHandler"
}

func (o OrderBeerHandler) Handle(ctx context.Context, cmd *OrderBeer) error {
if rand.Int63n(10) == 0 {
// sometimes there is no beer left, command will be retried
Expand Down Expand Up @@ -136,13 +132,22 @@ func main() {
cqrsMarshaler := cqrs.ProtobufMarshaler{
// It will generate topic names based on the event/command type.
// So for example, for "RoomBooked" name will be "RoomBooked".
// It's later used to generate topic names.
//
// This value is used to generate topic names with "generateEventsTopic" and "generateCommandsTopic" functions.
GenerateName: cqrs.StructName,
}

generateEventsTopic := func(eventName string) string {
return "events." + eventName
}

generateCommandsTopic := func(commandName string) string {
return "commands." + commandName
}

// You can use any Pub/Sub implementation from here: https://watermill.io/pubsubs/
// Detailed RabbitMQ implementation: https://watermill.io/pubsubs/amqp/
// Commands will be send to queue, because they need to be consumed once.
// Commands will be sent to queue, because they need to be consumed once.
commandsAMQPConfig := amqp.NewDurableQueueConfig(amqpAddress)
commandsPublisher, err := amqp.NewPublisher(commandsAMQPConfig, logger)
if err != nil {
Expand Down Expand Up @@ -344,11 +349,3 @@ func publishCommands(commandBus *cqrs.CommandBus) func() {
time.Sleep(time.Second)
}
}

func generateEventsTopic(eventName string) string {
return "events." + eventName
}

func generateCommandsTopic(commandName string) string {
return "commands." + commandName
}
3 changes: 2 additions & 1 deletion _examples/basic/6-cqrs-ordered-events/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Example Golang CQRS application - ordered events
# Example Golang CQRS application - ordered events with Kafka

This application is using [Watermill CQRS](http://watermill.io/docs/cqrs) component.

Expand All @@ -20,6 +20,7 @@ The system maintains:
- A current list of all active subscribers
- A timeline of all subscription-related activities

In this example, keeping order of events is crucial.
If events won't be ordered, and `SubscriberSubscribed` would arrive after `SubscriberUnsubscribed` event,
the subscriber will be still subscribed.

Expand Down
2 changes: 1 addition & 1 deletion _examples/basic/6-cqrs-ordered-events/go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module main.go

require (
github.com/ThreeDotsLabs/watermill v1.4.2-0.20241216125745-7ab13543158c
github.com/ThreeDotsLabs/watermill v1.4.2
github.com/ThreeDotsLabs/watermill-kafka/v3 v3.0.5
google.golang.org/protobuf v1.34.2
)
Expand Down
2 changes: 2 additions & 0 deletions _examples/basic/6-cqrs-ordered-events/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/IBM/sarama v1.43.3 h1:Yj6L2IaNvb2mRBop39N7mmJAHBVY3dTPncr3qGVkxPA=
github.com/IBM/sarama v1.43.3/go.mod h1:FVIRaLrhK3Cla/9FfRF5X9Zua2KpS3SYIXxhac1H+FQ=
github.com/ThreeDotsLabs/watermill v1.4.2-0.20241216125745-7ab13543158c h1:YucRnlvVXDydpiKp88znVBjcAYDIpmjKtHxixEAEJ38=
github.com/ThreeDotsLabs/watermill v1.4.2-0.20241216125745-7ab13543158c/go.mod h1:fO+fo0wGp1+dKQGkoPdLGIHXxwmYj+kitAXJDk/6l0Y=
github.com/ThreeDotsLabs/watermill v1.4.2 h1:lX/J79HyUipxZ2VetC7vMPqlw29xreHMxzhPlcZnYoQ=
github.com/ThreeDotsLabs/watermill v1.4.2/go.mod h1:lBnrLbxOjeMRgcJbv+UiZr8Ylz8RkJ4m6i/VN/Nk+to=
github.com/ThreeDotsLabs/watermill-kafka/v3 v3.0.5 h1:ud+4txnRgtr3kZXfXZ5+C7kVQEvsLc5HSNUEa0g+X1Q=
github.com/ThreeDotsLabs/watermill-kafka/v3 v3.0.5/go.mod h1:t4o+4A6GB+XC8WL3DandhzPwd265zQuyWMQC/I+WIOU=
github.com/cenkalti/backoff/v3 v3.2.2 h1:cfUAAO3yvKMYKPrvhDuHSwQnhZNk/RMHKdZqKTxfm6M=
Expand Down
4 changes: 2 additions & 2 deletions docs/content/docs/cqrs.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ In the scenario, when we have multiple event types on one topic, you have two op
- Simple one-to-one matching of events to handlers

2. `EventGroupProcessor`:
- Group of handlers share a single subscriber instance (and one consumer group, if such mechanism is supported)
- Group of handlers share a single subscriber instance (and one consumer group, if such mechanism is supported -- allows to maintain order of events),
- One handler group can support multiple event types,
- When message arrives to the topic, Watermill will match it to the handler in the group based on event type

Expand Down Expand Up @@ -168,7 +168,7 @@ graph TD
Thanks to using one subscriber instance and consumer group, events will be processed in the order they were sent.

{{< callout context="note" title="Note" icon="outline/info-circle" >}}
It's allowed to have multiple handlers for the same event type in one group, but we recommend to not do that.
It's supported to have multiple handlers for the same event type in one group, but we recommend to not do that.

Please keep in mind that those handlers will be processed within the same message.
If first handler succeeds and the second fails, the message will be re-delivered and the first will be re-executed.
Expand Down

0 comments on commit ffc518c

Please sign in to comment.