question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suppliers.memoize should take an option to memoize exceptions

See original GitHub issue

Related Pull request to fix the documentation https://github.com/google/guava/pull/2916 has been merged.

If the delegate in the Suppliers.memoize throws an exception on calling get() then further calls to the get() method of the Memoized suppliers still goes to the delegate. With the pull request the Javadocs clarify that there is a danger that the underlying delegate could be called more than once.

What we need is a method with the following signature

Suppliers.memoize(Supplier<T> delegate, boolean memoizeExceptions)

By default the single argument memoize method could delegate to the above method with a “false” flag i.e, exceptions are NOT memoized by default. However if true is passed then even when the delegate throws an exception, that exception is stored by the memoized supplier and is re-thrown everytime it is called.

Use cases where this can be used

  • A class X uses a memoized Supplier the delegate talks to an external Paypal like service or a configuration service. When the delegate fails we do not want to retry the external service (until such time as we have a notification that the external service is back up in which case class X re-initializes the supplier)
  • A class X has a memoized supplier with expiration of 5 seconds and wants an absolute guarantee that even in the case of failures the underlying delegate is NEVER called more than once in 5 seconds.

I am happy to contribute a patch if this new API is accepted.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kannanekanathcommented, Mar 25, 2020

As original requestor of this issue and having read Guava guideline that “If you find something is missing in guava, you should log a request and do your own thing” that is exactly what I did.

In general I agree it is perhaps not a good idea to store an exception as a member variable which we have to store if we are to throw the exact exception. In my code I have done exactly that. As @tkruse points out if that is a problem you can store a boolean variable to throw some generic exception message.

The key problem here is that the original purpose of protecting an underlying resource from being flooded is actually made more worse by Guava as it gives the impression to the developer that it is a good idea to wrap expensive calls into this method. If not already done this should at least be clarified in the javadoc to avoid confusion.

1reaction
kevinb9ncommented, Sep 6, 2017

I wonder if a useful generalization of this advice is, “don’t threat Throwable instances as data.”

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Suppliers.memoize when method throws Checked ...
Use UncheckedIOException. You're tagging java-8, so you should use the UncheckedIOException which is present for this very use case.
Read more >
com.google.common.base.Suppliers.memoize java ... - Tabnine
When the underlying delegate throws an exception then this memoizing supplier will keep delegating calls until it returns valid data. If delegate is...
Read more >
Introduction to Guava Memoizer - Baeldung
Learn how to memoize results of method calls using Google Guava's Memoizer.
Read more >
java - Ability to forget the memoized supplier value
Guava has a feature request which is asking to provide the ability to forget memoized value on a given supplier. On top on...
Read more >
Suppliers (Guava: Google Core Libraries for Java 31.1-jre API)
When the underlying delegate throws an exception then this memoizing supplier will keep delegating calls until it returns valid data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found