How tower-rs/tower works
Project Type and Purpose
• Tower is an open-source Rust library (a collection of crates) that provides a modular, middleware‐style framework for building request/response‐based networking clients and servers.
• It exposes two core traits—Service and Layer—and a rich set of ready-made layers (buffering, retry, timeout, rate-limit, load-shedding, etc.) which can be composed into processing pipelines.
Main Components
• tower-service: defines the Service trait (request → future → response)
• tower-layer: defines the Layer trait (wraps one Service into another)
• tower: implements all of the layer types, utility functions, and the “make” builders
• tower-test: provides mock services, testing macros, and test harnesses
• examples & guides: usage examples and tutorials
B. External Dependencies
Relationships and Data Flow
• tower-service → tower-layer: tower‐layer depends on the abstraction of Service
• tower-layer → tower: tower implements concrete layers using both Service and Layer traits
• tower-test → tower / tower-service: for verifying services and layers via mocks
• Application (user code) invokes a Service by calling Service::call, traversing the layer stack, and ultimately hitting the real transport
Architectural Patterns and Design Principles
• Middleware / chain of responsibility: each Layer wraps a downstream Service
• Composition over inheritance: layers are generic, zero‐cost abstractions
• Trait‐based abstraction: Service and Layer traits decouple protocols from implementations
• Builder pattern: “make_service” modules provide utilities to assemble stacks