Skip to content

How do RSocket issue lease to multiple clients? #966

Closed

Description

@kevinten10

I could create a server lease to a single client as follows:

    @Slf4j
    public class LeaseServer {
    
        private static final String SERVER_TAG = "server";
    
        public static void main(String[] args) throws InterruptedException {
            // Queue for incoming messages represented as Flux
            // Imagine that every fireAndForget that is pushed is processed by a worker
            int queueCapacity = 50;
            BlockingQueue<String> messagesQueue = new ArrayBlockingQueue<>(queueCapacity);
            // emulating a worker that process data from the queue
            Thread workerThread =
                    new Thread(
                            () -> {
                                try {
                                    while (!Thread.currentThread().isInterrupted()) {
                                        String message = messagesQueue.take();
                                        System.out.println("consume message:" + message);
                                        Thread.sleep(100000); // emulating processing
                                    }
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            });
            workerThread.start();
            CloseableChannel server = getFireAndForgetServer(messagesQueue, workerThread);
            TimeUnit.MINUTES.sleep(10);
            server.dispose();
        }
    
        private static CloseableChannel getFireAndForgetServer(BlockingQueue<String> messagesQueue, Thread workerThread) {
            CloseableChannel server =
                    RSocketServer.create((setup, sendingSocket) ->
                            Mono.just(new RSocket() {
                                @Override
                                public Mono<Void> fireAndForget(Payload payload) {
                                    // add element. if overflows errors and terminates execution
                                    // specifically to show that lease can limit rate of fnf requests in
                                    // that example
                                    try {
                                        if (!messagesQueue.offer(payload.getDataUtf8())) {
                                            System.out.println("Queue has been overflowed. Terminating execution");
                                            sendingSocket.dispose();
                                            workerThread.interrupt();
                                        }
                                    } finally {
                                        payload.release();
                                    }
                                    return Mono.empty();
                                }
                            }))
                            .lease(() -> Leases.create().sender(new LeaseCalculator(SERVER_TAG, messagesQueue)))
                            .bindNow(TcpServerTransport.create("localhost", 7000));
            return server;
        }
    }

But how do I issue a lease to multiple clients connected to that server?

Otherwise my queue will be written multiple times by multiple clients, resulting in an overflow of the service.

I can't find the details in the public documents and materials.

Your help was very much appreciated.

And I ask for help on stackoverflow: https://stackoverflow.com/questions/65227829/how-do-rsocket-issue-lease-to-multiple-clients

Activity

  1. OlegDokuka commented on Dec 19, 2020

    @OlegDokuka
    Member

    Hi @kevinten10!

    Thank you for asking!
    Great news for you! We are working hard to improve the current lease API and provide you with a straight forward way of lease issuing to multiple Requesters.

    Please track the progress here #885! Feel free to use a snapshot from that repo and try the API out! Any feedback is more than appreciated. Also, you may find a set of examples on how to control multiple requesters here -> https://github.com/rsocket/rsocket-java/pull/885/files#diff-05334cc64278a568fba3834c8d5798b66ed451a337dd96c6ed2f54659b8e35c8

    Stay tuned,
    Oleh

  2. kevinten10 commented on Dec 20, 2020

    @kevinten10
    Author

    @OlegDokuka Thanks for your answer!

    I read this commit #885 .

    Looking forward to a more complete lease mechanism.

    This will enable lease to be truly applied in a distributed environment.

    I will further explore the use of lease.

    Best regrads.

  3. kevinten10 commented on Dec 20, 2020

    @kevinten10
    Author

    @OlegDokuka By the way

    Whether to consider providing default implementation of common indicators for lease, such as queue capacity, cpu indicators, etc.

    It can avoid repeated development.

  4. OlegDokuka commented on Dec 20, 2020

    @OlegDokuka
    Member

    @kevinten10 I will consider that, however, the actual advanced implementation is out of library scope. But feel free to build out something and if it is not that complex, we can definitely consider porting it into the core library.

    Also, most likely we will provide a standard mechanism that should simplify significantly stats based lease management implementation

    Cheers,
    Oleh

  5. kitkars commented on Dec 29, 2020

    @kitkars

    @OlegDokuka @rstoyanchev

    Just curious - currently lease supplier does not seem to know any knowledge on the client side/requester. Is this expected?
    For ex: It certain cases it could be useful - to adjust the lease based on the client.

    Thanks.

  6. OlegDokuka commented on Feb 17, 2021

    @OlegDokuka
    Member

    @kitkars sorry for the late answer. This is up to the application logic. Your clients may provide different information at the setup phase and then you may provide this info to the lease manager so it can decide how to treat a particular connection. Lease is just a mechanism to manage responder load, but the actual algorithm is on the application implementers shoulders

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

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions