Closed hajsf closed 11 months ago
This is the type of thing the context is there for.
As for this specific implementation, I would probably make the context's allocator
an Allocator
instead of an *Allocator
. This is true in general, but particularly here where it's already behind a pointer (*Context
).
You might also consider giving each handler a buf: []u8
to re-use. That way you aren't allocating and freeing memory all the time. While this can add a lot of performance, you need to figure out how to deal with messages that don't fit inbuf
.
const Handler = struct {
buf: []u8
ctx: *Context
fn init(...) Handler {
return .{.buf = try ctx.allocator.alloc(u8, 1024)};
}
pub fn close(h: *Handler) void {
h.ctx.allocator.free(h.buf);
}
...
}
Then you can use bufPrint
Thanks a lot.
Hi, in my server I want to return a custom text not just echo back, something like changing the:
to
Here I have to use memory allocator, and was thinking about the best practice, the thoughts came to my mind are:
handle
function and use itI defined the
server.zig
as below and it is working fine, but liked to hear your thoughts as well:Thanks