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

feat: Implement Port Manager for Handling Port Expiration and State Management #21

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

faviann
Copy link

@faviann faviann commented Nov 10, 2024

This pull request introduces a Port Manager to handle port expiration and maintain state between runs. The key changes include:

  • Implemented a port lifetime manager to avoid renewing ports unnecessarily by maintaining state between runs.
  • Added functions to calculate expiration time, check if a port is expired, and serialize/deserialize port data to/from a .env file.
  • Integrated the PortManager into the main function to handle port expiration and external monitoring, with early exit if the port is still valid.
  • Enhanced logging for port events, including expiration times, reservation, and reallocation.

This is my first time coding in Python so I might have goofed in some places but I'm running right now and it seems to work well for me.

Please note that this implementation was created to meet my specific needs and may not align with the direction you're looking for. If this approach is not suitable, feel free to close the pull request. Note that I am open to feedback and willing to make any changes to better fit the direction. I thought I'd still push a PR in case you'd want part of that code

This is to have some form of stateful-ness in between runs and avoid renewing ports even if reset. It keeps as state its port, timestamp of the port, and the "random" expiration date assigned

Notes:
- At the moment it chooses a random minute on the last 24 hours as its expiration date (to avoid potential perceived bot issues from Windscribe)
- It can serialize/deserialize it's data in the form of a docker .env file. I will be using/monitor the file from outside docker to restart the stack. That env file will also populate gluetun and make it "compatible" until dynamic forwarded port setting is available through an API.
…monitoring

Couple of important changes:

1) If the port previously request is not yet expired, main just early outs
2) Because of the "early out" main runs every 5 minutes anyways (costs nothing)
3) If it cannot deserialize the port file for some reason, it's gonna be treated as the port being expired (and hence renewed)
4) The requested port is serialized in the "WS_COOKIE_PATH/port.env". This will allow from the outside to monitor when the value changed and only required to restart the stack to 'update' the port.
5) I wanted to prevent the failed heartbeat for QBittorrent to derail the port forwarding mechanism so I moved it further down. Now port requests will ALWAYS be done but the updating of QBittorrent might be skipped
@dhruvinsh
Copy link
Owner

thank you for the pull request, I will have look at it this weekend.

@faviann
Copy link
Author

faviann commented Nov 14, 2024

As a note, it's really a first draft version. While it works I think my PR needs additional work but I'll let you come back with feedback first

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.

2 participants