Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for "Session resuming" & "session data queueing". #59

Closed
wants to merge 62 commits into from
Closed

Conversation

WizzyGeek
Copy link
Contributor

@WizzyGeek WizzyGeek commented Jun 22, 2020

This PR is Implemented in #66

Added New attributes

  • resume_session: bool
  • resume_timeout: float
  • resume_key: str (Best left None)
  • payload_timeout: float This is basically how recent requests shall be sent to the server
  • Websocket.reset() method.

What are the new features?

Websocket queueing.

  • Websocket._send() method now queues any data that is requested to be sent while the WS is disconnected.
  • New method Websocket.send_queue() which is called when connecting, and sends queue only when a session is resumed and Websocket is connected.
  • New exception NodeSessionClosedError raised when node closes a session or doesn't resume the old session. Handled within _connect method.

Resuming

  • example of use of new features
import string
import secret
class key:
    def __init__(self, len, first_key="NoSnoopingOnMyLava"):
        self.len = len
        self.persistent = first_key

    def __str__(self):
        return self.persistent

    def __repr__(self):
        """This should generate a key and shall make it persistent """
        self.persistent = SomeSecurePasswordGenerator(self.len)
        return self.persistent

---------------

 async def start_nodes(self) -> None:
    """Connect and intiate nodes."""
    nodes = {'MAIN': {'host': 'lavaserver.badhost.com',
                      'port': 80,
                      'rest_uri': 'http://lavaserver.badhost.com',
                      'password': "verytrivialpassword",
                      'identifier': 'MAIN',
                      'region': 'us_central',
                      'heartbeat': 40.0, # ping the server every 40s
                      'resume_session': True,
                      'resume_timeout': 90.0,
                      'resume_key': key(10) # or "Astring"
                      'payload_timeout': 40.0
                      }}

    for n in nodes.values():
        await self.bot.wavelink.initiate_node(**n)
  • or simply by setting 'resume_session': True
 async def start_nodes(self) -> None:
    """Connect and intiate nodes."""
    nodes = {'MAIN': {'host': 'lavaserver.badhost.com',
                      'port': 80,
                      'rest_uri': 'http://lavaserver.badhost.com',
                      'password': "verytrivialpassword",
                      'identifier': 'MAIN',
                      'region': 'us_central',
                      'resume_session': True
                      }}

    for n in nodes.values():
        await self.bot.wavelink.initiate_node(**n)

Though the former has more use cases by using a Key Object we can:

  • Make the key persistent across Clusters
  • log the events, since __str__ is called when configuring the server, while __repr__ is called when the Node's session is closed to generate a new key.
  • To Generate a more secure password
  • Increase length of the password (The default is 32 characters)
  • Use

How does resuming work?

When the bot disconnects from the node (eg: 1006) then the lavalink server keeps on playing the music until session timeout, this allows the bot to reconnect and take control of the session. This PR implements this lavalink feature.
The changes are not breaking, assuming no-one initializes Node instances directly.

How does Queueing work?

After the Node disconnects, we try to reconnect twice before timeout. during this time the requests to the node are queued in an asyncio.Queue subclass whose payload expires after some time.
This could be useful when you have an higher timeout but want to only send recent requests

- Added Queue which is only sent if a session is restored or has a chance that it might be restored
- Modified backoff to try connecting twice to lavalink server before session is closed
- Resume key is generated with secret.choice and defaults to 32 character, if No resume key is passed
- Changes are not breaking since all added args are optional
@WizzyGeek
Copy link
Contributor Author

The capture of logs of testing

Capture

@WizzyGeek
Copy link
Contributor Author

Version number not changed yet.

@WizzyGeek
Copy link
Contributor Author

Could you review this?

@WizzyGeek
Copy link
Contributor Author

WizzyGeek commented Jun 24, 2020

I have switched to asyncio.Queue(ish), could you re-review?

@WizzyGeek
Copy link
Contributor Author

WizzyGeek commented Jun 25, 2020

_TimedQueue Test (Edited due to recent commit)

from wavelink import _TimedQueue # Modified __init__
import time
import asyncio

if __name__ == '__main__':
    a = _TimedQueue(0, timeout=6.0)
    a.put_nowait("aight")
    time.sleep(7.0)
    print(a.get_nowait()) # None
    a.put_nowait("hmmm")
    a.clear()
    try:
        a.get_nowait()
    except asyncio.QueueEmpty:
        print("Excepted") # Queue should be cleared.
    a.put_nowait("This should be printed.")
    time.sleep(3.0)
    print(a.get_nowait())

Output

None
Excepted
This should be printed.

@WizzyGeek WizzyGeek marked this pull request as draft June 30, 2020 08:46
@WizzyGeek WizzyGeek changed the title Add support for "Session resuming", "session data queueing" and Cleanup. Add support for "Session resuming" and "session data queueing". Jun 30, 2020
@WizzyGeek WizzyGeek marked this pull request as ready for review June 30, 2020 08:50
@WizzyGeek WizzyGeek changed the title Add support for "Session resuming" and "session data queueing". [Req-to-Review] Add support for "Session resuming" & "session data queueing". Aug 3, 2020
@WizzyGeek WizzyGeek changed the title [Req-to-Review] Add support for "Session resuming" & "session data queueing". Add support for "Session resuming" & "session data queueing". Aug 13, 2020
@WizzyGeek WizzyGeek closed this Aug 13, 2020
WizzyGeek added a commit to WizzyGeek/Wavelink that referenced this pull request Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant