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

Allows CacheServiceInstance to customize the cache invalidation #368

Closed
cescoffier opened this issue Sep 26, 2022 · 1 comment
Closed

Allows CacheServiceInstance to customize the cache invalidation #368

cescoffier opened this issue Sep 26, 2022 · 1 comment
Assignees
Labels
enhancement Enhancements or Request for enhancement
Milestone

Comments

@cescoffier
Copy link
Contributor

(This is the first step of #299)

At the moment, the service discovery providers using the cache support (extending io.smallrye.stork.impl.CachingServiceDiscovery) are limited to a time-based cache. This issue is about allowing more advanced strategies such as background invalidation.

Design idea:

Add a method to CachingServiceDiscovery that allows customizing the "memorize" part of the pipeline:

// ...
    public CachingServiceDiscovery(String refreshPeriod) {
        try {
            this.refreshPeriod = DurationUtils.parseDuration(refreshPeriod, REFRESH_PERIOD);
        } catch (DateTimeParseException e) {
            throw new IllegalArgumentException(REFRESH_PERIOD + " for service discovery should be a number, got: " +
                    refreshPeriod,
                    e);
        }

        this.lastResults = Collections.emptyList();
        uni<ServiceInstance> list = Uni.createFrom().deferred(() -> fetchNewServiceInstances(this.lastResults)
                .invoke(l -> this.lastResults = l)
                .onFailure().invoke(this::handleFetchError)
                .onFailure().recoverWithItem(this.lastResults));
         this.instances = cache(list);
    }

// Overridable by the provider
public Uni<ServiceInstance> cache(Uni<ServiceInstance> uni) {
       return uni.memoize().atLeast(this.refreshPeriod);
}

To implement background invalidation, the provider would do something like:

AtomicBoolean invalidated = new AtomicBoolean();
@Override
public Uni<ServiceInstance> cache(Uni<ServiceInstance> uni) {
       return uni.memoize().unil(() -> invalidated.get())
                    .invoke(() -> invalidated.set(false));
}

public void invalidate() {
   invalidated.set(true);
}
@cescoffier cescoffier added this to the 1.3.0 milestone Sep 26, 2022
@cescoffier
Copy link
Contributor Author

Closed by #374

@cescoffier cescoffier added enhancement Enhancements or Request for enhancement core labels Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancements or Request for enhancement
Projects
None yet
Development

No branches or pull requests

2 participants