-
Notifications
You must be signed in to change notification settings - Fork 116
core: services: helper: main: Control number of worker for scan_ports based on hardware #3478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core: services: helper: main: Control number of worker for scan_ports based on hardware #3478
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's Guidescan_ports now dynamically assigns the ThreadPoolExecutor worker count based on CPU type (Pi3) and the number of ports to optimize CPU usage peaks Class diagram for updated scan_ports worker assignment logicclassDiagram
class Helper {
<<static>> SKIP_PORTS
<<static>> detect_service(port)
}
class CpuType {
PI3
}
class main {
scan_ports()
}
Helper <|-- main
CpuType <|-- main
main : scan_ports() uses get_cpu_type() and len(ports) to set max_workers
main : scan_ports() uses ThreadPoolExecutor(max_workers)
main : scan_ports() submits detect_service(port) for each port
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @joaoantoniocardoso - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `core/services/helper/main.py:435` </location>
<code_context>
+ if get_cpu_type() == CpuType.PI3 or len(ports) == 0:
+ max_workers = 2
+ else:
+ max_workers = len(ports)
+
+ with futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
</code_context>
<issue_to_address>
Setting max_workers to len(ports) may cause excessive thread creation for large port lists.
Consider setting an upper limit for max_workers to prevent resource exhaustion and maintain system stability.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
if get_cpu_type() == CpuType.PI3 or len(ports) == 0:
max_workers = 2
else:
max_workers = len(ports)
with futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
=======
MAX_WORKERS = 8 # Upper limit for thread pool size
if get_cpu_type() == CpuType.PI3 or len(ports) == 0:
max_workers = 2
else:
max_workers = min(len(ports), MAX_WORKERS)
with futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
>>>>>>> REPLACE
</suggested_fix>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| if get_cpu_type() == CpuType.PI3 or len(ports) == 0: | ||
| max_workers = 2 | ||
| else: | ||
| max_workers = len(ports) | ||
|
|
||
| with futures.ThreadPoolExecutor(max_workers=max_workers) as executor: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (performance): Setting max_workers to len(ports) may cause excessive thread creation for large port lists.
Consider setting an upper limit for max_workers to prevent resource exhaustion and maintain system stability.
| if get_cpu_type() == CpuType.PI3 or len(ports) == 0: | |
| max_workers = 2 | |
| else: | |
| max_workers = len(ports) | |
| with futures.ThreadPoolExecutor(max_workers=max_workers) as executor: | |
| MAX_WORKERS = 8 # Upper limit for thread pool size | |
| if get_cpu_type() == CpuType.PI3 or len(ports) == 0: | |
| max_workers = 2 | |
| else: | |
| max_workers = min(len(ports), MAX_WORKERS) | |
| with futures.ThreadPoolExecutor(max_workers=max_workers) as executor: |
|
|
||
| # The detect_services run several of requests sequentially, so we are capping the amount of executors to lower the peaks on the CPU usage | ||
| with futures.ThreadPoolExecutor(max_workers=2) as executor: | ||
| if get_cpu_type() == CpuType.PI3 or len(ports) == 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): Simplify sequence length comparison (simplify-len-comparison)
| if get_cpu_type() == CpuType.PI3 or len(ports) == 0: | |
| if get_cpu_type() == CpuType.PI3 or not ports: |
… based on hardware Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
eeb1913 to
d687b33
Compare
This is a backport of #3362 into 1.4
Summary by Sourcery
Enhancements: