Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive Streams: Spring WebFlux - subscribe to existing publisher

I am currently migrating our existing Spring asynchronous REST architecture to Spring's new WebFlux library and have a question around joining multiple requests so that they can listen for the same published response.

Use Case is as follows:

  1. Client A connects to our web server and requests data
  2. We hit our cache to check if we have the data there
  3. We don't, so we go and retrieve this data (Client A has subscribed and waits for a response)
  4. Client B connects to our web server and requests the same data (hits the same endpoint)
  5. We check the cache, data is still not there
  6. As we are already fetching this data for Client A we don't want to make another request, however, we also do not want to turn Client B away. Client B should be able to listen for the same information

How can Client B subscribe to the same response stream that Client A is waiting for?

like image 567
wild_nothing Avatar asked Jan 29 '23 07:01

wild_nothing


1 Answers

"Client A has subscribed and waits for a response" I suppose the request is coded as a Mono and client A sibscribes to it literally:

Subscriber<Response> clientA = ... Mono<Response> request = makeRequest(...); request.subscribe(clientA);

then clientB should subscribe the same way:

Subscriber<Response> clientB = ... request.subscribe(clientB);

Moreover, the cache should contain not the previously saved response data, but the requests themselves, of type Mono<Response>. Then, if such a request is found in the cache, new clients simply subscribe to it, regardless of was that request already completed or not.

like image 150
Alexei Kaigorodov Avatar answered Jan 31 '23 09:01

Alexei Kaigorodov