Skip to content

Navigation Menu

Sign in
Sign up

[WIP] Chapter 10: Tower middleware code samples - #28

Open
lmammino wants to merge 3 commits into
main from
feat/chapter-10-middleware
Open

[WIP] Chapter 10: Tower middleware code samples #28
lmammino wants to merge 3 commits into
main from
feat/chapter-10-middleware

Conversation

@lmammino

@lmammino lmammino commented May 28, 2026

Copy link
Copy Markdown
Contributor

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.rsRateLimitLayer / RateLimitService / abstract RateLimitStore trait / DynamoDbRateLimitStore impl / customisable on_over_limit and on_unavailable hooks / 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-middleware branch there.

Test plan

  • cargo build --workspace clean from chapter-10/
  • cargo test middleware::rate_limit — 9 tests, including the new allows_exactly_max_requests_before_blocking boundary test
  • Full cargo test --workspace (some pre-existing figment::Jail env-var contention in configuration.rs is unrelated)
  • End-to-end test against a deployed stack

lmammino added 3 commits May 5, 2026 11:05
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

AltStyle によって変換されたページ (->オリジナル) /