Skip to content
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

Close errors channel on events listening canceled #84

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions blockchain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ func (c *Client) StartEventsListening(

liveChangesC := sub.Chan()
histChangesC := make(chan types.StorageChangeSet)
var wg sync.WaitGroup

// Query historical changes.
var cancelled atomic.Value
cancelled.Store(false)
wg.Add(1)
go func(begin types.BlockNumber, liveChanges <-chan types.StorageChangeSet, histChangesC chan types.StorageChangeSet) {
defer wg.Done()
defer close(histChangesC)

set := <-liveChanges // first live changes set block is the last historical block
Expand Down Expand Up @@ -123,7 +126,9 @@ func (c *Client) StartEventsListening(

// Sequence historical and live changes.
changesC := make(chan types.StorageChangeSet)
wg.Add(1)
go func(histChangesC, liveChangesC <-chan types.StorageChangeSet, changesC chan types.StorageChangeSet) {
defer wg.Done()
defer close(changesC)

for set := range histChangesC {
Expand All @@ -137,7 +142,9 @@ func (c *Client) StartEventsListening(

// Decode events from changes skipping blocks before 'begin'.
eventsC := make(chan blockEvents)
wg.Add(1)
go func(changesC <-chan types.StorageChangeSet, eventsC chan blockEvents) {
defer wg.Done()
defer close(eventsC)

for set := range changesC {
Expand Down Expand Up @@ -190,6 +197,8 @@ func (c *Client) StartEventsListening(
once.Do(func() {
sub.Unsubscribe()
cancelled.Store(true)
wg.Wait()
close(c.errsListening)
c.isListening = 0
})
}
Expand Down
Loading