The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Beekeeper::Worker::Extension::RemoteSession - Remote client session handling

VERSION

Version 0.09

SYNOPSIS

  use Beekeeper::Worker::Extension::RemoteSession;
  
  $self->bind_remote_session( address => "frontend.user-123" );
  
  $self->send_notification(
      method  => 'myapp.private_message',
      address => 'frontend.user-123',
      params  => 'hello',
  );
  
  $self->unbind_remote_session;
  
  $self->unbind_remote_address( address => "frontend.user-123" );
  
  $self->bind_remote_session_async(
      address    => "frontend.user-123";
      on_success => sub {
          log_info "Address assigned";
      },
      on_error => sub {
          my ($error) = @_;
          log_error $error->message;
      },
  );

DESCRIPTION

This extension allows to assign authorization data to remote client sessions and give arbitrary addresses to them. These addresses can be used later to push unicasted notifications to clients.

Router workers pull requests from all frontend brokers and forward them to the single backend broker it is connected to, and pull generated responses from the backend and forward them to the aproppiate frontend broker which the client is connected to.

Additionally, routers include some primitives that can be used to implement session management and push notifications. In order to push unicasted notifications, routers will keep an in-memory shared table of client connections and server side assigned addresses. Each entry consumes 1.5 KiB of memory, so a table of 100K sessions will consume around 150 MiB for each Router worker.

If the application does not bind client sessions the routers can scale horizontally really well, as you can have thousands of them connected to hundreds of brokers.

But please note that, when the application does use the session binding mechanism, all routers will need the in-memory shared table, and this shared table will not scale to a great extent as the rest of the system. The limiting factor is the global rate of updates to the table, which will cap around 5000 bind operations (logins) per second. This might be fixed on future releases by means of partitioning the table. Meanwhile, this session binding mechanism is not suitable for applications with a large number of concurrent clients.

Router workers are not created automatically. In order to add Router workers to a pool these must be declared into config file pool.config.json.

METHODS

bind_remote_session ( address => $address )

Make authorization data persist for remote caller session and optionally assign an arbitrary address to the remote client.

The authorization data can be used to store a session ID or other tokens which identify requests as coming from a particular remote client.

If an address is provided it can be used to push notifications to the client. The same address can be assigned to multiple remote clients, and all of them will receive the notifications sent to it. This is intended to allow to push notifications to users logged into multiple devices.

unbind_remote_session

Clear the authorization data and address assignment of a single remote caller session.

This does not affect other remote clients which share the same address. This is intended to implement "logout from this device" functionality.

unbind_remote_address ( address => $address )

Clear the authorization data and address assignment of all remote clients which were assigned the given address.

This is intended to implement "logout from all devices" functionality.

bind_remote_session_async ( address => $address, on_success => $cb, on_error => $cb )

Asynchronous version of bind_remote_session method.

Callbacks on_success and on_error must be coderefs and will receive respectively Beekeeper::JSONRPC::Response and Beekeeper::JSONRPC::Error objects as arguments.

unbind_remote_session_async ( on_success => $cb, on_error => $cb )

Asynchronous version of unbind_remote_session method.

unbind_remote_address_async ( address => $address, on_success => $cb, on_error => $cb )

Asynchronous version of unbind_remote_address method.

SEE ALSO

Beekeeper::Service::Router::Worker

AUTHOR

José Micó, jose.mico@gmail.com

COPYRIGHT AND LICENSE

Copyright 2015-2023 José Micó.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language itself.

This software is distributed in the hope that it will be useful, but it is provided “as is” and without any express or implied warranties. For details, see the full text of the license in the file LICENSE.