[WIP] Chapter 10: Tower middleware code samples - #28
Open
lmammino wants to merge 3 commits into
Open
Conversation
Backend-agnostic trait now takes (identifier, bucket); DynamoDB-specific details (table_name, key format, TTL) live on DynamoDbRateLimitStore. Renames the store error variant DynamoDb -> Backend. Imports std::collections::HashMap in the test module for the in-memory mock, and updates the test comment to make explicit that Retry-After is only meaningful on 429.
Adds allows_exactly_max_requests_before_blocking, which exercises a non-trivial limit (N=3) and asserts that requests 1..=N succeed with decreasing X-RateLimit-Remaining values and request N+1 is the first to receive 429. The existing over_limit_returns_429 only covered N=1, which left the boundary semantic implicit; this pins it so a future refactor cannot silently shift it.
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.
Status: work in progress for chapter 10 of the book.
Summary
Adds the chapter 10 workspace to
code-samples/. The chapter covers Tower-based middleware for AWS Lambda HTTP handlers, building up to a DynamoDB-backed fixed-window rate limiter wired into the URL shortener.Headline additions:
chapter-10/shared/src/middleware/ip_extractor.rs— pulls the client IP from the API Gateway request context.chapter-10/shared/src/middleware/rate_limit.rs—RateLimitLayer/RateLimitService/ abstractRateLimitStoretrait /DynamoDbRateLimitStoreimpl / customisableon_over_limitandon_unavailablehooks / unit tests with an in-memory mock.chapter-10/shared/examples/{noop_layer,log_layer_request_only,log_layer,powered_by_layer,error_recovery}.rs— the small Tower examples the chapter walks through.chapter-10/lambdas/{create_link,get_links,visit_link,process_link_*}/— URL shortener Lambdas with the rate-limit layer applied to the management endpoints (POST /links,GET /links) and intentionally not applied to the redirect path.chapter-10/template.yaml— SAM template with the rate-limit DynamoDB table and least-privilege IAM.chapter-10/integration-tests/— end-to-end checks against a deployed stack.Companion PR
Book prose lives in rust-lambda/book and is being reviewed in the matching
feat/chapter-10-middlewarebranch there.Test plan
cargo build --workspaceclean fromchapter-10/cargo test middleware::rate_limit— 9 tests, including the newallows_exactly_max_requests_before_blockingboundary testcargo test --workspace(some pre-existingfigment::Jailenv-var contention inconfiguration.rsis unrelated)