-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Adds transaction with watched key example script. #2297
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
81738e8
transction example improved
markanaAjay 31fd259
readme fixed
sailingwithsandeep 6cea630
delay added for watched key changes
sailingwithsandeep b7444c3
pooling error fixed on recursion
sailingwithsandeep 731c1a8
Minor comment update.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// This example shows you sample transaction for how to achieve optimistic locking using WATCH with check-and-set (CAS) behavior. | ||
// If any watched key changes outside the session then entire transaction will be aborted. | ||
// To know more about isolated execution & redis transaction: https://github.com/redis/node-redis/blob/master/docs/isolated-execution.md | ||
// https://redis.io/docs/manual/transactions | ||
|
||
// We just have to repeat the operation by calling the function again(recursion) hoping this time we'll not get race condition. | ||
// restrictFunctionCalls is a counter function to limit recursive calls | ||
|
||
import { createClient, WatchError } from 'redis'; | ||
|
||
const client = createClient(); | ||
await client.connect(); | ||
|
||
/** | ||
* | ||
* @param {function} fn | ||
* @param {number} maxCalls | ||
* @returns a function that is being called based on maxCalls | ||
*/ | ||
function restrictFunctionCalls(fn, maxCalls) { | ||
let count = 1; | ||
return function (...args) { | ||
return count++ < maxCalls ? fn(...args) : false; | ||
}; | ||
} | ||
let fn = restrictFunctionCalls(transaction, 4); | ||
async function transaction() { | ||
try { | ||
await client.executeIsolated(async (isolatedClient) => { | ||
await isolatedClient.watch('paymentId:1259'); | ||
const multi = isolatedClient | ||
.multi() | ||
.set('paymentId:1259', 'Payment Successfully Completed!') | ||
.set('paymentId:1260', 'Refund Processed Successfully!'); | ||
await multi.exec(); | ||
console.log('Transaction completed Successfully!'); | ||
}); | ||
} catch (error) { | ||
if (error instanceof WatchError) { | ||
console.log('Transaction Failed Due To Concurrent Modification!'); | ||
fn(); | ||
} else { | ||
console.log(`Error: ${error}`); | ||
} | ||
} | ||
} | ||
|
||
transaction(); | ||
|
||
await client.quit(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason to change anything in this
README.md
at all except for adding a new line entry in the table for your new script?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sailingwithsandeep please see @simonprickett's question above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@simonprickett Hey so sorry i just realised i changed the order of readme file's coding guidelines list. it happened because i copied that readme file from previous pull request and somehow changed the order. i hope you understand my concern. Again extremely sorry for changing unwanted things. (P.S. I am so excited that i am contributing into redis community so, given that state i might have rushed it.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't rush @sailingwithsandeep and only change the things that are necessary :) please change it back and we will re-review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@simonprickett @SuzeShardlow Hey i committed the changes hopefully this time there won't be an issue. Again sorry for the trouble.