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

Use streaming mode for watching bridge account #879

Open
sameh-farouk opened this issue Oct 22, 2023 · 0 comments
Open

Use streaming mode for watching bridge account #879

sameh-farouk opened this issue Oct 22, 2023 · 0 comments
Labels
type_feature New feature or request
Milestone

Comments

@sameh-farouk
Copy link
Member

sameh-farouk commented Oct 22, 2023

for {
select {
case <-ctx.Done():
return ctx.Err()
default:
log.Info().Str("account", opRequest.ForAccount).Str("horizon", client.HorizonURL).Str("cursor", opRequest.Cursor).Msgf("fetching stellar transactions")
response, err := client.Transactions(opRequest)
if err != nil {
log.Err(err).Msg("Error getting transactions for stellar account")
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(5 * time.Second):
continue
}
}
for _, tx := range response.Embedded.Records {
mintEvents, err := w.processTransaction(tx)
if err != nil {
return err
}
mintChan <- MintEventSubscription{
Events: mintEvents,
}
opRequest.Cursor = tx.PagingToken()
}
if len(response.Embedded.Records) == 0 {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(10 * time.Second):
}
}
}

Instead of using a for loop to send requests and wait for 10 seconds when no new transactions are found, we can use the Stellar SDK stream mode to subscribe to an event-stream that gives us the latest payment information for the bridge account. When no more transactions are found, the stream stays open and new payments are sent as they are made.

Some benefits of this approach are:

  • We can get real-time updates on the status of payments.
  • We can avoid sending too many requests to the stellar network.
  • We can save resources (cpu time, network traffic) by not sending unnecessary requests.

See the client stream methods that can be used:
https://github.com/stellar/go/blob/master/clients/horizonclient/client.go#L603

@sameh-farouk sameh-farouk added this to the later milestone Oct 31, 2023
@xmonader xmonader added this to 3.14.x Nov 5, 2023
@xmonader xmonader removed this from 3.14.x Nov 5, 2023
@xmonader xmonader removed this from 3.13.x Nov 5, 2023
@xmonader xmonader added this to 3.14.x Nov 5, 2023
@sameh-farouk sameh-farouk added the type_feature New feature or request label Nov 6, 2023
@renauter renauter removed this from 3.14.x Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type_feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant