mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 04:34:44 +00:00
mcp: optimize memory re-use and add thread safety to Server.sendResponse
This commit is contained in:
@@ -20,6 +20,8 @@ session: *lp.Session,
|
|||||||
page: *lp.Page,
|
page: *lp.Page,
|
||||||
|
|
||||||
writer: *std.io.Writer,
|
writer: *std.io.Writer,
|
||||||
|
mutex: std.Thread.Mutex = .{},
|
||||||
|
aw: std.io.Writer.Allocating,
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator, app: *App, writer: *std.io.Writer) !*Self {
|
pub fn init(allocator: std.mem.Allocator, app: *App, writer: *std.io.Writer) !*Self {
|
||||||
const self = try allocator.create(Self);
|
const self = try allocator.create(Self);
|
||||||
@@ -28,6 +30,7 @@ pub fn init(allocator: std.mem.Allocator, app: *App, writer: *std.io.Writer) !*S
|
|||||||
self.allocator = allocator;
|
self.allocator = allocator;
|
||||||
self.app = app;
|
self.app = app;
|
||||||
self.writer = writer;
|
self.writer = writer;
|
||||||
|
self.aw = .init(allocator);
|
||||||
|
|
||||||
self.http_client = try app.http.createClient(allocator);
|
self.http_client = try app.http.createClient(allocator);
|
||||||
errdefer self.http_client.deinit();
|
errdefer self.http_client.deinit();
|
||||||
@@ -45,6 +48,7 @@ pub fn init(allocator: std.mem.Allocator, app: *App, writer: *std.io.Writer) !*S
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
|
self.aw.deinit();
|
||||||
self.browser.deinit();
|
self.browser.deinit();
|
||||||
self.notification.deinit();
|
self.notification.deinit();
|
||||||
self.http_client.deinit();
|
self.http_client.deinit();
|
||||||
@@ -53,11 +57,13 @@ pub fn deinit(self: *Self) void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn sendResponse(self: *Self, response: anytype) !void {
|
pub fn sendResponse(self: *Self, response: anytype) !void {
|
||||||
var aw: std.io.Writer.Allocating = .init(self.allocator);
|
self.mutex.lock();
|
||||||
defer aw.deinit();
|
defer self.mutex.unlock();
|
||||||
try std.json.Stringify.value(response, .{ .emit_null_optional_fields = false }, &aw.writer);
|
|
||||||
try aw.writer.writeByte('\n');
|
self.aw.clearRetainingCapacity();
|
||||||
try self.writer.writeAll(aw.writer.buffered());
|
try std.json.Stringify.value(response, .{ .emit_null_optional_fields = false }, &self.aw.writer);
|
||||||
|
try self.aw.writer.writeByte('\n');
|
||||||
|
try self.writer.writeAll(self.aw.writer.buffered());
|
||||||
try self.writer.flush();
|
try self.writer.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user