Skip to content

Handle no priority set case. Take CP config refresh delay from config#14

Open
prshant70 wants to merge 1 commit intomainfrom
13-handle-no-gateway-priority-case
Open

Handle no priority set case. Take CP config refresh delay from config#14
prshant70 wants to merge 1 commit intomainfrom
13-handle-no-gateway-priority-case

Conversation

@prshant70
Copy link
Contributor

@prshant70 prshant70 commented Dec 24, 2024


Size S: This PR changes include 25 lines and should take approximately 15-30 minutes to review


DeputyDev generated PR summary:

The pull request (PR) introduces several changes aimed at improving the configuration handling and gateway selection logic for channel partners in the system. Here's a breakdown of what the PR does:

  1. Configuration Synchronization Delay:

    • The _periodic_sync_delay in the ChannelPartners class is now dynamically set using a value from a configuration file (CONFIG.config["CHANNEL_PARTNERS"]["CONFIG_SYNC_DELAY"]) instead of a hardcoded value. This allows for easier adjustments of the sync delay without modifying the code.
  2. New Method for Configured Gateways:

    • A new method get_configured_gateways is added to the abstract_handler.py and gateway_priority.py files. This method returns a list of configured gateways and is intended to be overridden by subclasses. This change enhances the flexibility and extensibility of the handler classes.
  3. Gateway Selection Logic:

    • The select_gateway method in gateway_priority.py has been updated to handle cases where no default priority order is available. If total_gateways is zero, it now falls back to using all configured gateways to select from, ensuring that the system can still function even if priority configurations are missing.
  4. Configuration Template Update:

    • The config_template.json file is updated to include a new configuration section for CHANNEL_PARTNERS, which includes CONFIG_SYNC_DELAY.

These changes improve the configurability and robustness of the system by allowing dynamic configuration of sync delays and enhancing gateway selection logic to handle edge cases more gracefully.

Here's the corrective code in markdown format:

# In get_configurations.py
_periodic_sync_delay = CONFIG.config["CHANNEL_PARTNERS"]["CONFIG_SYNC_DELAY"]

# In abstract_handler.py
@classmethod
def get_configured_gateways(cls) -> list:
    return list(cls.PROVIDERS.keys())

# In gateway_priority.py
@classmethod
@abstractmethod
def get_configured_gateways(cls) -> list:
    pass

@classmethod
def select_gateway(cls, n_attempts: int, request_data: dict) -> str:
    if total_gateways > 0:
        return current_priority[n_attempts % total_gateways]
    else:
        all_gateways = cls.get_configured_gateways()
        return all_gateways[n_attempts % len(all_gateways]

# In config_template.json
"CHANNEL_PARTNERS": {
    "CONFIG_SYNC_DELAY": 5
}

@prshant70 prshant70 linked an issue Dec 24, 2024 that may be closed by this pull request
@deputydev-agent
Copy link

DeputyDev has started reviewing your pull request.

class ChannelPartners:

_periodic_sync_delay = 5*60
_periodic_sync_delay = CONFIG.config["CHANNEL_PARTNERS"]["CONFIG_SYNC_DELAY"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PERFORMANCE: The _periodic_sync_delay value is now being read from the configuration file, which is a good practice. However, there's no validation or error handling for this value. If the configuration is missing or invalid, it could lead to unexpected behavior. Consider adding a default value and error handling to ensure the system behaves predictably even if the configuration is missing or invalid.

class ChannelPartners:
    _periodic_sync_delay = CONFIG.config.get("CHANNEL_PARTNERS", {}).get("CONFIG_SYNC_DELAY", 300)  # Default to 5 minutes
    PROVIDERS_CONFIG = dict()

    @classmethod
    def __init__(cls):
        if cls._periodic_sync_delay <= 0:
            logger.warning("Invalid CONFIG_SYNC_DELAY, using default of 300 seconds")
            cls._periodic_sync_delay = 300

@@ -13,7 +15,7 @@

class ChannelPartners:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RUNTIME_ERROR: The new configuration parameter CONFIG_SYNC_DELAY is introduced, but there's no error handling if this configuration is missing. This could lead to a runtime error if the configuration is not properly set.

# Add a default value and error handling
_periodic_sync_delay = CONFIG.config.get("CHANNEL_PARTNERS", {}).get("CONFIG_SYNC_DELAY", 300)  # Default to 5 minutes

return cls._HANDLER_CONFIG["dynamic_priority"]

@classmethod
def get_configured_gateways(cls) -> list:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CODE_ROBUSTNESS: The addition of the get_configured_gateways method to the AbstractHandler class enhances code robustness by providing a fallback mechanism when no gateways are available in the priority list.

pass
total_gateways = min(len(cls.get_default_priority()), len(current_priority))
return current_priority[n_attempts % total_gateways]
if total_gateways > 0:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CODE_ROBUSTNESS: The code now handles the case when no priority is set or when the total_gateways is 0, improving the robustness of the gateway selection process.

@deputydev-agent
Copy link

DeputyDev has completed a review of your pull request for commit 105345c.

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.

Handle no gateway priority case

2 participants