Skip to content

Commit 088fde2

Browse files
authored
feat(utils): add session-based rate limiting overload (#878)
* Add session-based rate limiting overload Add allow(client_id, session_id) overload that creates a composite key "client_id:session_id" for per-session rate limiting. Falls back to IP-only behavior when session_id is empty. * docs: add Phase 2 changelog entries for #871 and #872 Add security entry for no_tls deprecation warning and added entry for rate_limiter session-based keys.
1 parent 5e22ac7 commit 088fde2

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Parallelize connection pool initialization with `std::async` ([#870](https://github.com/kcenon/network_system/issues/870))
1313

14+
### Security
15+
16+
- Mark `no_tls` policy as deprecated with warning to use TLS-enabled policies in production ([#871](https://github.com/kcenon/network_system/issues/871))
17+
1418
### Added
19+
20+
- Extend `rate_limiter` to support composite session-based identification keys ([#872](https://github.com/kcenon/network_system/issues/872))
1521
- Migration guide for transitioning from adapters to NetworkSystemBridge pattern
1622
- Comprehensive step-by-step migration instructions
1723
- API comparison tables for old vs new patterns

src/internal/utils/rate_limiter.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,29 @@ class rate_limiter {
160160
return false;
161161
}
162162

163+
/**
164+
* @brief Check if request should be allowed (session-aware)
165+
*
166+
* @param client_id Client identifier (e.g., IP address)
167+
* @param session_id Session identifier for per-session rate limiting
168+
* @return true if request is allowed, false if rate limited
169+
*
170+
* When session_id is non-empty, a composite key "client_id:session_id"
171+
* is used, enabling per-session rate limiting. When session_id is empty,
172+
* falls back to client_id-only behavior.
173+
*/
174+
[[nodiscard]] bool allow(std::string_view client_id, std::string_view session_id) {
175+
if (session_id.empty()) {
176+
return allow(client_id);
177+
}
178+
std::string composite_key;
179+
composite_key.reserve(client_id.size() + 1 + session_id.size());
180+
composite_key.append(client_id);
181+
composite_key.push_back(':');
182+
composite_key.append(session_id);
183+
return allow(std::string_view(composite_key));
184+
}
185+
163186
/**
164187
* @brief Check if request would be allowed (without consuming token)
165188
*

0 commit comments

Comments
 (0)