Core cleanup: two stage shutdown (#5876)
* Core cleanup: two stage shutdown * fix spell * fix * add async logger to close * change aiohttp to use CLOSE * address paulus comments * Fix tests * Add unittest
This commit is contained in:
parent
4623d1071e
commit
41849eab06
8 changed files with 97 additions and 107 deletions
|
@ -9,7 +9,7 @@ from aiohttp.web_exceptions import HTTPGatewayTimeout
|
|||
import async_timeout
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE
|
||||
from homeassistant.const import __version__
|
||||
|
||||
DATA_CONNECTOR = 'aiohttp_connector'
|
||||
|
@ -38,6 +38,7 @@ def async_get_clientsession(hass, verify_ssl=True):
|
|||
connector=connector,
|
||||
headers={USER_AGENT: SERVER_SOFTWARE}
|
||||
)
|
||||
_async_register_clientsession_shutdown(hass, clientsession)
|
||||
hass.data[key] = clientsession
|
||||
|
||||
return hass.data[key]
|
||||
|
@ -121,7 +122,7 @@ def _async_register_clientsession_shutdown(hass, clientsession):
|
|||
clientsession.detach()
|
||||
|
||||
hass.bus.async_listen_once(
|
||||
EVENT_HOMEASSISTANT_STOP, _async_close_websession)
|
||||
EVENT_HOMEASSISTANT_CLOSE, _async_close_websession)
|
||||
|
||||
|
||||
@callback
|
||||
|
@ -130,37 +131,30 @@ def _async_get_connector(hass, verify_ssl=True):
|
|||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
is_new = False
|
||||
|
||||
if verify_ssl:
|
||||
if DATA_CONNECTOR not in hass.data:
|
||||
connector = aiohttp.TCPConnector(loop=hass.loop)
|
||||
hass.data[DATA_CONNECTOR] = connector
|
||||
is_new = True
|
||||
else:
|
||||
connector = hass.data[DATA_CONNECTOR]
|
||||
else:
|
||||
if DATA_CONNECTOR_NOTVERIFY not in hass.data:
|
||||
connector = aiohttp.TCPConnector(loop=hass.loop, verify_ssl=False)
|
||||
hass.data[DATA_CONNECTOR_NOTVERIFY] = connector
|
||||
is_new = True
|
||||
else:
|
||||
connector = hass.data[DATA_CONNECTOR_NOTVERIFY]
|
||||
|
||||
if is_new:
|
||||
@asyncio.coroutine
|
||||
def _async_close_connector(event):
|
||||
"""Close connector pool."""
|
||||
yield from connector.close()
|
||||
|
||||
hass.bus.async_listen_once(
|
||||
EVENT_HOMEASSISTANT_CLOSE, _async_close_connector)
|
||||
|
||||
return connector
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_cleanup_websession(hass):
|
||||
"""Cleanup aiohttp connector pool.
|
||||
|
||||
This method is a coroutine.
|
||||
"""
|
||||
tasks = []
|
||||
if DATA_CLIENTSESSION in hass.data:
|
||||
hass.data[DATA_CLIENTSESSION].detach()
|
||||
if DATA_CONNECTOR in hass.data:
|
||||
tasks.append(hass.data[DATA_CONNECTOR].close())
|
||||
if DATA_CLIENTSESSION_NOTVERIFY in hass.data:
|
||||
hass.data[DATA_CLIENTSESSION_NOTVERIFY].detach()
|
||||
if DATA_CONNECTOR_NOTVERIFY in hass.data:
|
||||
tasks.append(hass.data[DATA_CONNECTOR_NOTVERIFY].close())
|
||||
|
||||
if tasks:
|
||||
yield from asyncio.wait(tasks, loop=hass.loop)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue