Skip to content

Enable ReactiveRedisTemplate for multi-tenancy usage #2145

Closed

Description

@petromir

Hi,

I would like to use ReactiveRedisTemplate in multitenant environment. In short, the option to use single ReactiveRedisTemplate which can connect to multiple Redis instances based on tenant values passed in reactive context.
Unfortunately, I can not connect the reactive context with the connection factory so I can create different connections (or using different connection pool) based on tenant value in the context.

One option is to override doInConnection, but it has private modifier. Changing it will allow me to provide custom logic about connection creation.

Activity

  1. changed the title [-]Change visibility of doInConnection method in ReactiveRedisTemplate[/-] [+]Enable `ReactiveRedisTemplate` for multi-tenancy usage[/+] on Aug 23, 2021
  2. added
    type: enhancement
    and removed
    status: waiting-for-triage
    on Aug 23, 2021
  3. mp911de commented on Aug 23, 2021

    @mp911de
    Member

    doInConnection is an internal method that handles resource allocation. What we should look for is how to enable ReactiveRedisTemplate for multi-tenancy usage. The first part of resource allocation is having a Mono emitting ReactiveRedisConnection. We could introduce a Mono<ReactiveRedisConnection> getConnection() method as extension point.

  4. petromir commented on Aug 24, 2021

    @petromir
    ContributorAuthor

    Thanks a lot. When do you plan to tackle this?

  5. mp911de commented on Aug 24, 2021

    @mp911de
    Member

    Having a pull request would improve things in terms of timing so we could integrate it faster.

  6. petromir commented on Aug 24, 2021

    @petromir
    ContributorAuthor

    I knew you gonna say that 😄

  7. petromir commented on Aug 24, 2021

    @petromir
    ContributorAuthor

    We could introduce a Mono getConnection() method as extension point.

    I assume you mean in having this in ReactiveRedisTemplate?

  8. mp911de commented on Aug 24, 2021

    @mp911de
    Member

    Yes, exactly. A protected method that can be overridden by subclasses. Right now, the calls to obtain a connection return plain ReactiveConnection on the connection factory and are not suitable to access the reactor context. So solving the issue within the template would create the least impact.

  9. petromir commented on Aug 24, 2021

    @petromir
    ContributorAuthor

    Something like

    private <T> Publisher<T> doInConnection(ReactiveRedisCallback<T> action, boolean exposeConnection) {
    
    	Assert.notNull(action, "Callback object must not be null");
    
    	return Flux.usingWhen(Mono.deferContextual(contextView -> getConnection(contextView, exposeConnection)), conn -> {
    		Publisher<T> result = action.doInRedis(conn);
    
    		return postProcessResult(result, conn, false);
    
     	}, ReactiveRedisConnection::closeLater);
    }
    
    protected Mono<ReactiveRedisConnection> getConnection(final ContextView contextView, boolean exposeConnection) {
    	ReactiveRedisConnectionFactory factory = getConnectionFactory();
    	ReactiveRedisConnection conn = factory.getReactiveConnection();
    	ReactiveRedisConnection connToUse = preProcessConnection(conn, false);
    
    	return Mono.just(exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));
    }
    
  10. mp911de commented on Aug 24, 2021

    @mp911de
    Member

    Mono.fromSupplier() is sufficient. One calling getConnection() that returns a Mono would expect deferred resource allocation. Otherwise, you already have a connection allocated and the only way to dispose it is by subscribing to the mono. Retaining fromSupplier(…) avoids such leaks in the first place.

  11. petromir commented on Aug 27, 2021

    @petromir
    ContributorAuthor

    Then how I will be able to pass the context to getConnection()?

  12. mp911de commented on Aug 27, 2021

    @mp911de
    Member

    Since the method returns a Mono already, you can override the method by calling Mono.deferContextual(…) in your override.

  13. petromir commented on Sep 8, 2021

    @petromir
    ContributorAuthor

    You mean

    private <T> Publisher<T> doInConnection(ReactiveRedisCallback<T> action, boolean exposeConnection) {
    
    	Assert.notNull(action, "Callback object must not be null");
    
    	return Flux.usingWhen(getConnection(exposeConnection), conn -> {
    		Publisher<T> result = action.doInRedis(conn);
    
    		return postProcessResult(result, conn, false);
    
     	}, ReactiveRedisConnection::closeLater);
    }
    
    protected Mono<ReactiveRedisConnection> getConnection(boolean exposeConnection) {
    	ReactiveRedisConnectionFactory factory = getConnectionFactory();
    	ReactiveRedisConnection conn = factory.getReactiveConnection();
    	ReactiveRedisConnection connToUse = preProcessConnection(conn, false);
    
    	return Mono.just(exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));
    }

    Then in my overriding class

    protected Mono<ReactiveRedisConnection> getConnection(boolean exposeConnection) {
    	return Mono.deferContextual(contextView -> {
    	    // Something that returns Mono<ReactiveRedisConnection> based on the value in the context
            }
    )
  14. mp911de commented on Sep 8, 2021

    @mp911de
    Member

    Yes, exactly.

  15. petromir commented on Sep 10, 2021

    @petromir
    ContributorAuthor

    @mp911de Please review my PR and let me know if I have to do some changes

  16. mp911de commented on Sep 13, 2021

    @mp911de
    Member

    Thanks, we're going to take this PR from here.

  17. added this to the 2.5.5 (2021.0.5) milestone on Sep 13, 2021
  18. added 2 commits that reference this issue on Sep 13, 2021
    7781c8f
    d37341e
  19. added a commit that references this issue on Sep 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions