README.md
1# Tower
2
3Tower is a library of modular and reusable components for building robust
4networking clients and servers.
5
6[![Crates.io][crates-badge]][crates-url]
7[![Documentation][docs-badge]][docs-url]
8[![Documentation (master)][docs-master-badge]][docs-master-url]
9[![MIT licensed][mit-badge]][mit-url]
10[![Build Status][actions-badge]][actions-url]
11[![Discord chat][discord-badge]][discord-url]
12
13[crates-badge]: https://img.shields.io/crates/v/tower.svg
14[crates-url]: https://crates.io/crates/tower
15[docs-badge]: https://docs.rs/tower/badge.svg
16[docs-url]: https://docs.rs/tower
17[docs-master-badge]: https://img.shields.io/badge/docs-master-blue
18[docs-master-url]: https://tower-rs.github.io/tower/tower
19[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
20[mit-url]: LICENSE
21[actions-badge]: https://github.com/tower-rs/tower/workflows/CI/badge.svg
22[actions-url]:https://github.com/tower-rs/tower/actions?query=workflow%3ACI
23[discord-badge]: https://img.shields.io/discord/500028886025895936?logo=discord&label=discord&logoColor=white
24[discord-url]: https://discord.gg/EeF3cQw
25## Overview
26
27Tower aims to make it as easy as possible to build robust networking clients and
28servers. It is protocol agnostic, but is designed around a request / response
29pattern. If your protocol is entirely stream based, Tower may not be a good fit.
30
31Tower provides a simple core abstraction, the [`Service`] trait, which
32represents an asynchronous function taking a request and returning either a
33response or an error. This abstraction can be used to model both clients and
34servers.
35
36Generic components, like [timeouts], [rate limiting], and [load balancing],
37can be modeled as [`Service`]s that wrap some inner service and apply
38additional behavior before or after the inner service is called. This allows
39implementing these components in a protocol-agnostic, composable way. Typically,
40such services are referred to as _middleware_.
41
42An additional abstraction, the [`Layer`] trait, is used to compose
43middleware with [`Service`]s. If a [`Service`] can be thought of as an
44asynchronous function from a request type to a response type, a [`Layer`] is
45a function taking a [`Service`] of one type and returning a [`Service`] of a
46different type. The [`ServiceBuilder`] type is used to add middleware to a
47service by composing it with multiple multiple [`Layer`]s.
48
49### The Tower Ecosystem
50
51Tower is made up of the following crates:
52
53* [`tower`] (this crate)
54* [`tower-service`]
55* [`tower-layer`]
56* [`tower-test`]
57
58Since the [`Service`] and [`Layer`] traits are important integration points
59for all libraries using Tower, they are kept as stable as possible, and
60breaking changes are made rarely. Therefore, they are defined in separate
61crates, [`tower-service`] and [`tower-layer`]. This crate contains
62re-exports of those core traits, implementations of commonly-used
63middleware, and [utilities] for working with [`Service`]s and [`Layer`]s.
64Finally, the [`tower-test`] crate provides tools for testing programs using
65Tower.
66
67## Usage
68
69Tower provides an abstraction layer, and generic implementations of various
70middleware. This means that the `tower` crate on its own does *not* provide
71a working implementation of a network client or server. Instead, Tower's
72[`Service` trait][`Service`] provides an integration point between
73application code, libraries providing middleware implementations, and
74libraries that implement servers and/or clients for various network
75protocols.
76
77Depending on your particular use case, you might use Tower in several ways:
78
79* **Implementing application logic** for a networked program. You might
80 use the [`Service`] trait to model your application's behavior, and use
81 the middleware [provided by this crate][all_layers] and by other libraries
82 to add functionality to clients and servers provided by one or more
83 protocol implementations.
84* **Implementing middleware** to add custom behavior to network clients and
85 servers in a reusable manner. This might be general-purpose middleware
86 (and if it is, please consider releasing your middleware as a library for
87 other Tower users!) or application-specific behavior that needs to be
88 shared between multiple clients or servers.
89* **Implementing a network protocol**. Libraries that implement network
90 protocols (such as HTTP) can depend on `tower-service` to use the
91 [`Service`] trait as an integration point between the protocol and user
92 code. For example, a client for some protocol might implement [`Service`],
93 allowing users to add arbitrary Tower middleware to those clients.
94 Similarly, a server might be created from a user-provided [`Service`].
95
96 Additionally, when a network protocol requires functionality already
97 provided by existing Tower middleware, a protocol implementation might use
98 Tower middleware internally, as well as as an integration point.
99
100### Library Support
101
102A number of third-party libraries support Tower and the [`Service`] trait.
103The following is an incomplete list of such libraries:
104
105* [`hyper`]: A fast and correct low-level HTTP implementation.
106* [`tonic`]: A [gRPC-over-HTTP/2][grpc] implementation built on top of
107 [`hyper`]. See [here][tonic-examples] for examples of using [`tonic`] with
108 Tower.
109* [`warp`]: A lightweight, composable web framework. See
110 [here][warp-service] for details on using [`warp`] with Tower.
111* [`tower-lsp`] and its fork, [`lspower`]: implementations of the [Language
112 Server Protocol][lsp] based on Tower.
113* [`kube`]: Kubernetes client and futures controller runtime. [`kube::Client`]
114 makes use of the Tower ecosystem: [`tower`], [`tower-http`], and
115 [`tower-test`]. See [here][kube-example-minimal] and
116 [here][kube-example-trace] for examples of using [`kube`] with Tower.
117
118[`hyper`]: https://crates.io/crates/hyper
119[`tonic`]: https://crates.io/crates/tonic
120[tonic-examples]: https://github.com/hyperium/tonic/tree/master/examples/src/tower
121[grpc]: https://grpc.io
122[`warp`]: https://crates.io/crates/warp
123[warp-service]: https://docs.rs/warp/0.2.5/warp/fn.service.html
124[`tower-lsp`]: https://crates.io/crates/tower-lsp
125[`lspower`]: https://crates.io/crates/lspower
126[lsp]: https://microsoft.github.io/language-server-protocol/
127[`kube`]: https://crates.io/crates/kube
128[`kube::Client`]: https://docs.rs/kube/latest/kube/struct.Client.html
129[kube-example-minimal]: https://github.com/clux/kube-rs/blob/master/examples/custom_client.rs
130[kube-example-trace]: https://github.com/clux/kube-rs/blob/master/examples/custom_client_trace.rs
131[`tower-http`]: https://crates.io/crates/tower-http
132
133If you're the maintainer of a crate that supports Tower, we'd love to add
134your crate to this list! Please [open a PR] adding a brief description of
135your library!
136
137### Getting Started
138
139The various middleware implementations provided by this crate are feature
140flagged, so that users can only compile the parts of Tower they need. By
141default, all the optional middleware are disabled.
142
143To get started using all of Tower's optional middleware, add this to your
144`Cargo.toml`:
145
146```toml
147tower = { version = "0.4", features = ["full"] }
148```
149
150Alternatively, you can only enable some features. For example, to enable
151only the [`retry`] and [`timeout`][timeouts] middleware, write:
152
153```toml
154tower = { version = "0.4", features = ["retry", "timeout"] }
155```
156
157See [here][all_layers] for a complete list of all middleware provided by
158Tower.
159
160[`Service`]: https://docs.rs/tower/latest/tower/trait.Service.html
161[`Layer`]: https://docs.rs/tower/latest/tower/trait.Layer.html
162[all_layers]: https://docs.rs/tower/latest/tower/#modules
163[timeouts]: https://docs.rs/tower/latest/tower/timeout/
164[rate limiting]: https://docs.rs/tower/latest/tower/limit/rate
165[load balancing]: https://docs.rs/tower/latest/tower/balance/
166[`ServiceBuilder`]: https://docs.rs/tower/latest/tower/struct.ServiceBuilder.html
167[utilities]: https://docs.rs/tower/latest/tower/trait.ServiceExt.html
168[`tower`]: https://crates.io/crates/tower
169[`tower-service`]: https://crates.io/crates/tower-service
170[`tower-layer`]: https://crates.io/crates/tower-layer
171[`tower-test`]: https://crates.io/crates/tower-test
172[`retry`]: https://docs.rs/tower/latest/tower/retry
173[open a PR]: https://github.com/tower-rs/tower/compare
174
175
176## Supported Rust Versions
177
178Tower will keep a rolling MSRV (minimum supported Rust version) policy of **at
179least** 6 months. When increasing the MSRV, the new Rust version must have been
180released at least six months ago. The current MSRV is 1.49.0.
181
182## License
183
184This project is licensed under the [MIT license](LICENSE).
185
186### Contribution
187
188Unless you explicitly state otherwise, any contribution intentionally submitted
189for inclusion in Tower by you, shall be licensed as MIT, without any additional
190terms or conditions.
191