-
Notifications
You must be signed in to change notification settings - Fork 39
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
Tudor/reconnect new heads #1910
Changes from 3 commits
628dd39
5ed5d21
66d2ea8
af31fb1
877da93
ff770f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ import ( | |
// ForwardFromChannels - reads messages from the input channels, and calls the `onMessage` callback. | ||
// Exits when the unsubscribed flag is true. | ||
// Must be called as a go routine! | ||
func ForwardFromChannels[R any](inputChannels []chan R, unsubscribed *atomic.Bool, onMessage func(R) error) { | ||
func ForwardFromChannels[R any](inputChannels []chan R, unsubscribed *atomic.Bool, onMessage func(R) error, onBackendDisconnect func(), timeoutInterval time.Duration) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd maybe call the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is also called when the backend disconnects via the usual means. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usual meaning when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this bit here:
I'll add a comment to clarify it. |
||
inputCases := make([]reflect.SelectCase, len(inputChannels)+1) | ||
|
||
// create a ticker to handle cleanup, check the "unsubscribed" flag and exit the goroutine | ||
|
@@ -25,8 +25,10 @@ func ForwardFromChannels[R any](inputChannels []chan R, unsubscribed *atomic.Boo | |
inputCases[i+1] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ch)} | ||
} | ||
|
||
lastMessageTime := time.Now() | ||
unclosedInputChannels := len(inputCases) | ||
for unclosedInputChannels > 0 { | ||
// this mechanism removes closed input channels. When there is none left, the subscription is considered "disconnected". | ||
chosen, value, ok := reflect.Select(inputCases) | ||
if !ok { | ||
// The chosen channel has been closed, so zero out the channel to disable the case | ||
|
@@ -45,7 +47,12 @@ func ForwardFromChannels[R any](inputChannels []chan R, unsubscribed *atomic.Boo | |
if unsubscribed != nil && unsubscribed.Load() { | ||
return | ||
} | ||
// no message was received longer than the timeout. Exiting. | ||
if time.Since(lastMessageTime) > timeoutInterval { | ||
break | ||
} | ||
case R: | ||
lastMessageTime = time.Now() | ||
err := onMessage(v) | ||
if err != nil { | ||
// todo - log | ||
|
@@ -56,11 +63,14 @@ func ForwardFromChannels[R any](inputChannels []chan R, unsubscribed *atomic.Boo | |
continue | ||
} | ||
} | ||
if onBackendDisconnect != nil { | ||
onBackendDisconnect() | ||
} | ||
} | ||
|
||
// HandleUnsubscribe - when the client calls "unsubscribe" or the subscription times out, it calls `onSub` | ||
// Must be called as a go routine! | ||
func HandleUnsubscribe(connectionSub *rpc.Subscription, unsubscribed *atomic.Bool, onUnsub func()) { | ||
func HandleUnsubscribe(connectionSub *rpc.Subscription, _ *atomic.Bool, onUnsub func()) { | ||
<-connectionSub.Err() | ||
onUnsub() | ||
} |
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.
This assignment doesn't have the desired effect I don't think. It won't update the channel in the slice that was passed into ForwardFromChannels above.
To do that you'd need to reference the slice, something like:
I wasn't sure how this worked, so did this to convince myself lol: https://go.dev/play/p/YD7x0HaUtoO
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.
Actually even that fix might be problematic I think (if I'm reading the magical incantations correctly).
The
inputCases
you build up with reflection at the start ofForwardFromChannels()
will still be listening on the old channel.