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 ping interval and timeout to player #304

Merged
merged 1 commit into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/poke_env/player/env_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def __init__(
server_configuration: Optional[ServerConfiguration] = None,
start_listening: bool = True,
start_timer_on_battle_start: bool = False,
ping_interval: Optional[float] = 20.0,
ping_timeout: Optional[float] = 20.0,
team: Optional[Union[str, Teambuilder]] = None,
start_challenging: bool = True,
):
Expand Down Expand Up @@ -65,6 +67,14 @@ def __init__(
:param start_timer_on_battle_start: Whether to automatically start the battle
timer on battle start. Defaults to False.
:type start_timer_on_battle_start: bool
:param ping_interval: How long between keepalive pings (Important for backend
websockets). If None, disables keepalive entirely.
:type ping_interval: float, optional
:param ping_timeout: How long to wait for a timeout of a specific ping
(important for backend websockets.
Increase only if timeouts occur during runtime).
If None pings will never time out.
:type ping_timeout: float, optional
:param team: The team to use for formats requiring a team. Can be a showdown
team string, a showdown packed team string, of a ShowdownTeam object.
Defaults to None.
Expand All @@ -91,6 +101,8 @@ def __init__(
start_listening=start_listening,
start_timer_on_battle_start=start_timer_on_battle_start,
team=team,
ping_interval=ping_interval,
ping_timeout=ping_timeout,
start_challenging=start_challenging,
)

Expand Down
12 changes: 12 additions & 0 deletions src/poke_env/player/openai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def __init__(
] = LocalhostServerConfiguration,
start_timer_on_battle_start: bool = False,
start_listening: bool = True,
ping_interval: Optional[float] = 20.0,
ping_timeout: Optional[float] = 20.0,
team: Optional[Union[str, Teambuilder]] = None,
start_challenging: bool = False,
):
Expand Down Expand Up @@ -142,6 +144,14 @@ def __init__(
:param start_timer_on_battle_start: Whether to automatically start the battle
timer on battle start. Defaults to False.
:type start_timer_on_battle_start: bool
:param ping_interval: How long between keepalive pings (Important for backend
websockets). If None, disables keepalive entirely.
:type ping_interval: float, optional
:param ping_timeout: How long to wait for a timeout of a specific ping
(important for backend websockets.
Increase only if timeouts occur during runtime).
If None pings will never time out.
:type ping_timeout: float, optional
:param team: The team to use for formats requiring a team. Can be a showdown
team string, a showdown packed team string, of a ShowdownTeam object.
Defaults to None.
Expand All @@ -162,6 +172,8 @@ def __init__(
server_configuration=server_configuration,
start_timer_on_battle_start=start_timer_on_battle_start,
start_listening=start_listening,
ping_interval=ping_interval,
ping_timeout=ping_timeout,
team=team,
)
self.battle_format = battle_format
Expand Down
12 changes: 12 additions & 0 deletions src/poke_env/player/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def __init__(
server_configuration: Optional[ServerConfiguration] = None,
start_timer_on_battle_start: bool = False,
start_listening: bool = True,
ping_interval: Optional[float] = 20.0,
ping_timeout: Optional[float] = 20.0,
team: Optional[Union[str, Teambuilder]] = None,
) -> None:
"""
Expand Down Expand Up @@ -91,6 +93,14 @@ def __init__(
:param start_listening: Whether to start listening to the server. Defaults to
True.
:type start_listening: bool
:param ping_interval: How long between keepalive pings (Important for backend
websockets). If None, disables keepalive entirely.
:type ping_interval: float, optional
:param ping_timeout: How long to wait for a timeout of a specific ping
(important for backend websockets.
Increase only if timeouts occur during runtime).
If None pings will never time out.
:type ping_timeout: float, optional
:param start_timer_on_battle_start: Whether to automatically start the battle
timer on battle start. Defaults to False.
:type start_timer_on_battle_start: bool
Expand All @@ -111,6 +121,8 @@ def __init__(
log_level=log_level,
server_configuration=server_configuration,
start_listening=start_listening,
ping_interval=ping_interval,
ping_timeout=ping_timeout,
)

self._format: str = battle_format
Expand Down
19 changes: 17 additions & 2 deletions src/poke_env/player/player_network_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def __init__(
log_level: Optional[int] = None,
server_configuration: ServerConfiguration,
start_listening: bool = True,
ping_interval: Optional[float] = 20.0,
ping_timeout: Optional[float] = 20.0,
) -> None:
"""
:param player_configuration: Player configuration.
Expand All @@ -52,10 +54,20 @@ def __init__(
:type log_level: int. Defaults to logging's default level.
:param server_configuration: Server configuration.
:type server_configuration: ServerConfiguration
:param start_listening: Wheter to start listening to the server. Defaults to
:param start_listening: Whether to start listening to the server. Defaults to
True.
:type start_listening: bool
:param ping_interval: How long between keepalive pings (Important for backend
websockets). If None, disables keepalive entirely.
:type ping_interval: float, optional
:param ping_timeout: How long to wait for a timeout of a specific ping
(important for backend websockets.
Increase only if timeouts occur during runtime).
If None pings will never time out.
:type ping_timeout: float, optional
"""
self._ping_interval = ping_interval
self._ping_timeout = ping_timeout
self._authentication_url = server_configuration.authentication_url
self._avatar = avatar
self._password = player_configuration.password
Expand Down Expand Up @@ -280,7 +292,10 @@ async def listen(self) -> None:
self.logger.info("Starting listening to showdown websocket")
try:
async with websockets.connect(
self.websocket_url, max_queue=None
self.websocket_url,
max_queue=None,
ping_interval=self._ping_interval,
ping_timeout=self._ping_timeout,
) as websocket:
self._websocket = websocket
async for message in websocket:
Expand Down