fix: Add timeout to keystone session

When a network failure occurs, the openstack client will be blocked for
a long time. Set a timeout on the session to feedback network problem
as soon as possible.

Change-Id: I2eec95b18241b0bf2103ad3dfa1efd2774f2bd77
This commit is contained in:
Gao Hanxiang 2021-07-22 08:11:14 -04:00
parent 70381b608c
commit acf17c752b
4 changed files with 9 additions and 5 deletions

View File

@ -16,7 +16,7 @@ from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException, Request, Response, status
from keystoneauth1.identity.v3 import Password
from keystoneauth1.session import Session as osSession
from keystoneauth1.session import Session
from keystoneclient.client import Client as KeystoneClient
from skyline_log import LOG
@ -36,6 +36,7 @@ from skyline_apiserver.core.security import (
generate_profile_by_token,
parse_access_token,
)
from skyline_apiserver.types import constants
from skyline_apiserver.db import api as db_api
router = APIRouter()
@ -78,7 +79,7 @@ async def login(credential: schemas.Credential, response: Response):
password=credential.password,
reauthenticate=False,
)
session = osSession(auth=unscope_auth, verify=False)
session = Session(auth=unscope_auth, verify=False, timeout=constants.DEFAULT_TIMEOUT)
unscope_client = KeystoneClient(session=session, endpoint=auth_url)
project_scope = unscope_client.auth.projects()
# we must get the project_scope with enabled project

View File

@ -25,6 +25,7 @@ from starlette.concurrency import run_in_threadpool
from skyline_apiserver.client import utils
from skyline_apiserver.client.utils import get_system_session
from skyline_apiserver.config import CONF
from skyline_apiserver.types import constants
async def get_project_scope_token(
@ -40,7 +41,7 @@ async def get_project_scope_token(
kwargs = {"project_id": project_id}
scope_auth = Token(auth_url=auth_url, token=keystone_token, **kwargs)
session = Session(auth=scope_auth, verify=False)
session = Session(auth=scope_auth, verify=False, timeout=constants.DEFAULT_TIMEOUT)
keystone_token = session.get_token()
return keystone_token

View File

@ -47,7 +47,7 @@ async def generate_session(profile: schemas.Profile) -> Any:
"project_id": profile.project.id,
}
auth = Token(**kwargs)
session = Session(auth=auth, verify=False)
session = Session(auth=auth, verify=False, timeout=constants.DEFAULT_TIMEOUT)
session.auth.auth_ref = await run_in_threadpool(session.auth.get_auth_ref, session)
return session
@ -66,7 +66,7 @@ def get_system_session() -> Session:
project_domain_name=CONF.openstack.system_project_domain,
reauthenticate=True,
)
SESSION = Session(auth=auth, verify=False)
SESSION = Session(auth=auth, verify=False, timeout=constants.DEFAULT_TIMEOUT)
return SESSION

View File

@ -35,3 +35,5 @@ ID_UUID_RANGE_STEP = 100
SETTINGS_HIDDEN_SET = set()
SETTINGS_RESTART_SET = set()
DEFAULT_TIMEOUT = 30