Skip to content

Commit

Permalink
Support overriding class for get_rpc_* helper functions
Browse files Browse the repository at this point in the history
We currently do not support overriding the class being
instantiated in the RPC helper functions, this adds that
support so that projects that define their own classes
that inherit from oslo.messaging can use the helpers.

For example neutron utilizes code from neutron-lib that
has it's own RPCClient implementation that inherits from
oslo.messaging, in order for them to use for example
the get_rpc_client helper they need support to override
the class being returned. The alternative would be to
modify the internal _manual_load variable which seems
counter-productive to extending the API provided to
consumers.

Change-Id: Ie22f2ee47a4ca3f28a71272ee1ffdb88aaeb7758
  • Loading branch information
tobias-urdin committed Jan 23, 2023
1 parent 7505316 commit 687dea2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
8 changes: 5 additions & 3 deletions oslo_messaging/rpc/client.py
Expand Up @@ -550,14 +550,16 @@ def can_send_version(self, version=_marker):
return self.prepare(version=version).can_send_version()


def get_rpc_client(transport, target, **kwargs):
def get_rpc_client(transport, target, client_cls=RPCClient, **kwargs):
"""Construct an RPC client.
:param transport: the messaging transport
:type transport: Transport
:param target: the exchange, topic and server to listen on
:type target: Target
:param client_cls: The client class to instantiate
:type client_cls: class
:param **kwargs: The kwargs will be passed down to the
RPCClient constructor
client_cls constructor
"""
return RPCClient(transport, target, _manual_load=False, **kwargs)
return client_cls(transport, target, _manual_load=False, **kwargs)
7 changes: 5 additions & 2 deletions oslo_messaging/rpc/server.py
Expand Up @@ -200,7 +200,8 @@ def _process_incoming(self, incoming):


def get_rpc_server(transport, target, endpoints,
executor=None, serializer=None, access_policy=None):
executor=None, serializer=None, access_policy=None,
server_cls=RPCServer):
"""Construct an RPC server.
:param transport: the messaging transport
Expand All @@ -217,10 +218,12 @@ def get_rpc_server(transport, target, endpoints,
:param access_policy: an optional access policy.
Defaults to DefaultRPCAccessPolicy
:type access_policy: RPCAccessPolicyBase
:param server_cls: The server class to instantiate
:type server_cls: class
"""
dispatcher = rpc_dispatcher.RPCDispatcher(endpoints, serializer,
access_policy)
return RPCServer(transport, target, dispatcher, executor)
return server_cls(transport, target, dispatcher, executor)


def expected_exceptions(*exceptions):
Expand Down
7 changes: 5 additions & 2 deletions oslo_messaging/rpc/transport.py
Expand Up @@ -22,7 +22,8 @@


def get_rpc_transport(conf, url=None,
allowed_remote_exmods=None):
allowed_remote_exmods=None,
transport_cls=msg_transport.RPCTransport):
"""A factory method for Transport objects for RPCs.
This method should be used to ensure the correct messaging functionality
Expand All @@ -43,7 +44,9 @@ def get_rpc_transport(conf, url=None,
transport will deserialize remote exceptions
from
:type allowed_remote_exmods: list
:param transport_cls: the transport class to instantiate
:type transport_cls: class
"""
return msg_transport._get_transport(
conf, url, allowed_remote_exmods,
transport_cls=msg_transport.RPCTransport)
transport_cls=transport_cls)
5 changes: 5 additions & 0 deletions releasenotes/notes/get-rpc-helpers-cls-8911826ac08aef2a.yaml
@@ -0,0 +1,5 @@
---
features:
- |
The ``get_rpc_transport``, ``get_rpc_server`` and ``get_rpc_client`` helper
functions now have support for overriding the class that is instantiated.

0 comments on commit 687dea2

Please sign in to comment.