Skip to content

Commit

Permalink
Merge pull request #447 from kytos-ng/feat/api_pool
Browse files Browse the repository at this point in the history
Add in a config option for api threadpool size
  • Loading branch information
Ktmi authored Feb 7, 2024
2 parents 1ecb615 + e5470f9 commit 74a04b4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ All notable changes to the kytos project will be documented in this file.
UNRELEASED - Under development
******************************

General Information
===================

- ``kytos.conf.template`` has changed you might want to regenerate ``kytos.conf`` if you want to set non default values

Added
=====
- Added ``Interface.tag_ranges`` as ``dict[str, list[list[int]]]`` as replacement for ``vlan_pool`` settings.
Expand All @@ -15,6 +20,8 @@ Added
- Added ``special_available_tags`` which stores `"untagged"` and `"any"` if they can be used from an Interface.
- Added ``maxsize_multiplier`` on ``event_buffer_conf``, which will multiply the ``maxsize`` value of the queue. By default, all KytosEventBuffer who use a bounded queue will have ``maxsize_multiplier: 2``. This default is reasonable to work out of the box with kytos-ng core NApps. But, if you have other NApps who tend to produce too many events you might want to either increase the size of the queue with and/or increase the number of max workers in the thread pool if the event handler is running on a thread pool. Typically, you'll want to first start adjusting the number of workers in the thread pool.
- Introduced a new ``meta`` on ``KytosBuffers``, which is meant for general core control events.
- Added ``api`` threadpool. This is used by the ASGI server to handle requests to non async endpoints.
- Added in configuration option ``api_concurrency_limit``, specifies the max number of concurrent API requests before rejecting the request.

Changed
=======
Expand Down
18 changes: 18 additions & 0 deletions kytos/core/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from urllib.request import urlretrieve

import httpx
from anyio import CapacityLimiter
from anyio.lowlevel import RunVar
from starlette.applications import Starlette
from starlette.exceptions import HTTPException
from starlette.middleware import Middleware
Expand All @@ -23,6 +25,7 @@

from kytos.core.auth import authenticated
from kytos.core.config import KytosConfig
from kytos.core.helpers import get_thread_pool_max_workers
from kytos.core.rest_api import JSONResponse, Request

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -57,11 +60,26 @@ def __init__(self, listen='0.0.0.0', port=8181,
Middleware(CORSMiddleware, allow_origins=["*"]),
],
)
kytos_conf = KytosConfig().options["daemon"]

api_threadpool_size = get_thread_pool_max_workers().get('api', 160)

concurrency_limit = kytos_conf.api_concurrency_limit
if concurrency_limit == 'threadpool':
concurrency_limit = api_threadpool_size

@self.app.on_event("startup")
def _app_startup_func():
RunVar("_default_thread_limiter").set(
CapacityLimiter(api_threadpool_size)
)

self.server = Server(
UvicornConfig(
self.app,
host=self.listen,
port=self.port,
limit_concurrency=concurrency_limit
)
)

Expand Down
4 changes: 4 additions & 0 deletions kytos/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def parse_args(self):
'listen': '0.0.0.0',
'port': 6653,
'api_traceback_on_500': True,
'api_concurrency_limit': 'threadpool',
'foreground': False,
'protocol_name': '',
'enable_entities_by_default': False,
Expand Down Expand Up @@ -261,6 +262,9 @@ def _parse_json(value):
options.event_buffer_conf = _parse_json(
options.event_buffer_conf
)
options.api_concurrency_limit = _parse_json(
options.api_concurrency_limit
)

return options

Expand Down
7 changes: 6 additions & 1 deletion kytos/templates/kytos.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ jwt_secret = {{ jwt_secret }}
# - sb: it's used automatically by kytos/of_core.* events, it's meant for southbound related messages
# - app: it's meant for general NApps event, it's default pool if no other one has been specified
# - db: it can be used by for higher priority db related tasks (need to be parametrized on decorator)
thread_pool_max_workers = {"sb": 256, "db": 256, "app": 512}
# - api: Not used by events, but instead API requests.
thread_pool_max_workers = {"sb": 256, "db": 256, "app": 512, "api": 160}

# Configuration for KytosEventBuffers
# Valid event buffers are "msg_in", "msg_out", "app", "conn", and "raw".
Expand Down Expand Up @@ -127,3 +128,7 @@ apm =
# is in the list, then every URL containing "kytos/mef_eline" will match
# it and, therefore, require authentication.
# authenticate_urls = ["kytos/mef_eline", "kytos/pathfinder"]

# Define the max number of connections to accept before additional
# connections are automatically rejected.
api_concurrency_limit = "threadpool"
2 changes: 1 addition & 1 deletion tests/unit/test_core/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test():
@staticmethod
def test_default_executors():
"""Test default expected executors."""
pools = ["app", "db", "sb"]
pools = ["api", "app", "db", "sb"]
assert sorted(pools) == sorted(executors.keys())
assert not ds_executors

Expand Down

0 comments on commit 74a04b4

Please sign in to comment.