Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delay repeated WebClient get request

I am using the Spring 5 WebClient to repeatedly fetch some state of a running process from a REST api.

With help from here I for now came to this solution:

webClient.get().uri(...).retrieve.bodyToMono(State.class)
          .repeat()
          .skipUntil(state -> stateFinished())
          .limitRequest(1)
          .subscribe(state -> {...});

While this works, the get request is fired at a very high rate. What would be the right way to limit the request rate to let's say 1 request per second?

I tried using delayElements(Duration.ofSeconds(1)) but that only delays the results, not the request itself.

like image 828
Rosso Avatar asked Apr 21 '26 22:04

Rosso


1 Answers

You could use repeatWhen operator with your custom implementation of the companion Publisher

Mono.just("test")
        .repeatWhen(longFlux -> Flux.interval(Duration.ofSeconds(1)))
        .take(5)
        .log()
        .blockLast();

or with Repeate function from the reactor-addons

Mono.just("test")
        .repeatWhen(Repeat.times(Long.MAX_VALUE)
                .fixedBackoff(Duration.ofSeconds(1)))
        .take(5)
        .log()
        .blockLast();
like image 86
Alexander Pankin Avatar answered Apr 29 '26 05:04

Alexander Pankin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!