Selkies
Developer Referencewebrtc_utils

RESTRTCMonitor

Periodically re-fetches TURN credentials from a TURN REST API.

Fetches every period seconds and delivers the parsed config through the on_rtc_config callback, which the consumer must assign before start(). Request parameters (protocol, TLS, username) travel in configurable HTTP headers so custom REST endpoints can be matched without code changes.

Attributes

attributeperiod
= period
attributeenabled
= enabled
attributestop_event
= asyncio.Event()
attributeturn_rest_uri
= turn_rest_uri
attributeturn_rest_username
= turn_rest_username.replace(':', '-')
attributeturn_rest_username_auth_header
= turn_rest_username_auth_header
attributeturn_protocol
= turn_protocol
attributeturn_rest_protocol_header
= turn_rest_protocol_header
attributeturn_tls
= turn_tls
attributeturn_rest_tls_header
= turn_rest_tls_header
attributeturn_api_key
= turn_api_key if turn_api_key else None
attributeon_rtc_configCallable[[List[str], List[str], bytes], Any]
= lambda stun_servers, turn_servers, rtc_config: logger_rtcice.warning('unhandled on_rtc_config')

Functions

func__init__(self, turn_rest_uri, turn_rest_username, turn_rest_username_auth_header, turn_protocol='udp', turn_rest_protocol_header='x-turn-protocol', turn_tls=False, turn_rest_tls_header='x-turn-tls', turn_api_key=None, period=60, enabled=True)
Source Code
def __init__(
    self,
    turn_rest_uri: str,
    turn_rest_username: str,
    turn_rest_username_auth_header: str,
    turn_protocol: str = 'udp',
    turn_rest_protocol_header: str = 'x-turn-protocol',
    turn_tls: bool = False,
    turn_rest_tls_header: str = 'x-turn-tls',
    turn_api_key: Optional[str] = None,
    period: int = 60,
    enabled: bool = True
):
    self.period = period
    self.enabled = enabled
    self.stop_event = asyncio.Event()
    self._task: Optional[asyncio.Task] = None
    self.turn_rest_uri = turn_rest_uri
    self.turn_rest_username = turn_rest_username.replace(":", "-")
    self.turn_rest_username_auth_header = turn_rest_username_auth_header
    self.turn_protocol = turn_protocol
    self.turn_rest_protocol_header = turn_rest_protocol_header
    self.turn_tls = turn_tls
    self.turn_rest_tls_header = turn_rest_tls_header
    self.turn_api_key = turn_api_key if turn_api_key else None
    self.on_rtc_config: Callable[[List[str], List[str], bytes], Any] = lambda stun_servers, turn_servers, rtc_config: logger_rtcice.warning("unhandled on_rtc_config")
paramself
paramturn_rest_uristr
paramturn_rest_usernamestr
paramturn_rest_username_auth_headerstr
paramturn_protocolstr
= 'udp'
paramturn_rest_protocol_headerstr
= 'x-turn-protocol'
paramturn_tlsbool
= False
paramturn_rest_tls_headerstr
= 'x-turn-tls'
paramturn_api_keyOptional[str]
= None
paramperiodint
= 60
paramenabledbool
= True

Returns

None
funcstart(self) -> None

Starts the periodic refresh task; no-op when disabled.

Source Code
def start(self) -> None:
    """Starts the periodic refresh task; no-op when disabled."""
    if not self.enabled:
        return
    self.stop_event.clear()
    self._task = asyncio.create_task(self._monitor_loop())
    logger_rtcice.info("TURN REST RTC monitor started")
paramself

Returns

None
func_monitor_loop(self) -> None

Fetches and dispatches REST configs until stopped.

Per-iteration failures are logged and retried on the next period rather than killing the monitor.

Source Code
async def _monitor_loop(self) -> None:
    """Fetches and dispatches REST configs until stopped.

    Per-iteration failures are logged and retried on the next period
    rather than killing the monitor.
    """
    try:
        while not self.stop_event.is_set():
            try:
                stun_servers, turn_servers, rtc_config = await fetch_turn_rest(
                    self.turn_rest_uri,
                    self.turn_rest_username,
                    self.turn_rest_username_auth_header,
                    self.turn_protocol,
                    self.turn_rest_protocol_header,
                    self.turn_tls,
                    self.turn_rest_tls_header,
                    self.turn_api_key
                )
                await _dispatch_rtc_callback(self.on_rtc_config, stun_servers, turn_servers, rtc_config)
            except Exception as e:
                logger_rtcice.warning(f"could not fetch TURN REST config in periodic monitor: {e}")

            try:
                await asyncio.wait_for(self.stop_event.wait(), timeout=self.period)
            except asyncio.TimeoutError:
                pass
    except asyncio.CancelledError:
        pass
    except Exception as e:
        logger_rtcice.error(f"Error in TURN REST RTC monitor: {e}")
    finally:
        logger_rtcice.info("TURN REST RTC monitor stopped")
paramself

Returns

None
funcstop(self) -> None

Signals the loop to exit and waits for the task to finish.

Source Code
async def stop(self) -> None:
    """Signals the loop to exit and waits for the task to finish."""
    self.stop_event.set()
    if self._task:
        await self._task
paramself

Returns

None

On this page

Edit on GitHub