@@ -14,16 +14,14 @@ const URL = "https://telemetry.lightpanda.io";
1414const MAX_BATCH_SIZE = 20 ;
1515
1616pub const LightPanda = struct {
17- pending : List ,
1817 running : bool ,
1918 thread : ? std.Thread ,
2019 allocator : Allocator ,
2120 mutex : std.Thread.Mutex ,
2221 cond : Thread.Condition ,
2322 connection : Http.Connection ,
24- node_pool : std .heap .MemoryPool (List .Node ),
25-
26- const List = std .DoublyLinkedList (LightPandaEvent );
23+ pending : std.DoublyLinkedList ,
24+ mem_pool : std .heap .MemoryPool (LightPandaEvent ),
2725
2826 pub fn init (app : * App ) ! LightPanda {
2927 const connection = try app .http .newConnection ();
@@ -41,7 +39,7 @@ pub const LightPanda = struct {
4139 .running = true ,
4240 .allocator = allocator ,
4341 .connection = connection ,
44- .node_pool = std .heap .MemoryPool (List . Node ).init (allocator ),
42+ .mem_pool = std .heap .MemoryPool (LightPandaEvent ).init (allocator ),
4543 };
4644 }
4745
@@ -53,15 +51,17 @@ pub const LightPanda = struct {
5351 self .cond .signal ();
5452 thread .join ();
5553 }
56- self .node_pool .deinit ();
54+ self .mem_pool .deinit ();
5755 self .connection .deinit ();
5856 }
5957
6058 pub fn send (self : * LightPanda , iid : ? []const u8 , run_mode : App.RunMode , raw_event : telemetry.Event ) ! void {
61- const event = LightPandaEvent {
59+ const event = try self .mem_pool .create ();
60+ event .* = .{
6261 .iid = iid ,
6362 .mode = run_mode ,
6463 .event = raw_event ,
64+ .node = .{},
6565 };
6666
6767 self .mutex .lock ();
@@ -70,24 +70,20 @@ pub const LightPanda = struct {
7070 self .thread = try std .Thread .spawn (.{}, run , .{self });
7171 }
7272
73- const node = try self .node_pool .create ();
74- errdefer self .node_pool .destroy (node );
75- node .data = event ;
76- self .pending .append (node );
73+ self .pending .append (& event .node );
7774 self .cond .signal ();
7875 }
7976
8077 fn run (self : * LightPanda ) void {
81- var arr : std .ArrayListUnmanaged (u8 ) = .{};
82- defer arr .deinit (self .allocator );
78+ var aw = std .Io .Writer .Allocating .init (self .allocator );
8379
84- var batch : [MAX_BATCH_SIZE ]LightPandaEvent = undefined ;
80+ var batch : [MAX_BATCH_SIZE ]* LightPandaEvent = undefined ;
8581 self .mutex .lock ();
8682 while (true ) {
8783 while (self .pending .first != null ) {
8884 const b = self .collectBatch (& batch );
8985 self .mutex .unlock ();
90- self .postEvent (b , & arr ) catch | err | {
86+ self .postEvent (b , & aw ) catch | err | {
9187 log .warn (.telemetry , "post error" , .{ .err = err });
9288 };
9389 self .mutex .lock ();
@@ -99,29 +95,30 @@ pub const LightPanda = struct {
9995 }
10096 }
10197
102- fn postEvent (self : * const LightPanda , events : []LightPandaEvent , arr : * std .ArrayListUnmanaged (u8 )) ! void {
103- defer arr .clearRetainingCapacity ();
104- var writer = arr .writer (self .allocator );
98+ fn postEvent (self : * LightPanda , events : []* LightPandaEvent , aw : * std.Io.Writer.Allocating ) ! void {
99+ defer for (events ) | e | {
100+ self .mem_pool .destroy (e );
101+ };
102+
103+ defer aw .clearRetainingCapacity ();
105104 for (events ) | event | {
106- try std .json .stringify (event , .{ .emit_null_optional_fields = false }, writer );
107- try writer .writeByte ('\n ' );
105+ try std .json .Stringify . value (event , .{ .emit_null_optional_fields = false }, & aw . writer );
106+ try aw . writer .writeByte ('\n ' );
108107 }
109108
110- try self .connection .setBody (arr .items );
109+ try self .connection .setBody (aw .written ());
110+ std .debug .print ("{s}\n " , .{aw .written ()});
111111 const status = try self .connection .request ();
112112
113113 if (status != 200 ) {
114114 log .warn (.telemetry , "server error" , .{ .status = status });
115115 }
116116 }
117117
118- fn collectBatch (self : * LightPanda , into : []LightPandaEvent ) []LightPandaEvent {
118+ fn collectBatch (self : * LightPanda , into : []* LightPandaEvent ) []* LightPandaEvent {
119119 var i : usize = 0 ;
120- const node_pool = & self .node_pool ;
121120 while (self .pending .popFirst ()) | node | {
122- into [i ] = node .data ;
123- node_pool .destroy (node );
124-
121+ into [i ] = @fieldParentPtr ("node" , node );
125122 i += 1 ;
126123 if (i == MAX_BATCH_SIZE ) {
127124 break ;
@@ -135,6 +132,7 @@ const LightPandaEvent = struct {
135132 iid : ? []const u8 ,
136133 mode : App.RunMode ,
137134 event : telemetry.Event ,
135+ node : std.DoublyLinkedList.Node ,
138136
139137 pub fn jsonStringify (self : * const LightPandaEvent , writer : anytype ) ! void {
140138 try writer .beginObject ();
0 commit comments