@@ -15,26 +15,20 @@ const URL = "https://telemetry.lightpanda.io";
1515pub const LightPanda = struct {
1616 uri : std.Uri ,
1717 io : Client.IO ,
18- client : Client ,
1918 allocator : Allocator ,
2019 sending_pool : std .heap .MemoryPool (Sending ),
21- client_context_pool : std .heap .MemoryPool (Client .Ctx ),
2220
2321 pub fn init (allocator : Allocator , loop : * Loop ) ! LightPanda {
2422 return .{
2523 .allocator = allocator ,
2624 .io = Client .IO .init (loop ),
27- .client = .{ .allocator = allocator },
2825 .uri = std .Uri .parse (URL ) catch unreachable ,
2926 .sending_pool = std .heap .MemoryPool (Sending ).init (allocator ),
30- .client_context_pool = std .heap .MemoryPool (Client .Ctx ).init (allocator ),
3127 };
3228 }
3329
3430 pub fn deinit (self : * LightPanda ) void {
35- self .client .deinit ();
3631 self .sending_pool .deinit ();
37- self .client_context_pool .deinit ();
3832 }
3933
4034 pub fn send (self : * LightPanda , iid : ? []const u8 , run_mode : RunMode , event : Event ) ! void {
@@ -44,123 +38,113 @@ pub const LightPanda = struct {
4438 const resp_header_buffer = try arena .allocator ().alloc (u8 , 4096 );
4539 const body = try std .json .stringifyAlloc (arena .allocator (), .{
4640 .iid = iid ,
47- .run_mode = run_mode ,
41+ .driver = if (std .meta .activeTag (event ) == .navigate ) "cdp" else null ,
42+ .mode = run_mode ,
4843 .os = builtin .os .tag ,
49- .aarch = builtin .cpu .arch ,
44+ .arch = builtin .cpu .arch ,
5045 .version = build_info .git_commit ,
5146 .event = std .meta .activeTag (event ),
52- }, .{});
47+ }, .{ . emit_null_optional_fields = false });
5348
5449 const sending = try self .sending_pool .create ();
5550 errdefer self .sending_pool .destroy (sending );
5651
5752 sending .* = .{
5853 .body = body ,
5954 .arena = arena ,
55+ .ctx = undefined ,
6056 .lightpanda = self ,
61- .request = try self .client .create (.POST , self .uri , .{
62- .server_header_buffer = resp_header_buffer ,
63- }),
57+ .request = undefined ,
58+ .client = .{ .allocator = self .allocator },
6459 };
60+ sending .request = try sending .client .create (.POST , self .uri , .{
61+ .server_header_buffer = resp_header_buffer ,
62+ });
6563 errdefer sending .request .deinit ();
6664
67- const ctx = try self . client_context_pool . create ( );
68- errdefer self . client_context_pool . destroy ( ctx );
65+ sending . ctx = try Client . Ctx . init ( & self . io , & sending . request );
66+ errdefer sending . ctx . deinit ( );
6967
70- ctx .* = try Client .Ctx .init (& self .io , & sending .request );
71- ctx .userData = sending ;
72-
73- try self .client .async_open (
68+ try sending .client .async_open (
7469 .POST ,
7570 self .uri ,
7671 .{ .server_header_buffer = resp_header_buffer },
77- ctx ,
72+ & sending . ctx ,
7873 onRequestConnect ,
7974 );
8075 }
8176
82- fn handleError (sending : * Sending , ctx : * Client.Ctx , err : anyerror ) anyerror ! void {
77+ fn handleError (ctx : * Client.Ctx , err : anyerror ) anyerror ! void {
78+ const sending : * Sending = @fieldParentPtr ("ctx" , ctx );
8379 const lightpanda = sending .lightpanda ;
8480
85- ctx .deinit ();
86- lightpanda .client_context_pool .destroy (ctx );
87-
8881 sending .deinit ();
8982 lightpanda .sending_pool .destroy (sending );
9083 log .info ("request failure: {}" , .{err });
9184 }
9285
9386 fn onRequestConnect (ctx : * Client.Ctx , res : anyerror ! void ) anyerror ! void {
94- const sending : * Sending = @ptrCast ( @alignCast ( ctx . userData ) );
95- res catch | err | return handleError (sending , ctx , err );
87+ const sending : * Sending = @fieldParentPtr ( " ctx" , ctx );
88+ res catch | err | return handleError (ctx , err );
9689
9790 ctx .req .transfer_encoding = .{ .content_length = sending .body .len };
9891 return ctx .req .async_send (ctx , onRequestSend ) catch | err | {
99- return handleError (sending , ctx , err );
92+ return handleError (ctx , err );
10093 };
10194 }
10295
10396 fn onRequestSend (ctx : * Client.Ctx , res : anyerror ! void ) anyerror ! void {
104- const sending : * Sending = @ptrCast ( @alignCast ( ctx . userData ) );
105- res catch | err | return handleError (sending , ctx , err );
97+ const sending : * Sending = @fieldParentPtr ( " ctx" , ctx );
98+ res catch | err | return handleError (ctx , err );
10699
107100 return ctx .req .async_writeAll (sending .body , ctx , onRequestWrite ) catch | err | {
108- return handleError (sending , ctx , err );
101+ return handleError (ctx , err );
109102 };
110103 }
111104
112105 fn onRequestWrite (ctx : * Client.Ctx , res : anyerror ! void ) anyerror ! void {
113- const sending : * Sending = @ptrCast (@alignCast (ctx .userData ));
114- res catch | err | return handleError (sending , ctx , err );
106+ res catch | err | return handleError (ctx , err );
115107 return ctx .req .async_finish (ctx , onRequestFinish ) catch | err | {
116- return handleError (sending , ctx , err );
108+ return handleError (ctx , err );
117109 };
118110 }
119111
120112 fn onRequestFinish (ctx : * Client.Ctx , res : anyerror ! void ) anyerror ! void {
121- const sending : * Sending = @ptrCast (@alignCast (ctx .userData ));
122- res catch | err | return handleError (sending , ctx , err );
113+ res catch | err | return handleError (ctx , err );
123114 return ctx .req .async_wait (ctx , onRequestWait ) catch | err | {
124- return handleError (sending , ctx , err );
115+ return handleError (ctx , err );
125116 };
126117 }
127118
128119 fn onRequestWait (ctx : * Client.Ctx , res : anyerror ! void ) anyerror ! void {
129- const sending : * Sending = @ptrCast ( @alignCast ( ctx . userData ) );
130- res catch | err | return handleError (sending , ctx , err );
120+ const sending : * Sending = @fieldParentPtr ( " ctx" , ctx );
121+ res catch | err | return handleError (ctx , err );
131122
132123 const lightpanda = sending .lightpanda ;
133124
134125 defer {
135- ctx .deinit ();
136- lightpanda .client_context_pool .destroy (ctx );
137-
138126 sending .deinit ();
139127 lightpanda .sending_pool .destroy (sending );
140128 }
141129
142- var buffer : [2048 ]u8 = undefined ;
143- const reader = ctx .req .reader ();
144- while (true ) {
145- const n = reader .read (& buffer ) catch 0 ;
146- if (n == 0 ) {
147- break ;
148- }
149- }
150130 if (ctx .req .response .status != .ok ) {
151131 log .info ("invalid response: {d}" , .{@intFromEnum (ctx .req .response .status )});
152132 }
153133 }
154134};
155135
156136const Sending = struct {
137+ ctx : Client.Ctx ,
138+ client : Client ,
157139 body : []const u8 ,
158140 request : Client.Request ,
159141 lightpanda : * LightPanda ,
160142 arena : std.heap.ArenaAllocator ,
161143
162144 pub fn deinit (self : * Sending ) void {
145+ self .ctx .deinit ();
163146 self .arena .deinit ();
164147 self .request .deinit ();
148+ self .client .deinit ();
165149 }
166150};
0 commit comments