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

Optimization of invocations of ShippingMethod checkers #536

Closed
chladog opened this issue Oct 30, 2020 · 1 comment
Closed

Optimization of invocations of ShippingMethod checkers #536

chladog opened this issue Oct 30, 2020 · 1 comment
Milestone

Comments

@chladog
Copy link
Contributor

chladog commented Oct 30, 2020

Is your feature request related to a problem? Please describe.
Currently all shipping method checkers are being called on every modification to the Order.
This is far from optimal resources utilization and makes async checks especially with external services slow and expensive.

Describe the solution you'd like

  • only re-run the checkers if a ShippingMethod is already assigned to the order.
  • if so, only re-check the single assigned checker.
  • introduce new config property "deferred" that would defer a check (not run check on every Order change) to "checkpoints" (eligibleShippingMethods call, OrderStateTransition from "AddingItems" - maybe checkpoints could be configured as new Event, so the custom "checkpoints" could be added by publish() and check would run on every Event emission.
    This deferred option would be viable option for more resource expensive checks - e.g. calls to external services, DB queries etc.

Describe alternatives you've considered
Saving a state of the Order when last check was invoked and skip the heavy lifting if not needed.

Slack thread: https://vendure-ecommerce.slack.com/archives/CKYMF0SU8/p1604053389143600

@michaelbromley michaelbromley added this to the v0.17.0 milestone Oct 30, 2020
@michaelbromley
Copy link
Member

Another solution to the problem of minimizing redundant checks:

We could have an approach kinda similar to React's shouldComponentUpdate method, i.e. allow the dev to provide a function which is used to determine whether or not to re-run the check.

type ShouldRunCheckFn<T> = (
  ctx: RequestContext,
  order: Order,
  args: ConfigArgValues) => T | Promise<T>;

The return type T gets cached and is compared to the last result (using an object-equivalence comparison). If it is unchanged, the check skipped.

For example, imagine a checker which depends country and postcode of the order:

const checker = new ShippingEligibilityChecker({
    code: 'checker',
    description: [],
    args: {},
    check: (ctx, order) => {
        // some super expensive async check...
    },
    shouldRunCheck: (ctx, order) => {
        return order.shippingAddress;
    },
});

With the above checker, each time the order contents are changed, before running the check function, Vendure would first run the shouldRunCheck function and compare the shippingAddress object with the cached object from the last check. If they are equal, the check function will be skipped.

michaelbromley added a commit that referenced this issue Nov 5, 2020
Relates to #536. This commit implements the following changes:

1. The `check` function is only run on changes to the order _if_ a ShippingMethod has been assigned
 to the Order.
2. If the order has a ShippingMethod, then _only_ that method's `check` function is invoked on
changes.
michaelbromley added a commit that referenced this issue Nov 9, 2020
Relates to #536. Allows one to optimize calls to the `check()` function in order to prevent
unnecessary expensive calls.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants