Skip to content
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

refactor: Use cleanup manager to unsubscribe web3 subscriptions #432

Merged
merged 2 commits into from
Nov 18, 2024

Conversation

bgins
Copy link
Contributor

@bgins bgins commented Nov 8, 2024

Summary

This pull request makes the following changes:

  • Unsubscribe from web3 events using cleanup manager
  • Add unsubscribeSub helper

This pull request refactors our web3 subscriptions to unsubscribe using our cleanup manager.

Test plan

Add a temporary print line to the new unsubscribeSub helper function to confirm that it is called when exiting the process.

Details

This change unsubscribes as a part of our clean up when we receive a cancellation signal.

@cla-bot cla-bot bot added the cla-signed label Nov 8, 2024
@bgins bgins marked this pull request as ready for review November 8, 2024 19:33
@bgins bgins requested a review from a team as a code owner November 8, 2024 19:33
@bgins bgins self-assigned this Nov 8, 2024
@bgins bgins force-pushed the bgins/refactor-web3-unsubscribe branch from d7e6c69 to 3fa2960 Compare November 8, 2024 19:45
@cla-bot cla-bot bot added the cla-signed label Nov 8, 2024
Copy link
Collaborator

@walkah walkah left a comment

Choose a reason for hiding this comment

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

Seems to work. Not sure if this is related, but I get the following output when existing a service (this is from solver):

C2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
Unsubscribing
Unsubscribing
Unsubscribing
2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
Unsubscribing
Unsubscribing
2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
Unsubscribing

Seems like we're trying to restart listeners maybe one last time?

@bgins bgins force-pushed the bgins/refactor-web3-unsubscribe branch from 3fa2960 to 9b9ef55 Compare November 18, 2024 21:27
@bgins
Copy link
Contributor Author

bgins commented Nov 18, 2024

Seems to work. Not sure if this is related, but I get the following output when existing a service (this is from solver):

C2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
Unsubscribing
Unsubscribing
Unsubscribing
2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
Unsubscribing
Unsubscribing
2024-11-18T15:22:22-05:00 ERR ../../../.asdf/installs/golang/1.22.6/go/src/runtime/asm_arm64.s:1222 > error starting listeners: cancel by context reconnect in 2 seconds
Unsubscribing

Seems like we're trying to restart listeners maybe one last time?

It's related. We aren't properly handling cancellation when starting these subscriptions:

for _, collection := range eventChannels.collections {
c := collection
go func() {
for {
err := c.Start(sdk, ctx, cm)
if err != nil {
log.Error().Msgf("error starting listeners: %s reconnect in 2 seconds", err.Error())
}
time.Sleep(time.Second * 2)
}
}()
}

We call Start over each of the subscriptions, passing in the cleanup manager, which this PR uses to call unsubscribe. This loop will continue to attempt to connect though, so we likely want to check for cancellation. We may also want exponential backoff and max retries here as well.

@bgins bgins merged commit 53b17c5 into main Nov 18, 2024
4 checks passed
@bgins bgins deleted the bgins/refactor-web3-unsubscribe branch November 18, 2024 21:39
walkah pushed a commit that referenced this pull request Dec 10, 2024
* chore: Add unsubscribe helper

* refactor: Use cleanup manager to unsubscribe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants