mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 16:28:58 +00:00
Share the HTTP client globally
This commit is contained in:
@@ -5,8 +5,8 @@ const build_info = @import("build_info");
|
||||
const Thread = std.Thread;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const App = @import("../app.zig").App;
|
||||
const telemetry = @import("telemetry.zig");
|
||||
const RunMode = @import("../app.zig").RunMode;
|
||||
|
||||
const log = std.log.scoped(.telemetry);
|
||||
const URL = "https://telemetry.lightpanda.io";
|
||||
@@ -19,11 +19,13 @@ pub const LightPanda = struct {
|
||||
allocator: Allocator,
|
||||
mutex: std.Thread.Mutex,
|
||||
cond: Thread.Condition,
|
||||
client: *std.http.Client,
|
||||
node_pool: std.heap.MemoryPool(List.Node),
|
||||
|
||||
const List = std.DoublyLinkedList(LightPandaEvent);
|
||||
|
||||
pub fn init(allocator: Allocator) !LightPanda {
|
||||
pub fn init(app: *App) !LightPanda {
|
||||
const allocator = app.allocator;
|
||||
return .{
|
||||
.cond = .{},
|
||||
.mutex = .{},
|
||||
@@ -31,6 +33,7 @@ pub const LightPanda = struct {
|
||||
.thread = null,
|
||||
.running = true,
|
||||
.allocator = allocator,
|
||||
.client = @ptrCast(&app.http_client),
|
||||
.uri = std.Uri.parse(URL) catch unreachable,
|
||||
.node_pool = std.heap.MemoryPool(List.Node).init(allocator),
|
||||
};
|
||||
@@ -47,7 +50,7 @@ pub const LightPanda = struct {
|
||||
self.node_pool.deinit();
|
||||
}
|
||||
|
||||
pub fn send(self: *LightPanda, iid: ?[]const u8, run_mode: RunMode, raw_event: telemetry.Event) !void {
|
||||
pub fn send(self: *LightPanda, iid: ?[]const u8, run_mode: App.RunMode, raw_event: telemetry.Event) !void {
|
||||
const event = LightPandaEvent{
|
||||
.iid = iid,
|
||||
.mode = run_mode,
|
||||
@@ -57,7 +60,7 @@ pub const LightPanda = struct {
|
||||
self.mutex.lock();
|
||||
defer self.mutex.unlock();
|
||||
if (self.thread == null) {
|
||||
self.thread = try std.Thread.spawn(.{}, run, .{self});
|
||||
self.thread = try std.Thread.spawn(.{ .stack_size = 1024 * 1024 * 4 }, run, .{self});
|
||||
}
|
||||
|
||||
const node = try self.node_pool.create();
|
||||
@@ -68,19 +71,16 @@ pub const LightPanda = struct {
|
||||
}
|
||||
|
||||
fn run(self: *LightPanda) void {
|
||||
const client = self.client;
|
||||
var arr: std.ArrayListUnmanaged(u8) = .{};
|
||||
var client = std.http.Client{ .allocator = self.allocator };
|
||||
|
||||
defer {
|
||||
arr.deinit(self.allocator);
|
||||
client.deinit();
|
||||
}
|
||||
defer arr.deinit(self.allocator);
|
||||
|
||||
self.mutex.lock();
|
||||
while (true) {
|
||||
while (self.pending.popFirst()) |node| {
|
||||
self.mutex.unlock();
|
||||
self.postEvent(&node.data, &client, &arr) catch |err| {
|
||||
self.postEvent(&node.data, client, &arr) catch |err| {
|
||||
log.warn("Telementry reporting error: {}", .{err});
|
||||
};
|
||||
self.mutex.lock();
|
||||
@@ -113,7 +113,7 @@ pub const LightPanda = struct {
|
||||
|
||||
const LightPandaEvent = struct {
|
||||
iid: ?[]const u8,
|
||||
mode: RunMode,
|
||||
mode: App.RunMode,
|
||||
event: telemetry.Event,
|
||||
|
||||
pub fn jsonStringify(self: *const LightPandaEvent, writer: anytype) !void {
|
||||
|
||||
@@ -3,9 +3,9 @@ const builtin = @import("builtin");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const App = @import("../app.zig").App;
|
||||
const Loop = @import("jsruntime").Loop;
|
||||
const uuidv4 = @import("../id.zig").uuidv4;
|
||||
const RunMode = @import("../app.zig").RunMode;
|
||||
|
||||
const log = std.log.scoped(.telemetry);
|
||||
const IID_FILE = "iid";
|
||||
@@ -25,11 +25,11 @@ fn TelemetryT(comptime P: type) type {
|
||||
|
||||
disabled: bool,
|
||||
|
||||
run_mode: RunMode,
|
||||
run_mode: App.RunMode,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn init(allocator: Allocator, run_mode: RunMode, app_dir_path: ?[]const u8) Self {
|
||||
pub fn init(app: *App, run_mode: App.RunMode) Self {
|
||||
const disabled = std.process.hasEnvVarConstant("LIGHTPANDA_DISABLE_TELEMETRY");
|
||||
if (builtin.mode != .Debug and builtin.is_test == false) {
|
||||
log.info("telemetry {s}", .{if (disabled) "disabled" else "enabled"});
|
||||
@@ -38,8 +38,8 @@ fn TelemetryT(comptime P: type) type {
|
||||
return .{
|
||||
.disabled = disabled,
|
||||
.run_mode = run_mode,
|
||||
.provider = try P.init(allocator),
|
||||
.iid = if (disabled) null else getOrCreateId(app_dir_path),
|
||||
.provider = try P.init(app),
|
||||
.iid = if (disabled) null else getOrCreateId(app.app_dir_path),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -104,32 +104,32 @@ pub const Event = union(enum) {
|
||||
};
|
||||
|
||||
const NoopProvider = struct {
|
||||
fn init(_: Allocator) !NoopProvider {
|
||||
fn init(_: *App) !NoopProvider {
|
||||
return .{};
|
||||
}
|
||||
fn deinit(_: NoopProvider) void {}
|
||||
pub fn send(_: NoopProvider, _: ?[]const u8, _: RunMode, _: Event) !void {}
|
||||
pub fn send(_: NoopProvider, _: ?[]const u8, _: App.RunMode, _: Event) !void {}
|
||||
};
|
||||
|
||||
extern fn setenv(name: [*:0]u8, value: [*:0]u8, override: c_int) c_int;
|
||||
extern fn unsetenv(name: [*:0]u8) c_int;
|
||||
|
||||
const testing = std.testing;
|
||||
const testing = @import("../testing.zig");
|
||||
test "telemetry: disabled by environment" {
|
||||
_ = setenv(@constCast("LIGHTPANDA_DISABLE_TELEMETRY"), @constCast(""), 0);
|
||||
defer _ = unsetenv(@constCast("LIGHTPANDA_DISABLE_TELEMETRY"));
|
||||
|
||||
const FailingProvider = struct {
|
||||
fn init(_: Allocator) !@This() {
|
||||
fn init(_: *App) !@This() {
|
||||
return .{};
|
||||
}
|
||||
fn deinit(_: @This()) void {}
|
||||
pub fn send(_: @This(), _: ?[]const u8, _: RunMode, _: Event) !void {
|
||||
pub fn send(_: @This(), _: ?[]const u8, _: App.RunMode, _: Event) !void {
|
||||
unreachable;
|
||||
}
|
||||
};
|
||||
|
||||
var telemetry = TelemetryT(FailingProvider).init(testing.allocator, .serve, null);
|
||||
var telemetry = TelemetryT(FailingProvider).init(undefined, .serve);
|
||||
defer telemetry.deinit();
|
||||
telemetry.record(.{ .run = {} });
|
||||
}
|
||||
@@ -141,7 +141,7 @@ test "telemetry: getOrCreateId" {
|
||||
|
||||
const id1 = getOrCreateId("/tmp/").?;
|
||||
const id2 = getOrCreateId("/tmp/").?;
|
||||
try testing.expectEqualStrings(&id1, &id2);
|
||||
try testing.expectEqual(&id1, &id2);
|
||||
|
||||
std.fs.cwd().deleteFile("/tmp/" ++ IID_FILE) catch {};
|
||||
const id3 = getOrCreateId("/tmp/").?;
|
||||
@@ -149,7 +149,10 @@ test "telemetry: getOrCreateId" {
|
||||
}
|
||||
|
||||
test "telemetry: sends event to provider" {
|
||||
var telemetry = TelemetryT(MockProvider).init(testing.allocator, .serve, "/tmp/");
|
||||
var app = testing.app(.{});
|
||||
defer app.deinit();
|
||||
|
||||
var telemetry = TelemetryT(MockProvider).init(app, .serve);
|
||||
defer telemetry.deinit();
|
||||
const mock = &telemetry.provider;
|
||||
|
||||
@@ -165,28 +168,28 @@ test "telemetry: sends event to provider" {
|
||||
|
||||
const MockProvider = struct {
|
||||
iid: ?[]const u8,
|
||||
run_mode: ?RunMode,
|
||||
run_mode: ?App.RunMode,
|
||||
allocator: Allocator,
|
||||
events: std.ArrayListUnmanaged(Event),
|
||||
|
||||
fn init(allocator: Allocator) !@This() {
|
||||
fn init(app: *App) !@This() {
|
||||
return .{
|
||||
.iid = null,
|
||||
.run_mode = null,
|
||||
.events = .{},
|
||||
.allocator = allocator,
|
||||
.allocator = app.allocator,
|
||||
};
|
||||
}
|
||||
fn deinit(self: *MockProvider) void {
|
||||
self.events.deinit(self.allocator);
|
||||
}
|
||||
pub fn send(self: *MockProvider, iid: ?[]const u8, run_mode: RunMode, events: Event) !void {
|
||||
pub fn send(self: *MockProvider, iid: ?[]const u8, run_mode: App.RunMode, events: Event) !void {
|
||||
if (self.iid == null) {
|
||||
try testing.expectEqual(null, self.run_mode);
|
||||
self.iid = iid.?;
|
||||
self.run_mode = run_mode;
|
||||
} else {
|
||||
try testing.expectEqualStrings(self.iid.?, iid.?);
|
||||
try testing.expectEqual(self.iid.?, iid.?);
|
||||
try testing.expectEqual(self.run_mode.?, run_mode);
|
||||
}
|
||||
try self.events.append(self.allocator, events);
|
||||
|
||||
Reference in New Issue
Block a user