-
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 2 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,6 +25,7 @@ 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 { | ||
chosen, value, ok := reflect.Select(inputCases) | ||
|
@@ -45,7 +46,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 +62,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 would deadlock I think, you can't take the write-lock while it's under read-lock. Need to copy the notifiers list before iterating or make list of ones to delete after maybe.
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.
I haven't changed this bit. Just reformatted. It might have been there before. I'll have a look