DEV Community

Eastspire
Eastspire

Posted on

🚀Build High-Performance Web Services with Hyperlane

In today’s digital era, building high-performance and reliable web services is the goal of every developer. Today, we sincerely recommend Hyperlane, a lightweight and high-performance Rust HTTP server framework designed to simplify your network service development.

Project Overview

Hyperlane is built entirely in pure Rust using the standard library, offering true cross-platform compatibility across Windows, Linux, and macOS—with a consistent API experience on all platforms. It leverages Tokio’s asynchronous runtime for seamless network communication without requiring platform-specific dependencies.

Key Features:

  • Supports HTTP request parsing, response building, and TCP communication.
  • Includes middleware support for both requests and responses, as well as WebSocket and Server-Sent Events (SSE) for flexible and efficient real-time communication.
  • A simple and user-friendly API design enables developers to quickly get started and build modern web services.

Quick Start

To begin using Hyperlane, simply add it to your project with the following command:

cargo add hyperlane
Enter fullscreen mode Exit fullscreen mode

We also provide a quick-start project, hyperlane-quick-start, to help you quickly set up and run your first Hyperlane application.

Clone the project:

git clone https://github.com/eastspire/hyperlane-quick-start.git
Enter fullscreen mode Exit fullscreen mode

Navigate to the project directory:

cd hyperlane-quick-start
Enter fullscreen mode Exit fullscreen mode

Run the project:

cargo run
Enter fullscreen mode Exit fullscreen mode

You can also run, stop, or restart the service in the background:

# Run in the background
cargo run -d

# Stop the service
cargo run stop

# Restart the service
cargo run restart

# Restart in the background
cargo run restart -d
Enter fullscreen mode Exit fullscreen mode

Usage Example

The following example demonstrates how to set up middleware, routing, and WebSocket support in a simple Hyperlane application:

use hyperlane::*;

async fn request_middleware(ctx: Context) {
    let socket_addr: String = ctx.get_socket_addr_or_default_string().await;
    ctx.set_response_header(SERVER, HYPERLANE)
        .await
        .set_response_header(CONNECTION, CONNECTION_KEEP_ALIVE)
        .await
        .set_response_header(CONTENT_TYPE, TEXT_PLAIN)
        .await
        .set_response_header("SocketAddr", socket_addr)
        .await;
}

async fn response_middleware(ctx: Context) {
    let _ = ctx.send().await;
}

async fn root_route(ctx: Context) {
    ctx.set_response_status_code(200)
        .await
        .set_response_body("Hello hyperlane => /")
        .await;
}

async fn websocket_route(ctx: Context) {
    let request_body: Vec<u8> = ctx.get_request_body().await;
    let _ = ctx.send_response_body(request_body).await;
}

fn error_handle(error: String) {
    eprintln!("{}", error);
    let _ = std::io::Write::flush(&mut std::io::stderr());
}

#[tokio::main]
async fn main() {
    let server: Server = Server::new();
    server.host("0.0.0.0").await;
    server.port(60000).await;
    server.enable_nodelay().await;
    server.disable_linger().await;
    server.http_line_buffer_size(4096).await;
    server.websocket_buffer_size(4096).await;
    server.error_handle(error_handle).await;
    server.request_middleware(request_middleware).await;
    server.response_middleware(response_middleware).await;
    server.route("/", root_route).await;
    server.route("/websocket", websocket_route).await;
    server
        .route("/test/:text", move |ctx: Context| async move {
            let param: RouteParams = ctx.get_route_params().await;
            panic!("Test panic {:?}", param);
        })
        .await;
    server.run().await.unwrap();
}
Enter fullscreen mode Exit fullscreen mode

Performance Benchmark

We conducted performance benchmarks on Hyperlane to demonstrate its excellent performance. The results below were obtained using wrk and ab tools.

wrk Benchmark

Command:

wrk -c360 -d60s http://127.0.0.1:60000/
Enter fullscreen mode Exit fullscreen mode

Results:

Rank Framework QPS
1 Tokio 340130.92
2 Hyperlane 324323.71
3 Rocket 298945.31
4 Rust Stdlib 291218.96
5 Gin (Go) 242570.16
6 Go Stdlib 234178.93
7 Node.js Stdlib 139412.13

ab Benchmark

Command:

ab -n 1000000 -c 1000 -r -k http://127.0.0.1:60000/
Enter fullscreen mode Exit fullscreen mode

Results:

Rank Framework QPS
1 Tokio 308596.26
2 Hyperlane 307568.90
3 Rocket 267931.52
4 Rust Stdlib 260514.56
5 Go Stdlib 226550.34
6 Gin (Go) 224296.16
7 Node.js Stdlib 85357.18

License and Contributions

Hyperlane is licensed under the MIT license. For details, please refer to the license file. Community contributions are welcome! You can participate by submitting issues or creating pull requests.

If you have any questions or need further assistance, feel free to contact the author: root@ltpp.vip.

Choose Hyperlane—choose the future of high performance and simplified development. Start your journey today and experience the speed and convenience of Hyperlane!

Top comments (0)