Skip to content

add auto-reconnect while still returning a timeout error #2

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

Merged
merged 1 commit into from
Mar 24, 2018

Conversation

aakselrod
Copy link

This allows the ZMQ SUB to automatically reconnect to a PUB socket when disconnected. It hides the disconnection and reconnection from the client; therefore, in use cases like btcwallet, there will be no logging of the disconnect/reconnect.

@aakselrod
Copy link
Author

This is an alternative implementation to Roasbeef/btcwallet#21

c.timeout)
if err != nil {
// Prevent CPU overuse by refused reconnection attempts.
time.Sleep(c.timeout)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should use an exponential back off here? For reference the btcsuite rpcclient does something similar: https://github.com/btcsuite/btcd/blob/master/rpcclient/infrastructure.go#L641

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used synchronously, which means each time we back off, we can delay processing of other events in an event loop that include a call to this function. For example, https://github.com/Roasbeef/btcwallet/blob/master/chain/bitcoind.go#L632 is in a loop that also checks for updates to watch lists and rescans. The rpcclient reconnect handler runs as a goroutine.

An option for doing the incremental back off would be to do that in a goroutine, and just sleep/return a timeout error in the synchronous portion.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option for doing incremental backoff is to add more state to Conn to keep track of last attempted reconnection time when disconnected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so even with this patch, we need reconnection logic within the wallet as well? As this just return a timeout error in either case, even if we're able to reconnect successfully.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the wallet already ignores read timeouts, this code just emulates those: https://github.com/Roasbeef/btcwallet/blob/master/chain/bitcoind.go#L636

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we reconnect successfully, the next attempt at reading will return a message if there is one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, thanks! OK, will test out this patch locally.

@@ -50,6 +62,7 @@ func connFromAddr(addr string) (net.Conn, error) {
// Conn is a connection to a ZMQ server.
type Conn struct {
conn net.Conn
topics []string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this value ever being set. So it would see that with this, we'd be able to reconnect, but then would lose notifications concerning the topics we care about?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comment on line 342.

@@ -326,7 +339,7 @@ func Subscribe(addr string, topics []string, timeout time.Duration) (*Conn, erro

conn.SetDeadline(time.Now().Add(10 * time.Second))

c := &Conn{conn, timeout}
c := &Conn{conn, topics, timeout}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where topics is set.

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

@Roasbeef Roasbeef merged commit 462a8a7 into lightninglabs:master Mar 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants