-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 after-publish cart updates for speed #5477
Conversation
Signed-off-by: Eric Dobbertin <[email protected]>
This is prep work for being able to have plugins register cart transforms that run prior to save. Signed-off-by: Eric Dobbertin <[email protected]>
This improves the speed of cart updates and simplifies the code paths. Signed-off-by: Eric Dobbertin <[email protected]>
Signed-off-by: Eric Dobbertin <[email protected]>
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.
Some minor nits but overall looks good. Looks like a lot of work sorting this out so thanks for that!
imports/plugins/core/cart/server/no-meteor/mutations/saveManyCarts.js
Outdated
Show resolved
Hide resolved
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.
Signed-off-by: Eric Dobbertin <[email protected]>
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.
Just one item I think needs to be changed, and a couple of questions/comments that could be deferred to another ticket
|
||
let writeErrors; | ||
try { | ||
Logger.trace({ ...logCtx, bulkWrites }, "Running bulk op"); |
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.
Yay, for use of Logger.trace
} | ||
|
||
/** | ||
* @summary Get updated prices for a single cart, and check whether there are any changes. |
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.
I think we should be adding/setting a flag here for when the cart has been modified so that the UX could display some sort of message.
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.
I'd need some more information about what you mean. For price changes, that was the idea behind why we have a priceWhenAdded
field on there that doesn’t ever change. The thought was that the client can compare price
with priceWhenAdded
and show a “price has increased/decreased” or whatever messaging they want.
If there are other automatic changes clients would care about, we could do something similar.
imports/plugins/core/cart/server/no-meteor/util/updateCartItemsForVariantChanges.js
Show resolved
Hide resolved
Signed-off-by: Eric Dobbertin <[email protected]>
Resolves #5098
Impact: minor
Type: performance
Changes
After-cart-update performance improvements
There were several "afterCartUpdate" and "afterCartCreate" listeners in various plugins that were essentially transforming the cart but doing it without any coordination was chaotic and slow. Now there is instead a single cart transform function in the Cart plugin, which always runs before every cart insert or update. Plugins now register custom transforms for this using a pattern like this in
registerPlugin
:The main cart plugin transform then runs these custom ones in priority order. This is a bit faster, uses less memory, and resulted in consolidated and cleaner code in the Cart plugin.
After-publish performance improvements
The cart plugin has a
afterPublishProductToCatalog
listener that updates cart item properties from catalog variant properties and, if any have changed, saves the cart. We found that this could be very slow and crash with a Mongo connection error when there are thousands of carts that contain the changed variant.Now the cart lookups and updates are batched so that memory usage and speed are optimal. This combined with the improved cart transformation flow described above appear to be ~10x speed improvement when publishing to catalog, based on local tests with 8,000 carts.
Other
getSurcharges
was callingextendCommonOrder
even when there were no surcharges defined. Now it exits early if there are no surcharges, which makes publishing and cart updates faster on shops that don't use surcharges.Breaking changes
None
Testing
Test publishing price and/or tax code changes to a variant or option that is in one or more carts. It should work and be faster than before.
You could also test this with a variant that is in 5-10k carts to verify it no longer crashes (but I've tested this thoroughly).
Almost all code related to carts has been moved around, though is mostly unchanged. The best way to test this is with something like the following smoke test flow in the example storefront: