PHP 8.2.30
Preview: agent.py Size: 2.55 KB
//opt/cloudlinux/venv/lib/python3.11/site-packages/ssa/agent.py

# -*- coding: utf-8 -*-

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2021 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

"""
This module contains contains classes implementing SSA Agent behaviour
"""
import atexit
import json
import logging
from concurrent.futures import ThreadPoolExecutor
from threading import current_thread
from .internal.constants import agent_sock
from .internal.exceptions import SSAError
from .internal.utils import create_socket
from .modules.processor import RequestProcessor

# Maximum number of concurrent worker threads for handling requests.
# Limits memory usage on high-traffic servers.
MAX_WORKERS = 50


class SimpleAgent:
    """
    SSA Simple Agent class
    """

    def __init__(self):
        self.logger = logging.getLogger('agent')
        self.request_processor = RequestProcessor()
        self.executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)
        atexit.register(self._shutdown)
        # start serving incoming connections
        self.listen()

    def _shutdown(self):
        """Gracefully shutdown the thread pool executor."""
        self.executor.shutdown(wait=False)

    def listen(self) -> None:
        """
        Start listening socket
        """
        _socket = create_socket(agent_sock)
        while True:
            connection, address = _socket.accept()
            self.executor.submit(self.handle, connection)
            self.logger.debug('[ThreadPool] Submitted task')

    def handle(self, connection: 'socket object') -> None:
        """
        Handle incoming connection
        :param connection: socket object usable to
        send and receive data on the connection
        """
        fileobj = connection.makefile(errors='ignore')
        try:
            input_data = self.read_input(fileobj)
            self.request_processor.handle(input_data)
        except SSAError as e:
            self.logger.error('Handled exception in [%s]: %s',
                              current_thread().name, str(e))
            pass
        connection.close()

    def read_input(self, fileio: 'file object') -> dict:
        """
        Read input data and return decoded json
        :param fileio: a file-like object providing read method
        """
        data = fileio.read()
        self.logger.info('[%s] I received %i bytes: %s',
                         current_thread().name, len(data.encode()),
                         data)
        if data:
            return json.loads(data.strip(), strict=False)
        else:
            return {}

Directory Contents

Dirs: 6 × Files: 9

Name Size Perms Modified Actions
- drwxr-xr-x 2026-03-14 07:02:31
Edit Download
- drwxr-xr-x 2026-03-14 07:02:31
Edit Download
internal DIR
- drwxr-xr-x 2026-03-14 07:02:31
Edit Download
modules DIR
- drwxr-xr-x 2026-03-14 07:02:31
Edit Download
ssa_utils DIR
- drwxr-xr-x 2026-03-14 07:02:31
Edit Download
- drwxr-xr-x 2026-03-14 07:03:38
Edit Download
2.55 KB lrw-r--r-- 2026-02-10 14:39:48
Edit Download
1.29 KB lrw-r--r-- 2026-02-10 14:39:48
Edit Download
382 B lrwxr-xr-x 2026-02-10 14:39:48
Edit Download
374 B lrwxr-xr-x 2026-02-10 14:39:48
Edit Download
378 B lrwxr-xr-x 2026-02-10 14:39:48
Edit Download
8.07 KB lrw-r--r-- 2026-02-10 14:39:48
Edit Download
20.43 KB lrw-r--r-- 2026-02-10 14:39:48
Edit Download
8.39 KB lrw-r--r-- 2026-02-10 14:39:48
Edit Download
0 B lrw-r--r-- 2026-02-10 14:39:48
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).