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

Add suggested params options to AppClientCallCoreParams #293

Closed
PhearZero opened this issue Jun 22, 2024 · 2 comments
Closed

Add suggested params options to AppClientCallCoreParams #293

PhearZero opened this issue Jun 22, 2024 · 2 comments
Labels
question Further information is requested

Comments

@PhearZero
Copy link
Member

PhearZero commented Jun 22, 2024

Problem

Typed clients attempt to fetch suggested params on every method invocation. When calling a large amount of methods, the algod node rate limits the request and the transaction factory fails. This is important when the sendParams is configured to skipSending

Solution

It would be nice to allow passing in of suggested params to AppClientCallCoreParams. It may be good to use a Mutex|Semaphore for the fetch call or it could even be as simple as caching the params with a low TTL.

Proposal

It would be possible to combine all three methods, not a requirement to resolve the issue. The solutions here are just illustrative and not prescriptive.

Passing in Params

// fetch suggestedParams
client.abiMethod([], {suggestedParams})

Mutex|Semaphore for suggestedParams

Only allow one suggested params request in flight per any instance of the client.

Here is an example for pending transactions that could be adapted to the suggestedParams call.

import {Mutex, Semaphore} from "async-mutex";
const fetchMutex = new Mutex();
const fetchSemaphore = new Semaphore(5);

export function fetchPendingTransactionInformation(txId: string, signal: AbortSignal) {
    return fetchSemaphore.runExclusive(async () =>  {
        const url = `${baseServer}:${port}/v2/transactions/pending/${txId}`
        const response = await fetch(url, {signal, headers: { 'X-Algo-API-Token': token }})
        return response.json()
    })

}
export function fetchPendingTransactionInformationSync(txId: string, signal: AbortSignal) {
    return fetchMutex.runExclusive(async () =>  {
        const url = `${baseServer}:${port}/v2/transactions/pending/${txId}`
        const response = await fetch(url, {signal, headers: { 'X-Algo-API-Token': token }})
        return response.json()
    })
}

Caching Params

client.abiMethod([], {suggestedParamsCacheTime: 1000})

Pros and Cons

Pros of passing in suggestedParams

  • Simple implementation
  • Moves the implementation details to the developer's project
  • Allows for fine grained control over fetching params and AbortControlers since the developer owns the fetch call

Cons of passing in suggestedParams

  • Suggested params could change during the duration of the execution
  • Less automagic then the other solutions, requires developer implementation

Pros of Mutex

  • Developer does not need to think of the amount of client calls
  • Developer does not need to configure the client to get this behavior

Cons of Mutex

  • Still can rate limit based on the response time and IO of the local machine. Only one request will be in-flight but still could send them at a high request per second
  • Generally it is not configurable (also a positive)

Pros of Caching

  • Simple implementation

Cons of Caching

  • Suggested params could change while the cache is not stale.

Dependencies

No

Related Issues

@robdmoore
Copy link
Contributor

I think that as we move to AlgorandClient this problem will be solved.

In the meantime, the ability to pass in suggestedParams already exists, when you construct the app client you can pass them in and they will get used across all calls for that client. There is currently no way of overriding for a single call though.

@PhearZero
Copy link
Member Author

Sounds good, thanks for the feedback! I'll wait for the new client, it will come in handy for a few side projects.

@robdmoore robdmoore added the question Further information is requested label Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants