-
Notifications
You must be signed in to change notification settings - Fork 338
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
Observe(rxgo.WithContext(ctx)) does not cancel observable? #342
Comments
According to Line 69 in 6d23b16
The current code for observing the eventSourceIterable is the following:
func (i *eventSourceIterable) Observe(opts ...Option) <-chan Item {
option := parseOptions(append(i.opts, opts...)...)
next := option.buildChannel()
i.Lock()
if i.disposed {
close(next)
} else {
i.observers = append(i.observers, next)
}
i.Unlock()
return next
} It indeed seems it doesn't handle context cancellations from options. Either you handle it from your code: func observe(index int, ob rxgo.Observable) context.CancelFunc {
ctx, cancel := context.WithCancel(context.Background())
go func() {
itemCh := ob.Observe(rxgo.WithContext(ctx))
OuterLoop:
for {
select {
case <-ctx.Done():
// close(itemCh) ?
break OuterLoop
case i, ok := <-itemCh:
if !ok {
break OuterLoop
}
log.Printf("Observer[%d]: %v", index, i.V)
}
}
log.Printf("Observer[%d] stopped", index)
}()
return cancel
} Or maybe patching the RxGo code with something like this might do the trick (untested): func (i *eventSourceIterable) Observe(opts ...Option) <-chan Item {
option := parseOptions(append(i.opts, opts...)...)
next := option.buildChannel()
i.Lock()
if i.disposed {
close(next)
} else {
i.observers = append(i.observers, next)
ctx := option.buildContext(context.Background())
if ctx.Done() != nil {
go func() {
<-ctx.Done()
close(next)
}()
}
}
i.Unlock()
return next
} Edit: fixed |
I think i had the same issue #318. |
I've noticed in many places that the context is not chained through (either processed from options list or from the parent observable). I've managed to work through most of my issues with cancellations not taking effect with careful selection of which operators/sources and passing through the context option at places where I can and know the parent won't honor the parent cancellation. |
Hi,
I am not sure I'm using RxGo the correct way or if this use case is not supported. I have one producer and multiple consumers which want to subscribe and unsubscribe for messages. Consumers can come and go, my idea was to use the rxgo.WithContext to "cancel" consumer subscriptions, but they don't get cancelled? Is this the intended behavior or am I missing something? I've been testing out some different approaches to no success.
The test I try with his this:
Produces the following output:
Have I just made a mistake somewhere which leads to the observable not being cancelled? Any help is much appreciated, thanks for a great package.
The text was updated successfully, but these errors were encountered: