forked from jen20/go-event-sourcing-sample
-
Notifications
You must be signed in to change notification settings - Fork 24
feat: realtime async events function for one aggregate types #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
hallgren
wants to merge
21
commits into
master
Choose a base branch
from
async_save_hook_one_aggregate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b5aff6e
feat: realtime events / aggregate typ
hallgren bc8bf35
clear events after the save hook is ran
hallgren d00a5bd
test the post save hook on tictactoe example
hallgren 0c6bef0
tidy
hallgren d9263d1
remove binary file
hallgren b3a80e6
realtime tictactoe example
hallgren e310191
fix typo
hallgren 8bd92b4
tidy and docs
hallgren f75206c
SetSaveHook returns error
hallgren 5baeba7
change docs for ErrAggregateNotRegistered error
hallgren 80c64ab
more info in error returned
hallgren ac99ab2
multiple save hook functions per aggregate
hallgren 72902d7
ResetRegister
hallgren 3c1f2ca
docs
hallgren 274eb9d
update README
hallgren 0edb503
update README example
hallgren 02c0ec2
realtime events example
hallgren 264023f
feat (realtime): SaveHook takes one aggregate
hallgren c6f3f05
feat(realtime): async SaveHook takes one aggregate
hallgren 54eed79
test: synctest
hallgren 2f5c607
build: go 1.25
hallgren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| module github.com/hallgren/eventsourcing/example | ||
|
|
||
| go 1.22 | ||
| go 1.25 | ||
|
|
||
| require github.com/hallgren/eventsourcing v0.8.1 | ||
|
|
||
| require github.com/hallgren/eventsourcing/core v0.4.0 // indirect | ||
|
|
||
| // replace github.com/hallgren/eventsourcing => ../. | ||
| replace github.com/hallgren/eventsourcing => ../. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,2 @@ | ||
| github.com/hallgren/eventsourcing v0.8.0 h1:lFMWRdc59+tRwzCzmby8I3qr2WbD1GWqB/ypJrBMbVc= | ||
| github.com/hallgren/eventsourcing v0.8.0/go.mod h1:62QwV0m8wLQM6SZwtuGn4lwA5UUFozFnALyZvFeX9ps= | ||
| github.com/hallgren/eventsourcing v0.8.1 h1:SHefx3Wf2K1ylmGBzykSmt4dRVnDTQGYjODiM69c4uU= | ||
| github.com/hallgren/eventsourcing v0.8.1/go.mod h1:62QwV0m8wLQM6SZwtuGn4lwA5UUFozFnALyZvFeX9ps= | ||
| github.com/hallgren/eventsourcing/core v0.4.0 h1:a11TT3df7JlrZtIogqbGmLGgmeugRavwD8HrLtW1Uxw= | ||
| github.com/hallgren/eventsourcing/core v0.4.0/go.mod h1:rgo2kFwNVCb0bzUub5nOPlUYNlFkp1uUQBEQx5fM3Lk= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // This realtime events example demonstrates capturing and analyzing game events such as player moves and results | ||
| // by using save hooks in the event sourcing framework. The program outputs aggregated statistics | ||
| // after running a set of games. | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/rand" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/hallgren/eventsourcing" | ||
| "github.com/hallgren/eventsourcing/aggregate" | ||
| "github.com/hallgren/eventsourcing/eventstore/memory" | ||
| "github.com/hallgren/eventsourcing/example/tictactoe" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Buffered channels to receive move and result events | ||
| chanMoved := make(chan string, 100) | ||
| chanResult := make(chan string, 100) | ||
|
|
||
| // Aggregated counts of moves and end results | ||
| moveResult := map[string]int{ | ||
| "XMoved": 0, | ||
| "OMoved": 0, | ||
| } | ||
| endResults := map[string]int{ | ||
| "Draw": 0, | ||
| "XWon": 0, | ||
| "OWon": 0, | ||
| } | ||
| // In-memory event store | ||
| es := memory.Create() | ||
|
|
||
| // Register the TicTacToe Game aggregate | ||
| aggregate.Register(&tictactoe.Game{}) | ||
|
|
||
| // Hook to capture final game result events | ||
| err := aggregate.SetSaveHook(func(events []eventsourcing.Event) { | ||
| lastEvent := events[len(events)-1] | ||
| chanResult <- lastEvent.Reason() | ||
| }, &tictactoe.Game{}) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Hook to capture move events (XMoved/OMoved) | ||
| err = aggregate.SetSaveHook(func(events []eventsourcing.Event) { | ||
| for _, event := range events { | ||
| switch event.Data().(type) { | ||
| case *tictactoe.XMoved, *tictactoe.OMoved: | ||
| chanMoved <- event.Reason() | ||
| } | ||
| } | ||
| }, &tictactoe.Game{}) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| // waitgroup to sync the finish of the async workers | ||
| wg := sync.WaitGroup{} | ||
| wg.Add(2) | ||
|
|
||
| // Start async workers for processing move and result events | ||
| go movesWorker(chanMoved, moveResult, &wg) | ||
| go resultWorker(chanResult, endResults, &wg) | ||
|
|
||
| // Simulate and save 10 TicTacToe games | ||
| for i := 0; i < 10; i++ { | ||
| game := PlayGame() | ||
| aggregate.Save(es, game) | ||
| } | ||
|
|
||
| // Close channels to signal no more incoming data | ||
| close(chanMoved) | ||
| close(chanResult) | ||
| fmt.Println("Events are saved, wait for workers to finish.") | ||
| wg.Wait() | ||
|
|
||
| // Print out aggregated move and result statistics | ||
| fmt.Println(moveResult, endResults) | ||
| } | ||
|
|
||
| func resultWorker(c chan string, m map[string]int, wg *sync.WaitGroup) { | ||
| for { | ||
| select { | ||
| case result, ok := <-c: | ||
| // no more results | ||
| if !ok { | ||
| wg.Done() | ||
| fmt.Println("results worker finsished") | ||
| return | ||
| } | ||
| time.Sleep(50 * time.Millisecond) | ||
| m[result]++ | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // do some time consuming operations in the go routine | ||
| func movesWorker(c chan string, m map[string]int, wg *sync.WaitGroup) { | ||
| for { | ||
| select { | ||
| case move, ok := <-c: | ||
| // no more moves | ||
| if !ok { | ||
| wg.Done() | ||
| fmt.Println("moves worker finsished") | ||
| return | ||
| } | ||
| time.Sleep(50 * time.Millisecond) | ||
| m[move]++ | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func PlayGame() *tictactoe.Game { | ||
| game := tictactoe.NewGame() | ||
| for !game.Done() { | ||
| x := rand.Intn(3) | ||
| y := rand.Intn(3) | ||
| game.PlayMove(x, y) | ||
| } | ||
| return game | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| module github.com/hallgren/eventsourcing | ||
|
|
||
| go 1.19 | ||
| go 1.25 | ||
|
|
||
| require github.com/hallgren/eventsourcing/core v0.4.0 | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needing go 1.25 to be able to test with
testing/synctestis not optimal.