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

cart: Cleanup cache after failed adapter call #196

Open
jeinfeldt opened this issue Mar 3, 2020 · 0 comments
Open

cart: Cleanup cache after failed adapter call #196

jeinfeldt opened this issue Mar 3, 2020 · 0 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@jeinfeldt
Copy link
Contributor

Hello friends,
we noticed we sometimes have inconsistencies in our flamingo cache compared with the source of our adapters for the cart.

Let us look at the addProduct function:

...
func (cs *CartService) AddProduct(...) (productDomain.BasicProduct, error) {
	if deliveryCode == "" {
		deliveryCode = cs.defaultDeliveryCode
	}
       
	cart, behaviour, err := cs.cartReceiverService.GetCart(ctx, session)
	if err != nil {
		cs.logger.WithContext(ctx).WithField(flamingo.LogKeySubCategory, "AddProduct").Error(err)

		return nil, err
	}
	// cart cache must be updated - with the current value of cart
	var defers cartDomain.DeferEvents
	defer func() {
		cs.updateCartInCacheIfCacheIsEnabled(ctx, session, cart)

		cs.handleEmptyDelivery(ctx, session, cart, deliveryCode)
		cs.dispatchAllEvents(ctx, defers)
	}()
      ...
}

In case this function is successfull, it stores the new cart in the cache following the logic in the defer function.

Let us assume the getCart call fails, this results with an error return, but the cache is kept in whatever state it is until it is stale.
For a get operation this is fine, but in case the cart is already changed by the adapter but fails nonetheless this results in an inconsistency between cache and adapter.

This behaviour can be observed across all application services of the cart module.
I would suggest to enhance the behaviour in defer to check for the presence of an error and
in case it is not nil throw a InvalidateCartEvent to reset the cart in the cache.
Whenever a client now receives an error and fetches the cart, at least it is the same cart as provided by the port implementations.

This would affect various methods in the cart application package.
I am unsure whether this is the right approach to tackle the problem, I am looking for a discussion on this issue.

@jeinfeldt jeinfeldt added the help wanted Extra attention is needed label Mar 3, 2020
@jeinfeldt jeinfeldt changed the title cart: Cleanup cache after failed port call cart: Cleanup cache after failed adapter call Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants