mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
Endpoint example: /stop endpoint & leak detection
This commit is contained in:
parent
b4a23e1000
commit
28b90c7b00
2 changed files with 74 additions and 34 deletions
|
@ -1,6 +1,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const zap = @import("zap");
|
const zap = @import("zap");
|
||||||
const Endpoint = @import("endpoint.zig");
|
const Endpoint = @import("endpoint.zig");
|
||||||
|
const StopEndpoint = @import("stopendpoint.zig");
|
||||||
|
|
||||||
// this is just to demo that we can catch arbitrary slugs
|
// this is just to demo that we can catch arbitrary slugs
|
||||||
fn on_request(r: zap.SimpleRequest) void {
|
fn on_request(r: zap.SimpleRequest) void {
|
||||||
|
@ -17,44 +18,54 @@ pub fn main() !void {
|
||||||
}){};
|
}){};
|
||||||
var allocator = gpa.allocator();
|
var allocator = gpa.allocator();
|
||||||
|
|
||||||
// setup listener
|
// we scope everything that can allocate within this block for leak detection
|
||||||
var listener = zap.SimpleEndpointListener.init(
|
{
|
||||||
allocator,
|
// setup listener
|
||||||
.{
|
var listener = zap.SimpleEndpointListener.init(
|
||||||
.port = 3000,
|
allocator,
|
||||||
.on_request = on_request,
|
.{
|
||||||
.log = true,
|
.port = 3000,
|
||||||
.public_folder = "examples/endpoint/html",
|
.on_request = on_request,
|
||||||
.max_clients = 100000,
|
.log = true,
|
||||||
.max_body_size = 100 * 1024 * 1024,
|
.public_folder = "examples/endpoint/html",
|
||||||
},
|
.max_clients = 100000,
|
||||||
);
|
.max_body_size = 100 * 1024 * 1024,
|
||||||
defer listener.deinit();
|
},
|
||||||
|
);
|
||||||
|
defer listener.deinit();
|
||||||
|
|
||||||
var endpoint = Endpoint.init(allocator, "/users");
|
var endpoint = Endpoint.init(allocator, "/users");
|
||||||
defer endpoint.deinit();
|
defer endpoint.deinit();
|
||||||
|
|
||||||
// add endpoint
|
var stopEp = StopEndpoint.init("/stop");
|
||||||
try listener.addEndpoint(endpoint.getUserEndpoint());
|
|
||||||
|
|
||||||
// fake some users
|
// add endpoint
|
||||||
var uid: usize = undefined;
|
try listener.addEndpoint(endpoint.getUserEndpoint());
|
||||||
uid = try endpoint.getUsers().addByName("renerocksai", null);
|
try listener.addEndpoint(stopEp.getEndpoint());
|
||||||
uid = try endpoint.getUsers().addByName("renerocksai", "your mom");
|
|
||||||
|
|
||||||
// listen
|
// fake some users
|
||||||
try listener.listen();
|
var uid: usize = undefined;
|
||||||
|
uid = try endpoint.getUsers().addByName("renerocksai", null);
|
||||||
|
uid = try endpoint.getUsers().addByName("renerocksai", "your mom");
|
||||||
|
|
||||||
std.debug.print("Listening on 0.0.0.0:3000\n", .{});
|
// listen
|
||||||
|
try listener.listen();
|
||||||
|
|
||||||
// and run
|
std.debug.print("Listening on 0.0.0.0:3000\n", .{});
|
||||||
zap.start(.{
|
|
||||||
.threads = 2000,
|
// and run
|
||||||
// IMPORTANT! It is crucial to only have a single worker for this example to work!
|
zap.start(.{
|
||||||
// Multiple workers would have multiple copies of the users hashmap.
|
.threads = 2000,
|
||||||
//
|
// IMPORTANT! It is crucial to only have a single worker for this example to work!
|
||||||
// Since zap is quite fast, you can do A LOT with a single worker.
|
// Multiple workers would have multiple copies of the users hashmap.
|
||||||
// Try it with `zig build run-endpoint -Drelease-fast`
|
//
|
||||||
.workers = 1,
|
// Since zap is quite fast, you can do A LOT with a single worker.
|
||||||
});
|
// Try it with `zig build run-endpoint -Drelease-fast`
|
||||||
|
.workers = 1,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// show potential memory leaks when ZAP is shut down
|
||||||
|
const has_leaked = gpa.detectLeaks();
|
||||||
|
std.log.debug("Has leaked: {}\n", .{has_leaked});
|
||||||
}
|
}
|
||||||
|
|
29
examples/endpoint/stopendpoint.zig
Normal file
29
examples/endpoint/stopendpoint.zig
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const zap = @import("zap");
|
||||||
|
|
||||||
|
/// A simple endpoint listening on the /stop route that shuts down zap
|
||||||
|
/// the main thread usually continues at the instructions after the call to zap.start().
|
||||||
|
pub const Self = @This();
|
||||||
|
|
||||||
|
endpoint: zap.SimpleEndpoint = undefined,
|
||||||
|
|
||||||
|
pub fn init(
|
||||||
|
path: []const u8,
|
||||||
|
) Self {
|
||||||
|
return .{
|
||||||
|
.endpoint = zap.SimpleEndpoint.init(.{
|
||||||
|
.path = path,
|
||||||
|
.get = get,
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getEndpoint(self: *Self) *zap.SimpleEndpoint {
|
||||||
|
return &self.endpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
|
_ = e;
|
||||||
|
_ = r;
|
||||||
|
zap.stop();
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue