Repository: redis/redis-py
File: redis/maintenance_events.py (Python)

Code before:
                reset_relax_timeout = self.config.is_relax_timeouts_enabled()
                reset_host_address = self.config.proactive_reconnect

                self.pool.update_connections_settings(
                    relax_timeout=-1,
                    state=MaintenanceState.NONE,
                    matching_address=moving_address,
                    address_type_to_match="configured",
                    reset_relax_timeout=reset_relax_timeout,
                    reset_host_address=reset_host_address,
                    include_free_connections=True,
                )


class MaintenanceEventConnectionHandler:
    def __init__(
        self, connection: "ConnectionInterface", config: MaintenanceEventsConfig
    ) -> None:
        self.connection = connection
        self.config = config

    def handle_event(self, event: MaintenanceEvent):
        if isinstance(event, NodeMigratingEvent):
            return self.handle_maintenance_start_event(MaintenanceState.MIGRATING)
        elif isinstance(event, NodeMigratedEvent):
            return self.handle_maintenance_completed_event()
        elif isinstance(event, NodeFailingOverEvent):
            return self.handle_maintenance_start_event(MaintenanceState.FAILING_OVER)
        elif isinstance(event, NodeFailedOverEvent):
            return self.handle_maintenance_completed_event()
        else:
            logging.error(f"Unhandled event type: {event}")

    def handle_maintenance_start_event(self, maintenance_state: MaintenanceState):
        if (
            self.connection.maintenance_state == MaintenanceState.MOVING
            or not self.config.is_relax_timeouts_enabled()
        ):
            return

        self.connection.maintenance_state = maintenance_state
        self.connection.set_tmp_settings(tmp_relax_timeout=self.config.relax_timeout)
        # extend the timeout for all created connections
        self.connection.update_current_socket_timeout(self.config.relax_timeout)

    def handle_maintenance_completed_event(self):
        # Only reset timeouts if state is not MOVING and relax timeouts are enabled
        if (
            self.connection.maintenance_state == MaintenanceState.MOVING
            or not self.config.is_relax_timeouts_enabled()
        ):

Review comment (bug):
What about creating a separate mapping like this: `type(event) : callback`? This way you don't need to scale this if..else if tree and it will be better performance wise as it will be O(1) instead of O(N) complexity.

```
mapping = {
   NodeMigratingEvent: self.handle_maintenance_start_event
   ...
   NodeFailedOverEvent: self.handle_maintenance_completed_event
}

handler = self.mapping.get(type(event), None)

if handler is None:
    logging.error(f"Unhandled event type: {event}")
    return

handler()
```

Code after:
                reset_host_address = self.config.proactive_reconnect

                self.pool.update_connections_settings(
                    relax_timeout=-1,
                    state=MaintenanceState.NONE,
                    matching_address=moving_address,
                    address_type_to_match="configured",
                    reset_relax_timeout=reset_relax_timeout,
                    reset_host_address=reset_host_address,
                    include_free_connections=True,
                )


class MaintenanceEventConnectionHandler:
    # 1 = "starting maintenance" events, 0 = "completed maintenance" events
    _EVENT_TYPES: dict[type["MaintenanceEvent"], int] = {
        NodeMigratingEvent: 1,
        NodeFailingOverEvent: 1,
        NodeMigratedEvent: 0,
        NodeFailedOverEvent: 0,
    }

    def __init__(
        self, connection: "ConnectionInterface", config: MaintenanceEventsConfig
    ) -> None:
        self.connection = connection
        self.config = config

    def handle_event(self, event: MaintenanceEvent):
        # get the event type by checking its class in the _EVENT_TYPES dict
        event_type = self._EVENT_TYPES.get(event.__class__, None)

        if event_type is None:
            logging.error(f"Unhandled event type: {event}")
            return

        if event_type:
            self.handle_maintenance_start_event(MaintenanceState.MAINTENANCE)
        else:
            self.handle_maintenance_completed_event()

    def handle_maintenance_start_event(self, maintenance_state: MaintenanceState):
        if (
            self.connection.maintenance_state == MaintenanceState.MOVING
            or not self.config.is_relax_timeouts_enabled()
        ):
            return

        self.connection.maintenance_state = maintenance_state
        self.connection.set_tmp_settings(tmp_relax_timeout=self.config.relax_timeout)
        # extend the timeout for all created connections