Skip to content

Commit

Permalink
Default to PersistentSender, fixes #141
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-hilden committed May 7, 2020
1 parent 1826ddf commit e694b4a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
19 changes: 7 additions & 12 deletions docs/src/advanced_usage.rst
Expand Up @@ -147,15 +147,11 @@ Using senders
By default Tekore doesn't do anything clever when sending requests.
Its functionality, however, can be extended in a number of ways
using different kinds of :ref:`senders <senders>`.
They can be used for connection persistence, retrying and caching.
They can be used for e.g. retrying and caching.
User-defined sessions and additional keyword arguments
to :func:`requests.Session.send` can also be passed in.
For example, per-instance sessions can be enabled with
a :class:`PersistentSender`.

.. code:: python
tk.Spotify(sender=tk.PersistentSender())
For example, connection persistence and per-instance sessions are enabled
by default thanks to :class:`PersistentSender`.

Keepalive connections, retries and caching make up a performance-boosting
and convenient sender setup, easily constructed from simple building blocks.
Expand All @@ -164,14 +160,13 @@ busy applications that request the same static resources repeatedly.

.. code:: python
tk.default_sender_instance = tk.CachingSender(
sender = tk.CachingSender(
max_size=256,
sender=tk.RetryingSender(
retries=2,
sender=tk.PersistentSender()
)
sender=tk.RetryingSender(retries=2)
)
tk.Spotify(sender=sender)
For more detailed information, see :ref:`performance`.

Traversing paging objects
Expand Down
2 changes: 1 addition & 1 deletion docs/src/examples/async_server.rst
Expand Up @@ -22,7 +22,7 @@ requests take about ten seconds, no matter how many are sent at once.
conf = tk.config_from_environment()
token = tk.request_client_token(*conf[:2])
spotify = tk.Spotify(token, sender=tk.AsyncPersistentSender())
spotify = tk.Spotify(token, asynchronous=True)
routes = web.RouteTableDef()
Expand Down
3 changes: 3 additions & 0 deletions docs/src/release_notes.rst
Expand Up @@ -63,6 +63,9 @@ Miscellaneous
:ref:`errors` now inherit from a common base class. (:issue:`154`)
- :attr:`Token.scope` and :class:`RefreshingToken.scope` now return
a :class:`Scope` instead of a string. (:issue:`177`)
- Default :ref:`sender <senders>` changed from :class:`TransientSender` to
:class:`PersistentSender`, also affects :class:`Client` behavior
(:issue:`141`)

1.7.0 (2020-04-28)
------------------
Expand Down
2 changes: 1 addition & 1 deletion tekore/__init__.py
Expand Up @@ -131,7 +131,7 @@
Not used when any other keyword arguments are passed in.
"""

default_sender_type: _Union[_Type[SyncSender], _Type[AsyncSender]] = TransientSender
default_sender_type: _Union[_Type[SyncSender], _Type[AsyncSender]] = PersistentSender
"""
Sender to instantiate by default.
"""
Expand Down
8 changes: 4 additions & 4 deletions tekore/_sender/client.py
Expand Up @@ -2,7 +2,7 @@
from warnings import warn
from requests import Request, Response

from .concrete import Sender, TransientSender, AsyncTransientSender
from .concrete import Sender, PersistentSender, AsyncPersistentSender


def new_default_sender() -> Sender:
Expand All @@ -28,15 +28,15 @@ class Client:
asynchronous
synchronicity requirement - If specified, overrides passed
sender and defaults if they are in conflict and instantiates
a transient sender of the requested type
a persistent sender of the requested type
"""
def __init__(self, sender: Optional[Sender], asynchronous: bool = None):
new_sender = sender or new_default_sender()

if new_sender.is_async and asynchronous is False:
new_sender = TransientSender()
new_sender = PersistentSender()
elif not new_sender.is_async and asynchronous is True:
new_sender = AsyncTransientSender()
new_sender = AsyncPersistentSender()

self.sender = new_sender

Expand Down

0 comments on commit e694b4a

Please sign in to comment.