Skip to content

Commit

Permalink
Make Close synchronous
Browse files Browse the repository at this point in the history
This fixes #1349 where events are attempted to be handled after database close
  • Loading branch information
AndrewSisley committed Apr 13, 2023
1 parent a761abc commit 2673ea4
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions events/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type simpleChannel[T any] struct {
// that the order of operations is preserved.
commandChannel chan any
eventBufferSize int
hasClosedChan chan struct{}
isClosed bool
}

Expand All @@ -42,6 +43,7 @@ type closeCommand struct{}
func NewSimpleChannel[T any](commandBufferSize int, eventBufferSize int) Channel[T] {
c := simpleChannel[T]{
commandChannel: make(chan any, commandBufferSize),
hasClosedChan: make(chan struct{}, 0),
eventBufferSize: eventBufferSize,
}

Expand Down Expand Up @@ -82,6 +84,9 @@ func (c *simpleChannel[T]) Close() {
}
c.isClosed = true
c.commandChannel <- closeCommand{}

// Wait for the close command to be handled, in order, before returning
<-c.hasClosedChan
}

func (c *simpleChannel[T]) handleChannel() {
Expand All @@ -92,6 +97,7 @@ func (c *simpleChannel[T]) handleChannel() {
close(subscriber)
}
close(c.commandChannel)
close(c.hasClosedChan)
return

case subscribeCommand[T]:
Expand Down

0 comments on commit 2673ea4

Please sign in to comment.