Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request merges significant performance and infrastructure improvements from the dev branch to master, focusing on resource optimization for 1C1G (1 CPU, 1GB RAM) deployments.
Changes:
- Introduced comprehensive caching layer with TTL-based bounded caches for DNS, GeoIP, GeoSite, and GitHub API calls
- Added metrics collection and export system for monitoring performance and resource usage
- Refactored event loop architecture to use single async event loop instead of mixing threads and asyncio
- Implemented memory trimming helpers and conditional HTTP updates with ETag/Last-Modified support
- Added profiling and stress testing tools for performance analysis
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
tools/stress_sim.py |
New stress simulation tool for exercising core logic with concurrent workers |
tools/profile_runtime.py |
New runtime profiler for collecting RSS, VMS, swap, CPU, and task metrics |
tests/test_utils_cache_metrics.py |
Unit tests for TTLCache and MetricsStore |
tests/test_data_manager.py |
Tests for DataManager scheduler lifecycle and session management |
src/utils/metrics.py |
Lightweight metrics collection with counters, histograms, and periodic export |
src/utils/memory.py |
Memory trimming helper using glibc malloc_trim |
src/utils/cache.py |
Generic TTL-based bounded cache implementation |
src/services/github_service.py |
Added file caching, metrics instrumentation, and io.StringIO usage |
src/services/geoip_service.py |
Added location info caching with configurable size and TTL |
src/services/dns_service.py |
Added A/NS record caching, semaphore concurrency control, and metrics |
src/data_manager.py |
Refactored scheduler to asyncio, added conditional updates and GeoSite caching |
src/config.py |
Added configuration options for caches, timeouts, metrics, and memory limits |
src/main.py |
Unified event loop architecture, removed nested asyncio.run() calls |
src/bot.py |
Improved lifecycle management, added metrics exporter startup |
src/handlers/handler_manager.py |
Propagated cache configuration to services, added periodic cleanup |
README.md |
Documented new environment variables and deployment recommendations |
Comments suppressed due to low confidence (1)
src/handlers/handler_manager.py:228
- The user_add_history dictionary is accessed and modified by multiple methods (check_user_add_limit, _maybe_cleanup_user_history, record_user_add) without any synchronization. While Python's GIL provides some protection, the cleanup operation in _maybe_cleanup_user_history iterates over items while potentially modifying the dict (pop), and check_user_add_limit also modifies the list for a specific user. If multiple Telegram handler callbacks run concurrently (which they can in python-telegram-bot), this could lead to race conditions. Consider adding a threading.Lock to protect these operations.
self._maybe_cleanup_user_history()
current_time = time.time()
one_hour_ago = current_time - 3600 # 1小时前的时间戳
# 清理1小时前的记录
self.user_add_history[user_id] = [
timestamp for timestamp in self.user_add_history[user_id]
if timestamp > one_hour_ago
]
# 检查当前小时内的添加次数
current_count = len(self.user_add_history[user_id])
remaining = self.MAX_ADDS_PER_HOUR - current_count
return current_count < self.MAX_ADDS_PER_HOUR, remaining
def _maybe_cleanup_user_history(self) -> None:
now = time.time()
if now - self._last_history_cleanup < 600:
return
cutoff = now - 3600
for uid, timestamps in list(self.user_add_history.items()):
filtered = [ts for ts in timestamps if ts > cutoff]
if filtered:
self.user_add_history[uid] = filtered
else:
self.user_add_history.pop(uid, None)
self._last_history_cleanup = now
def record_user_add(self, user_id: int):
"""记录用户添加操作"""
current_time = time.time()
self.user_add_history[user_id].append(current_time)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.