Skip to content

Commit 7ec3943

Browse files
committed
Add demo of hyper
Signed-off-by: Aisuko <[email protected]>
1 parent cdd4cae commit 7ec3943

File tree

14 files changed

+942
-4
lines changed

14 files changed

+942
-4
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ _book
1414
*.epub
1515
*.mobi
1616
*.pdf
17-
gRPC/rust/target/
17+
*target
18+
*.idea
19+
*.vscode

SUMMARY.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
* [👨‍💻 Segfault](freesoftware/the-gnu-hurd/cgo/segmentation-fault.md)
1818
* [🛃 Rust FFI](freesoftware/the-gnu-hurd/rust-ffi.md)
1919
* [🧚🏻‍♂️ Programming](freesoftware/programming/README.md)
20-
* [✏️ Introduction to programming](freesoftware/programming/introduction-to-6811-programming.md)
21-
* [- Mutable Value Semantics](freesoftware/programming/mutable_value_semantics.md)
20+
* [📖 Introduction to programming](freesoftware/programming/introduction-to-6811-programming.md)
21+
* [📖 Mutable Value Semantics](freesoftware/programming/mutable_value_semantics.md)
2222
* [📖 Linked List](freesoftware/programming/linked-list.md)
23-
* [Rust](freesoftware/programming/rust/README.md)
23+
* [📖 Rust](freespftware/programming/rust/README.md)
2424
* [📖 Keyword dyn](freesoftware/programming/rust/keyword_dyn.md)
2525
* [🎹 Algorithm](freesoftware/algorithm/README.md)
2626
* [Two-pointer Technique](freesoftware/algorithm/two_pointer_technique.md)
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Trait Codec
2+
3+
Trait that knows how to encode and decode gRPC messages
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Crate hyper
2+
3+
hyper is a fast and correct HTTP implementation written in and for Rust
4+
5+
## Features Flags
6+
* HTTP/1 and HTTP/2
7+
* Asynchronous design
8+
* Leading in performance
9+
* Tested and correct
10+
* Extensive production use
11+
* Client and Server APIs
12+
13+
14+
## "Low-level"
15+
16+
The `hyper` crate provides a low-level HTTP implementation, means to be a building block for libraries and applications.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Crate prost
2+
3+
`prost` is a Protocol Buffers implementation for the Rust language. `prost` generates simple, idiomatic Rust code from `proto2` and `proto3` files.
4+
5+
The features of `prost`:
6+
* Generates simple, idiomatic, and readble Rust types by taking advantage of Rust `derive` attributes.
7+
* Retains comments from `.proto` files in generated Rust code.
8+
* Allows existing Rust types (not generated from a .proto) to be serialized and deseri8alized by adding attributes.
9+
* Uses the `bytes::{Buf, BufMut}` abstractions for seialization instead of `std::io::{Read, Write}`
10+
* Respects the Protobuf `package` specifier when organizing generated code into Rust modules.
11+
* Preserves unknown enum values during dserialization
12+
* Does not include support for runtime reflection or message descriptor
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# tokio
2+
3+
In Rust, we call package as `Crate`. Crate tokio is a runtime for wrtiting reliable netwrok applications without compromising speed. And it is a event-driven, non-blocking I/O platform for writing asynchronous applications with Rust. It provides a rich set of components for building servers and clients.
4+
5+
6+
## Working with Tasks
7+
8+
Asynchronous program in Rust are based around lightweigh, non-blocking units of execution called tasks.
9+
10+
11+
The `tokio::task` module provides important tools for working with tasks:
12+
* The `spawn` function and `JoinHandle` type, for scheduling a new task on the Tokio runtime and awaiting the output of a spawned task, respectively.
13+
* Functions for running blocking operatiions in an asynchronous task context.
14+
15+
The `tokio::task` module is present only when the "rt" feature flag is enabled.
16+
17+
The crate Tokio provides a runtime for executing asynchronous tasks. Most applications can use the `#[tokio::main]` macro to run their code on the tokio runtime. However, this macro provides only basic configuration options. More advanced APIs for configuring and managing runtimes are provided by the `tokio::runtime` module.
18+
19+
Using the runtime requires the "rt" or "rt-multi-thread" feature flags, to enable the current-thread single-threaded scheduler and the multi-thread scheduler, respectively.
20+
21+
22+
## CPU-bound tasks and blocking code
23+
24+
Tokio is able to concurrently run many tasks on a few thread by repeatedly swapping the currently running task on each thread. However, this kind of swapping can only happen at `.await` points, so code that spends a long time wihtout reaching an `.await` will prevent other tasks from running. To combat this, Tokio provides two kinds of threads: COre threads and blocking threads.
25+
26+
The core threads are where all asynchronous code runs, and Tokio will by default spawn one for each CPU core. You can use the env variable `TOKIO_WORKER_THREADS` to override the default value.
27+
28+
The blocking threads are spawned on demand, can be used to run blocking code that would otherwise block other tasks from running and are kept alive when not used for a certain amount of time which can be configured with `thread_keep_alive`. Since it is not possible for Tokio to swap out blocking tasks, like it can do with asynchronous code, the upper limit on the number of blocking threads is very large. These limits can be configured on the Builder.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Crate tonic
2+
3+
A Rust implementation of gRPC, a high performance, open source, general RPC framework that puts mobile and HTTP/2 first. And it is a gRPC over HTTP/2 implementation focused on:
4+
5+
* **high performance**
6+
* **interoperability**
7+
* **flexibility**
8+
9+
And the library was created to have first class support of async/await and to act as a core building block for production systems wriiten in Rust.
10+
11+
12+
# Features Flags
13+
14+
* `transport` Enable the fully featured, batteries included client and serber implementation based on the [`hyper`](./hyper.md), [`tower`](./tower.md) and [`tokio`](./tokio.md) crate. Enabled by default.
15+
* `channel` Enables just the full featured channel/client portion of the `transport` feature
16+
* `codegen` Enables all the required exports and optional dependencies required for `tonic-build`. Enabled by default.
17+
* `tls` Enables the `rustls` based TLS options for the `transport` feature. Not enabled by default.
18+
* `tls-roots` Adds system trust roots to `rustls` based gRPC client using the `rustls-native-certs` crate. Not enabled by default.
19+
* `tls-webpki-roots` Add the standard trust roots from the `webpki-roots` crate to `rustls` based gRPC clients. Not enabled by default.
20+
* [`prost`](./prost.md) Enables the prost based gRPC [Codec](./codec.md) implementation
21+
* `gzip` Enables compressing requests, responses, and streams. Depends on flate2. Not enabled by default. Replaces the `compression` flag from earlier versions of tonic(<=0.7>).
22+
23+
# Usage
24+
25+
Tower provides an abstraction layer, and generic implementation of various middleware. This menas that the `tower` crate on its own does not provide a working implementation of a network client ot server. Instead, Tower's Service trait provides an integration point between application code, libraries providing middleware implementations, and libraries that implement servers and/or clients for various network protocols.
26+
27+
Depending on your particulat use case, you might use Tower in several ways:
28+
29+
* Implementing application logic for a networked program
30+
* Implementing middleware to add custom behavior to network clients and servers in a reusable manner
31+
* Implementing a network protocol
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Crate tower
2+
3+
Tower is a library of modular and reusable components for building robust networking clients and servers.
4+
5+
Tower provides a simple core abstraction, the `Service` trait, which represents an asynchronous function taking a request and returning either a response or an error. This astraction can be used to model both client and servers.
6+
7+
Generic components, like `timeouts`, `rate limiting`, and `load balancing`, can be modeled as `Service`s that wrap some inner service and apply additional behavior before or after the inner service is called. This allows implementing these components in a protocol-agnostic, composable way. Typically, such services are referred to as middleware.
8+
9+
An additinal abstraction, the `Layer` trait, is used to compose middleware with `Service`s. If a `Service` can be thought of as an asynchronous function from a request type to a response type, a `Layer` is a functrion taking a `Service` of one typr and returning a `Service` of a different type. The `ServiceBuilder` type is used to add middleware to a service by composing it with multiple `Layer`s.

gRPC/hyper/.vscode/settings.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"editor.inlayHints.enabled": "on",
3+
"editor.inlayHints.fontFamily": "Courier New",
4+
"editor.inlayHints.fontSize": 11,
5+
6+
"editor.semanticHighlighting.enabled": "configuredByTheme",
7+
8+
"editor.semanticTokenColorCustomizations": {
9+
"enabled": true,
10+
"rules": {
11+
"*.mutable":{
12+
"fontStyle": "",
13+
},
14+
"genericType": {
15+
"foreground": "#ff0000",
16+
"fontStyle": "bold"
17+
},
18+
"parameter":{
19+
"foreground": "#ff0000",
20+
"fontStyle": "bold"
21+
},
22+
"type": {
23+
"foreground": "#ab1844",
24+
"fontStyle": "bold"
25+
},
26+
"operator.unsafe": "#ff6600",
27+
"function.unsafe": "#ff6600",
28+
"method.unsafe": "#ff6600"
29+
}
30+
},
31+
"rust-analyzer.linkedProjects": [
32+
"./base/Cargo.toml"
33+
],
34+
}

0 commit comments

Comments
 (0)