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

Request polling, Investigation #232

Closed
rodolfopietro97 opened this issue Nov 13, 2023 · 6 comments · Fixed by #293
Closed

Request polling, Investigation #232

rodolfopietro97 opened this issue Nov 13, 2023 · 6 comments · Fixed by #293
Assignees

Comments

@rodolfopietro97
Copy link
Contributor

rodolfopietro97 commented Nov 13, 2023

Connex has function pollHead to get block current block. Similar to web socket.
Can be useful to implement a similar generic function that "poll" a generic client function?

@rodolfopietro97 rodolfopietro97 changed the title Request poolling, Investigation Request polling, Investigation Nov 13, 2023
@rodolfopietro97
Copy link
Contributor Author

rodolfopietro97 commented Nov 14, 2023

Create a generic poll that input a function. Pool can be of two kind:

  1. Blocking pool -> Wait the result, block execution flow until condition is not satisfied
function blockingPoll(function: (...args: TArgs) => ReturnType, exitCondition: boolean, options) => Promise<ReturnType>
  1. Non blocking poll ->Similar to websocket, gives to handlerCallBack the responsibility of doing actions without block execution flow
function nonBlockingPool(function: () => ReturnType, handlerCallBack: (ReturnType) => void, options) => void

@Valazan
Copy link
Contributor

Valazan commented Nov 14, 2023

Can you provide some examples in which it can be useful to apply the blocking pool and the non blocking pool?

@rodolfopietro97
Copy link
Contributor Author

rodolfopietro97 commented Nov 15, 2023

Blocking

const balanceBefore = await thorClient.getBalance(receiverAddress)
const expectedBalanceAfter = balanceBefore + 1VET

const tx = new Transaction(...) // New transaction that transfer 1 VET to receiver
await thorClient.sendTransaction(tx.id)

/*
 * Until the condition"balanceBefore < expectedBalanceAfter" will not satisfied
 * function "thorClient.getBalance(receiverAddress)" will be called every 1 second
 *
 * EXECUTION FLOW IS BLOCKED MEANWHILE
 */
const newBalance = blockingPoll(
     thorClient.getBalance(receiverAddress), 
     balanceBefore < expectedBalanceAfter, 
     {
          requestTime: 1 SECOND
     }
)

@rodolfopietro97
Copy link
Contributor Author

rodolfopietro97 commented Nov 15, 2023

NON Blocking

const balanceBefore = await thorClient.getBalance(receiverAddress)
const expectedBalanceAfter = balanceBefore + 1VET

const tx = new Transaction(...) // New transaction that transfer 1 VET to receiver
await thorClient.sendTransaction(tx.id)

/*
 * Until the condition"balanceBefore < expectedBalanceAfter" will not satisfied
 * function "thorClient.getBalance(receiverAddress)" will be called every 1 second.
 * BUT in this case each result of "thorClient.getBalance(receiverAddress)" is given as input of 
 * an 'handlerCallback'.
 *
 * EXECUTION FLOW IS NOT BLOCKED MEANWHILE
 */
const newBalance = nonBlockingPoll(
     thorClient.getBalance(receiverAddress), 
    ({ balance, ... }) => {
        console.log(`Current balance is ${balance}`)

        if(balance >= expectedBalanceAfter) console.log('You have received money and you can buy the lambo!!!')
     },
     { requestTime: 1 SECOND }
)

// ...
doOtherStuff()

@rodolfopietro97
Copy link
Contributor Author

@Valazan added some pseudocode examples

@pierobassa
Copy link
Member

pierobassa commented Nov 21, 2023

As discussed, for non-blocking polling we'll work with the polling request-manager pattern

https://www.enterpriseintegrationpatterns.com/patterns/conversation/Polling.html

@rodolfopietro97 rodolfopietro97 linked a pull request Nov 23, 2023 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants