实战:Zig 编写高性能 Web 服务(2)

2024-06-06 09:20

本文主要是介绍实战:Zig 编写高性能 Web 服务(2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.1 编写 HTTP server

我们从python -m http.server 8000启动得到灵感,先确定好目标:

  • 编写一个HTTP/1.1 http server
  • zig version 0.12.0

使用zig init搭建项目的前置工作你先自行搭建好,不会的翻看前面铺垫的章节熟悉zig的项目结构。

关键文件build.zig:

const std = @import("std");// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {// Standard target options allows the person running `zig build` to choose// what target to build for. Here we do not override the defaults, which// means any target is allowed, and the default is native. Other options// for restricting supported target set are available.const target = b.standardTargetOptions(.{});// Standard optimization options allow the person running `zig build` to select// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not// set a preferred release mode, allowing the user to decide how to optimize.const optimize = b.standardOptimizeOption(.{});const exe = b.addExecutable(.{.name = "zig-http-server",.root_source_file = .{ .path = "src/main.zig" },.target = target,.optimize = optimize,// .use_llvm = false,// .use_lld = false,});const xev = b.dependency("libxev", .{ .target = target, .optimize = optimize });exe.root_module.addImport("xev", xev.module("xev"));// This declares intent for the executable to be installed into the// standard location when the user invokes the "install" step (the default// step when running `zig build`).b.installArtifact(exe);// This *creates* a Run step in the build graph, to be executed when another// step is evaluated that depends on it. The next line below will establish// such a dependency.const run_cmd = b.addRunArtifact(exe);// By making the run step depend on the install step, it will be run from the// installation directory rather than directly from within the cache directory.// This is not necessary, however, if the application depends on other installed// files, this ensures they will be present and in the expected location.run_cmd.step.dependOn(b.getInstallStep());// This allows the user to pass arguments to the application in the build// command itself, like this: `zig build run -- arg1 arg2 etc`if (b.args) |args| {run_cmd.addArgs(args);}// This creates a build step. It will be visible in the `zig build --help` menu,// and can be selected like this: `zig build run`// This will evaluate the `run` step rather than the default, which is "install".const run_step = b.step("run", "Run the app");run_step.dependOn(&run_cmd.step);
}

build.zig.zon文件:

.{.name = "zig-http-server",.version = "0.0.0",.dependencies = .{.libxev = .{.url = "https://codeload.github.com/mitchellh/libxev/tar.gz/b3f9918776b8700b337b7ebe769060328fe246b0",.hash = "122044caf67c7833c7110dc93531031899e459a6818ed125a0bcfdb0b5243bd7700b",},},.paths = .{"",},
}

马上开始我们的编程之旅,打开src/main.zig文件:

const std = @import("std");
const xev = @import("xev");const net = std.net;
const Allocator = std.mem.Allocator;const CompletionPool = std.heap.MemoryPoolExtra(xev.Completion, .{});
const ClientPool = std.heap.MemoryPoolExtra(Client, .{});

const xev = @import("xev");

我们采用了第三方的类库:mitchellh/libxev,libxev 是一个跨平台、高性能的事件循环库,提供了非阻塞 IO、定时器、事件等的抽象,并且能够在 Linux(io_uring 或 epoll)、macOS(kqueue)以及 Wasm + WASI 上运行。它既可以作为 Zig 语言的 API,也可以作为 C 语言的 API 使用。

我们需要建一个Client的结构体,代表一个网络客户端。这个客户端结构体包含了几个字段,如id(客户端ID)、socket(网络套接字)、loop(事件循环)、arena(内存分配器)、client_pool(客户端内存池)和completion_pool(完成操作内存池)。此外,它还有一个用于读取数据的缓冲区read_buf。

Client结构体还定义以下方法:

  • work:这个方法启动客户端的工作流程,它创建了一个Completion对象,并使用socket.read方法开始读取数据。
  • readCallback:这个回调函数在数据读取完成后被调用。它处理读取的结果,如果成功,则打印出读取的数据,并准备一个HTTP响应。然后,它使用socket.write方法发送响应。
  • writeCallback:这个回调函数在响应发送完成后被调用。它关闭套接字连接。
  • shutdownCallback:这个回调函数在套接字关闭后被调用。它调用socket.close方法来关闭套接字。
  • closeCallback:这个回调函数在套接字关闭完成后被调用。它清理资源,包括销毁Completion对象和Client对象本身。
  • destroy:这个方法用于手动销毁客户端资源。

这个Client结构体实现了一个简单的HTTP服务器,它能够读取客户端的请求,并返回一个包含"Hello, World!"消息的HTTP响应。每个客户端都有自己的内存分配器和内存池,用于管理内存分配和释放。当客户端连接关闭时,所有分配的内存都会被清理。代码片段如下:

const Client = struct {id: u32,socket: xev.TCP,loop: *xev.Loop,arena: std.heap.ArenaAllocator,client_pool: *ClientPool,completion_pool: *CompletionPool,read_buf: [4096]u8 = undefined,const Self = @This();pub fn work(self: *Self) void {const c_read = self.completion_pool.create() catch unreachable;self.socket.read(self.loop, c_read, .{ .slice = &self.read_buf }, Client, self, Client.readCallback);}pub fn readCallback(self_: ?*Client,l: *xev.Loop,c: *xev.Completion,s: xev.TCP,buf: xev.ReadBuffer,r: xev.TCP.ReadError!usize,) xev.CallbackAction {const self = self_.?;const n = r catch |err| {std.log.err("read error {any}", .{err});s.shutdown(l, c, Client, self, shutdownCallback);return .disarm;};const data = buf.slice[0..n];std.log.info("{s}", .{data});const httpOk =\\HTTP/1.1 200 OK\\Content-Type: text/plain\\Server: xev-http\\Content-Length: {d}\\Connection: close\\\\{s};const content_str =\\Hello, World! {d};const content = std.fmt.allocPrint(self.arena.allocator(), content_str, .{self.id}) catch unreachable;const res = std.fmt.allocPrint(self.arena.allocator(), httpOk, .{ content.len, content }) catch unreachable;self.socket.write(self.loop, c, .{ .slice = res }, Client, self, writeCallback);return .disarm;}fn writeCallback(self_: ?*Client,l: *xev.Loop,c: *xev.Completion,s: xev.TCP,buf: xev.WriteBuffer,r: xev.TCP.WriteError!usize,) xev.CallbackAction {_ = buf; // autofix_ = r catch unreachable;const self = self_.?;s.shutdown(l, c, Client, self, shutdownCallback);return .disarm;}fn shutdownCallback(self_: ?*Client,l: *xev.Loop,c: *xev.Completion,s: xev.TCP,r: xev.TCP.ShutdownError!void,) xev.CallbackAction {_ = r catch {};const self = self_.?;s.close(l, c, Client, self, closeCallback);return .disarm;}fn closeCallback(self_: ?*Client,l: *xev.Loop,c: *xev.Completion,socket: xev.TCP,r: xev.TCP.CloseError!void,) xev.CallbackAction {_ = l;_ = r catch unreachable;_ = socket;var self = self_.?;self.arena.deinit();self.completion_pool.destroy(c);self.client_pool.destroy(self);return .disarm;}pub fn destroy(self: *Self) void {self.arena.deinit();self.client_pool.destroy(self);}
};

 我们还需要定义名为Server的结构体,它代表了一个网络服务器。这个服务器结构体包含了几个字段,如loop(事件循环)、gpa(全局内存分配器)、completion_pool(完成操作内存池)、client_pool(客户端内存池)和conns(当前连接数)。

Server结构体定义了一个方法:

  • acceptCallback:这个回调函数在服务器接受到新的客户端连接时被调用。它创建了一个新的Client对象,并初始化它的各个字段,包括客户端ID、事件循环、网络套接字、内存分配器和内存池。然后,它调用client.work()方法来开始处理客户端请求。

这个回调函数的目的是接受新的客户端连接,并为每个连接创建一个Client实例来处理它。在创建了新的客户端实例后,它会增加conns计数器来记录当前的连接数。最后,它返回xev.CallbackAction.rearm,这意味着服务器会继续监听新的连接。

这个Server结构体实现了一个简单的网络服务器,它能够接受客户端连接,并为每个连接创建一个Client对象来处理通信。服务器使用内存池来管理客户端实例的内存分配,以提高性能和减少内存碎片。代码片段如下:

const Server = struct {loop: *xev.Loop,gpa: Allocator,completion_pool: *CompletionPool,client_pool: *ClientPool,conns: u32 = 0,fn acceptCallback(self_: ?*Server,l: *xev.Loop,// we ignore the completion, to keep the accept loop going for new connections_: *xev.Completion,r: xev.TCP.AcceptError!xev.TCP,) xev.CallbackAction {const self = self_.?;var client = self.client_pool.create() catch unreachable;client.* = Client{.id = self.conns,.loop = l,.socket = r catch unreachable,.arena = std.heap.ArenaAllocator.init(self.gpa),.client_pool = self.client_pool,.completion_pool = self.completion_pool,};client.work();self.conns += 1;return .rearm;}
};

1.2 进入main实现

main整体流程是:

  1. 初始化线程池和事件循环。
  2. 创建一个TCP套接字,并绑定到指定的地址和端口。
  3. 开始监听传入的连接。
  4. 初始化完成池和客户端池。
  5. 创建一个服务器结构体,包含所有必要的组件。
  6. 注册一个接受连接的回调函数。
  7. 运行事件循环,等待连接和处理事件。

http服务处理的就是多线程,所以我们借助了第三方类库xev来管理线程池。

var thread_pool = xev.ThreadPool.init(.{});defer thread_pool.deinit();defer thread_pool.shutdown();
  •  我们创建了一个xev.ThreadPool类型的变量thread_pool,并初始化它。xev是一个库,ThreadPool是这个库中的一个类型,用于管理线程池。
  • defer关键字用于注册一个函数,这个函数会在当前作用域结束时被调用。这里我们注册了thread_pool.deinit()和thread_pool.shutdown(),确保线程池在程序结束时被正确关闭和清理。
const port = 3000;
const addr = try net.Address.parseIp4("0.0.0.0", port);
var socket = try xev.TCP.init(addr);std.log.info("Listening on port {}", .{port});try socket.bind(addr);
try socket.listen(std.os.linux.SOMAXCONN);

绑定socket接口的方式,可以看出zig的简洁之道,和C一样。

socket.accept(&loop, c, Server, &server, Server.acceptCallback);

这行代码调用socket.accept方法,用于接受传入的连接。这个方法需要几个参数:

  • &loop:事件循环的引用,用于注册接受连接的事件。
  • c:完成事件的引用,用于在连接被接受时通知服务器。
  • Server:服务器结构体的类型,用于类型检查。
  • &server:服务器结构体的引用,用于在接受连接时传递给回调函数。
  • Server.acceptCallback:服务器结构体中的一个函数,当连接被接受时会被调用。
try loop.run(.until_done);

这行代码启动事件循环,并开始处理事件。run方法会阻塞当前线程,直到事件循环被关闭或者发生错误。.until_done是一个枚举值,表示事件循环应该运行直到所有任务都完成。

然后在项目直接运行zig build run就可以启动。main.zig完整代码如下:

const std = @import("std");
const xev = @import("xev");const net = std.net;
const Allocator = std.mem.Allocator;const CompletionPool = std.heap.MemoryPoolExtra(xev.Completion, .{});
const ClientPool = std.heap.MemoryPoolExtra(Client, .{});pub fn main() !void {var thread_pool = xev.ThreadPool.init(.{});defer thread_pool.deinit();defer thread_pool.shutdown();var loop = try xev.Loop.init(.{.entries = 4096,.thread_pool = &thread_pool,});defer loop.deinit();var gpa = std.heap.GeneralPurposeAllocator(.{}){};defer _ = gpa.deinit();const alloc = gpa.allocator();const port = 3000;const addr = try net.Address.parseIp4("0.0.0.0", port);var socket = try xev.TCP.init(addr);std.log.info("Listening on port {}", .{port});try socket.bind(addr);try socket.listen(std.os.linux.SOMAXCONN);var completion_pool = CompletionPool.init(alloc);defer completion_pool.deinit();var client_pool = ClientPool.init(alloc);defer client_pool.deinit();const c = try completion_pool.create();var server = Server{.loop = &loop,.gpa = alloc,.completion_pool = &completion_pool,.client_pool = &client_pool,};socket.accept(&loop, c, Server, &server, Server.acceptCallback);try loop.run(.until_done);
}const Client = struct {id: u32,socket: xev.TCP,loop: *xev.Loop,arena: std.heap.ArenaAllocator,client_pool: *ClientPool,completion_pool: *CompletionPool,read_buf: [4096]u8 = undefined,const Self = @This();pub fn work(self: *Self) void {const c_read = self.completion_pool.create() catch unreachable;self.socket.read(self.loop, c_read, .{ .slice = &self.read_buf }, Client, self, Client.readCallback);}pub fn readCallback(self_: ?*Client,l: *xev.Loop,c: *xev.Completion,s: xev.TCP,buf: xev.ReadBuffer,r: xev.TCP.ReadError!usize,) xev.CallbackAction {const self = self_.?;const n = r catch |err| {std.log.err("read error {any}", .{err});s.shutdown(l, c, Client, self, shutdownCallback);return .disarm;};const data = buf.slice[0..n];std.log.info("{s}", .{data});const httpOk =\\HTTP/1.1 200 OK\\Content-Type: text/plain\\Server: xev-http\\Content-Length: {d}\\Connection: close\\\\{s};const content_str =\\Hello, World! {d};const content = std.fmt.allocPrint(self.arena.allocator(), content_str, .{self.id}) catch unreachable;const res = std.fmt.allocPrint(self.arena.allocator(), httpOk, .{ content.len, content }) catch unreachable;self.socket.write(self.loop, c, .{ .slice = res }, Client, self, writeCallback);return .disarm;}fn writeCallback(self_: ?*Client,l: *xev.Loop,c: *xev.Completion,s: xev.TCP,buf: xev.WriteBuffer,r: xev.TCP.WriteError!usize,) xev.CallbackAction {_ = buf; // autofix_ = r catch unreachable;const self = self_.?;s.shutdown(l, c, Client, self, shutdownCallback);return .disarm;}fn shutdownCallback(self_: ?*Client,l: *xev.Loop,c: *xev.Completion,s: xev.TCP,r: xev.TCP.ShutdownError!void,) xev.CallbackAction {_ = r catch {};const self = self_.?;s.close(l, c, Client, self, closeCallback);return .disarm;}fn closeCallback(self_: ?*Client,l: *xev.Loop,c: *xev.Completion,socket: xev.TCP,r: xev.TCP.CloseError!void,) xev.CallbackAction {_ = l;_ = r catch unreachable;_ = socket;var self = self_.?;self.arena.deinit();self.completion_pool.destroy(c);self.client_pool.destroy(self);return .disarm;}pub fn destroy(self: *Self) void {self.arena.deinit();self.client_pool.destroy(self);}
};const Server = struct {loop: *xev.Loop,gpa: Allocator,completion_pool: *CompletionPool,client_pool: *ClientPool,conns: u32 = 0,fn acceptCallback(self_: ?*Server,l: *xev.Loop,// we ignore the completion, to keep the accept loop going for new connections_: *xev.Completion,r: xev.TCP.AcceptError!xev.TCP,) xev.CallbackAction {const self = self_.?;var client = self.client_pool.create() catch unreachable;client.* = Client{.id = self.conns,.loop = l,.socket = r catch unreachable,.arena = std.heap.ArenaAllocator.init(self.gpa),.client_pool = self.client_pool,.completion_pool = self.completion_pool,};client.work();self.conns += 1;return .rearm;}
};

1.3 运行效果:

1.3 学习总结

理解上面代码的关键是理解Zig中的基本概念,如defer、try、结构体、方法和事件循环的工作原理。同时,理解xev库的使用也很重要,因为本次http-server项目大量依赖于这个库的多线程管理能力。

这篇关于实战:Zig 编写高性能 Web 服务(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035703

相关文章

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

如何使用Maven创建web目录结构

《如何使用Maven创建web目录结构》:本文主要介绍如何使用Maven创建web目录结构的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录创建web工程第一步第二步第三步第四步第五步第六步第七步总结创建web工程第一步js通过Maven骨架创pytho