CPU Profiling with perf
- Add the following settings to Cargo.toml
1 | [profile.release] |
Also run the code in release mode.
- Install perf + FlameGraph
1 | sudo pacman -Syu perf |
1 | [profile.release] |
Also run the code in release mode.
1 | sudo pacman -Syu perf |
Author: suikammd ihciah
An HTTP Server contains many internal components: protocol implementation (h1, h2, compression, and so on), connection state management (keepalive), request dispatching, middleware, business logic, and more. Users could implement all of these themselves, but everything other than the business logic is a fairly general capability. Decoupling these general capabilities from the user’s business logic gives us an HTTP framework.
Because hyper already provides a fairly complete HTTP protocol implementation in the Rust ecosystem, building an HTTP framework on hyper only requires providing capabilities such as routing, shared state, and middleware.
From the perspective of HTTP framework design, this article uses the new version of Axum as an example to analyze how an HTTP framework in Rust can provide sound abstractions and type constraints. Rust’s powerful type system allows us to write efficient and correct code.
The core data structure for concurrent programming in Golang is the channel. Its central idea is to share memory by communicating rather than communicate by sharing memory. Channels are indeed useful and avoid the need to write various complex communication primitives directly. Rust naturally has corresponding implementations as well, and Tokio’s channel implementation is extremely clever and well worth studying. This article briefly introduces the basic concepts of channels, then implements a minimal mpsc unbuffered channel, and finally examines Tokio’s channel implementation.
This section implements a minimal channel. This channel has an unlimited buffer, allows multiple writers, and never blocks writes to the channel. However, if the channel is empty, consumers attempting to consume data from it will be blocked. In other words, this is an mpsc unbuffered channel. This section refers to Chen Tian: Rust Programming First Course, Lesson 35. Chen Tian’s lesson is highly accessible and uses TDD to complete the entire channel design step by step, starting with interface test cases. I will take the same approach here, using TDD and aligning with the standard library’s mspc interface.
Most of this article is based on aysnc-book and explains how to implement a simple executor. The project is available at simple-executor.
Asynchronous programming in Rust involves three important concepts: Future, executor, and reactor. When an executor polls a Future, it passes a waker to the Future. The reactor then waits for the events involved in the Future and, when an event completes, uses this waker to wake the executor so that it can continue executing the Future.
The core of asynchronous programming in Rust is the Future trait. The Future trait has an associated type, Output, which is the result returned when the Future completes. The executor polls the Future step by step until it completes.
1 | pub trait Future { |
When a Future is submitted to the executor, the executor polls it. If it returns Poll::Ready(Result), the Future is finished. If it returns Poll::Pending, the Future needs to be submitted to the executor again the next time it can be polled. This is done by calling Waker’s wake method.
The Tokio library provides the copy_bidirectional method, which makes it easy to copy data bidirectionally for data structures that implement AsyncRead+AsyncWrite. In my previous article (which is still being revised), I implemented a simple proxy based on WebSocket, using tokio_tungstenite as the WebSocket library.
Originally, I customized a function similar to bidirectional copying on the client side instead of directly using copy_bidirectional. This was because the WebSocketStream built on TcpStream by tokio_tungstenite did not implement AsyncRead+AsyncWrite. In addition, the WebSocketStream returned by tokio_tungstenite does not expose convenient read and write methods; it only implements Sink and Stream. However, it provides a split method that separates the stream’s reads and writes. The split read half blindly writes the data it reads to TcpStream, while the split write half blindly writes data read from TcpStream to its own stream. The resulting problem is that when reads and writes are separated, there is no graceful way to detect a Close packet and exit the read and write flows described above. Here is a simple illustration of the original implementation:
The first step toward becoming familiar with a language might be writing a proxy with it. After studying Rust syntax for a long time, I still had little opportunity to use it at work, so I had to create a need for it myself. I hope to find a job writing Rust in the future, or one that gives me the freedom to learn new things.
The main purpose is to practice writing Rust. Rust is also safe and efficient.
Getting past the mysterious wall requires an encrypted connection; otherwise, it can easily get killed. A common approach, like the one used by Shadowsocks, is to define a custom encryption protocol, but that is rather troublesome. To keep things simple, I used TLS WebSocket directly for this purpose.
The benefits of connection pooling hardly need explaining. Eliminating connection establishment time makes the proxy faster.
The overall proxy flow looks like this:

Source code: https://github.com/suikammd/rust-proxy
A beginner’s first CTF experience. Many thanks to Hai for the patient guidance!
The problem categories included SQL injection, SSRF, XSS, privilege bypass, information disclosure, and Android. Most categories created by ByteDance contained multiple problems of progressively increasing difficulty. For example, in SQL injection, the first problem showed you the Golang source code, so you could directly see how Gorm concatenated strings. The later problems gradually revealed less information to participants.
Useful tools:
Rate limiters primarily prevent highly concurrent traffic from overwhelming a service. Common rate-limiting algorithms include counters, token buckets, and leaky buckets. A token bucket directly rejects requests when there are insufficient tokens, while a leaky bucket blocks requests when there are insufficient requests. This article implements a rate limiter that allows a moderate amount of burst traffic. This is just for fun, so please don’t criticize my crude implementation.
When I first started learning Go, what puzzled me most was passing slices to functions: had the slice changed or not? I recently discovered that some of my colleagues were not clear about this either, so I decided to take a closer look at whether modifying a slice passed to a function changes the original slice.
I’ll start with the structure of a slice. As you can see, a slice is actually backed by an array.
1 | type slice struct { |
Recently, I have often worked with ANTLR. At first, I only half understood it and got things to work through all kinds of blind trial and error. After finishing that work, I wanted to systematically understand what a compiler frontend actually does: what lexical analysis and syntax analysis do, as well as the syntax of ANTLR lexers and parsers.
From a fairly high-level perspective, a compiler frontend processes a string in two steps. The first step is lexical analysis, which parses the string into a sequence of tokens. The second step is syntax analysis, which matches the corresponding grammar rules according to the grammar.
Lexical analysis
Take the SPL query search id |stats count by id as an example. Lexical analysis parses this string into the token sequence search, id, stats, count, by, and |, while also obtaining the type corresponding to each token.
Syntax analysis
The result of lexical analysis serves as the input to the parser. After syntax analysis, the AST for this SPL query is determined.