Skip to content

feat(client): add request rate limiting - #823

Merged
gijzelaerr merged 4 commits into
masterfrom
feat/628-request-rate-limiter
Sep 10, 2026
Merged

feat(client): add request rate limiting#823
gijzelaerr merged 4 commits into
masterfrom
feat/628-request-rate-limiter

Conversation

@gijzelaerr

@gijzelaerr gijzelaerr commented Aug 18, 2026

Copy link
Copy Markdown
Owner

Summary

  • add opt-in per-client request limiting at the outbound S7 PDU boundary
  • support evenly spaced fixed-rate and burst-capable token-bucket algorithms
  • support blocking and immediate rejection when the limit is reached
  • cover both synchronous and asynchronous clients
  • document configuration and add deterministic limiter tests

A multi-variable PDU counts as one request, while chunked and multi-packet operations count each actual PDU. Model-specific defaults are intentionally not inferred: the limiter is disabled by default and users choose a rate appropriate for their PLC and workload.

Silent drops are unsafe at the PDU boundary because the surrounding client operation would wait for a response to a request that was never sent. The non-blocking behavior therefore raises S7RateLimitError immediately.

The branch includes current master through a merge commit.

Validation

  • uv run --frozen pre-commit run --all-files
  • uv run --frozen --extra test --extra s7commplus pytest -q (1,902 passed, 78 skipped)
  • focused limiter/client tests (163 passed, 1 skipped)
  • uv build --no-sources

Fixes #628

@gijzelaerr
gijzelaerr requested review from nikteliy and removed request for nikteliy September 1, 2026 17:07
@gijzelaerr

Copy link
Copy Markdown
Owner Author

@NiklasReisser Given your recent work around server behavior and application-facing semantics, could you review the request-rate-limiting API and concurrency behavior here?

Comment thread snap7/rate_limiter.py Outdated
Comment thread snap7/rate_limiter.py Outdated
Comment thread snap7/rate_limiter.py Outdated
Comment thread README.rst Outdated
Comment thread snap7/rate_limiter.py
@gijzelaerr gijzelaerr added this to the 4.1 milestone Sep 8, 2026
@gijzelaerr

Copy link
Copy Markdown
Owner Author

@amorelettronico you previously tested the pure-Python client on CPU 414-5H and CPU416-2 hardware. Could you run a small real-PLC smoke test of the opt-in rate limiter on an S7-400 when convenient?\n\nThe useful check would be a low fixed rate (for example 5 requests/second), confirming reads still succeed and are spaced as configured, followed by disabling the limiter and confirming normal behavior returns. No production load test or sensitive PLC data is needed.

@amorelettronico

Copy link
Copy Markdown

Hi @gijzelaerr

Hardware smoke test — S7-416-2 DP

Tested PR #823 against a Siemens S7-416-2 DP using DB15 as a dedicated test DB.

Configuration

  • Test DB: DB15
  • Rate limit: 5 requests/sec
  • Expected interval: 200 ms
  • Algorithm: fixed
  • Behavior: block

DB read

  • Successfully read DB15.
  • Returned values matched the expected test data.

Rate limiter — 5 requests/sec

  • 15 consecutive DB reads completed successfully.
  • Measured intervals:
0.200, 0.201, 0.199, 0.199, 0.202,
0.198, 0.199, 0.202, 0.201, 0.200,
0.198, 0.200, 0.201, 0.200 sec
  • Average interval: 0.200 sec

Limiter disabled

  • With max_requests_per_second=0, reads completed successfully without the intentional 200 ms delay.
  • Measured intervals were approximately 6–15 ms, confirming that the rate limiter was no longer imposing the 5 req/sec limit.

Conclusion

The rate limiter behaves as expected on the S7-416-2 DP with a real PLC connection.

PR #823 smoke test: PASS.

@gijzelaerr

Copy link
Copy Markdown
Owner Author

Thanks @amorelettronico — this covers the requested hardware smoke test: successful reads, approximately 200 ms spacing at 5 requests/sec, and normal timing with the limiter disabled.

@NiklasReisser the review findings were addressed in cd631ed and replied to inline. All current checks pass, and the S7-416-2 DP hardware test now passes too. Could you take another look and submit a review? The existing review request is still pending.

@gijzelaerr
gijzelaerr merged commit 458b829 into master Sep 10, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request rate limiting to protect PLC resources

3 participants