feat(schema/appdata)!: make commit async (#21306)
This commit is contained in:
+13
-39
@@ -19,50 +19,24 @@ type AsyncListenerOptions struct {
|
||||
DoneWaitGroup *sync.WaitGroup
|
||||
}
|
||||
|
||||
// AsyncListenerMux returns a listener that forwards received events to all the provided listeners asynchronously
|
||||
// with each listener processing in a separate go routine. All callbacks in the returned listener will return nil
|
||||
// except for Commit which will return an error or nil once all listeners have processed the commit. The context
|
||||
// is used to signal that the listeners should stop listening and return. bufferSize is the size of the buffer for the
|
||||
// channels used to send events to the listeners.
|
||||
// AsyncListenerMux is a convenience function that calls AsyncListener for each listener
|
||||
// with the provided options and combines them using ListenerMux.
|
||||
func AsyncListenerMux(opts AsyncListenerOptions, listeners ...Listener) Listener {
|
||||
asyncListeners := make([]Listener, len(listeners))
|
||||
commitChans := make([]chan error, len(listeners))
|
||||
for i, l := range listeners {
|
||||
commitChan := make(chan error)
|
||||
commitChans[i] = commitChan
|
||||
asyncListeners[i] = AsyncListener(opts, commitChan, l)
|
||||
asyncListeners[i] = AsyncListener(opts, l)
|
||||
}
|
||||
mux := ListenerMux(asyncListeners...)
|
||||
muxCommit := mux.Commit
|
||||
mux.Commit = func(data CommitData) error {
|
||||
if muxCommit != nil {
|
||||
err := muxCommit(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, commitChan := range commitChans {
|
||||
err := <-commitChan
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return mux
|
||||
return ListenerMux(asyncListeners...)
|
||||
}
|
||||
|
||||
// AsyncListener returns a listener that forwards received events to the provided listener listening in asynchronously
|
||||
// in a separate go routine. The listener that is returned will return nil for all methods including Commit and
|
||||
// an error or nil will only be returned in commitChan once the sender has sent commit and the receiving listener has
|
||||
// processed it. Thus commitChan can be used as a synchronization and error checking mechanism. The go routine
|
||||
// an error or nil will only be returned when the callback returned by Commit is called.
|
||||
// Thus Commit() can be used as a synchronization and error checking mechanism. The go routine
|
||||
// that is being used for listening will exit when context.Done() returns and no more events will be received by the listener.
|
||||
// bufferSize is the size of the buffer for the channel that is used to send events to the listener.
|
||||
// Instead of using AsyncListener directly, it is recommended to use AsyncListenerMux which does coordination directly
|
||||
// via its Commit callback.
|
||||
func AsyncListener(opts AsyncListenerOptions, commitChan chan<- error, listener Listener) Listener {
|
||||
func AsyncListener(opts AsyncListenerOptions, listener Listener) Listener {
|
||||
commitChan := make(chan error)
|
||||
packetChan := make(chan Packet, opts.BufferSize)
|
||||
res := Listener{}
|
||||
ctx := opts.Context
|
||||
@@ -151,11 +125,11 @@ func AsyncListener(opts AsyncListenerOptions, commitChan chan<- error, listener
|
||||
}
|
||||
}
|
||||
|
||||
if listener.Commit != nil {
|
||||
res.Commit = func(data CommitData) error {
|
||||
packetChan <- data
|
||||
return nil
|
||||
}
|
||||
res.Commit = func(data CommitData) (func() error, error) {
|
||||
packetChan <- data
|
||||
return func() error {
|
||||
return <-commitChan
|
||||
}, nil
|
||||
}
|
||||
|
||||
return res
|
||||
|
||||
Reference in New Issue
Block a user