@@ -10,6 +10,7 @@ const telemetry = @import("telemetry.zig");
1010
1111const log = std .log .scoped (.telemetry );
1212const URL = "https://telemetry.lightpanda.io" ;
13+ const MAX_BATCH_SIZE = 20 ;
1314
1415pub const LightPanda = struct {
1516 uri : std.Uri ,
@@ -73,18 +74,18 @@ pub const LightPanda = struct {
7374 fn run (self : * LightPanda ) void {
7475 const client = self .client ;
7576 var arr : std .ArrayListUnmanaged (u8 ) = .{};
76-
7777 defer arr .deinit (self .allocator );
7878
79+ var batch : [MAX_BATCH_SIZE ]LightPandaEvent = undefined ;
7980 self .mutex .lock ();
8081 while (true ) {
81- while (self .pending .popFirst ()) | node | {
82+ while (self .pending .first != null ) {
83+ const b = self .collectBatch (& batch );
8284 self .mutex .unlock ();
83- self .postEvent (& node . data , client , & arr ) catch | err | {
85+ self .postEvent (b , client , & arr ) catch | err | {
8486 log .warn ("Telementry reporting error: {}" , .{err });
8587 };
8688 self .mutex .lock ();
87- self .node_pool .destroy (node );
8889 }
8990 if (self .running == false ) {
9091 return ;
@@ -93,9 +94,13 @@ pub const LightPanda = struct {
9394 }
9495 }
9596
96- fn postEvent (self : * const LightPanda , event : * const LightPandaEvent , client : * std.http.Client , arr : * std .ArrayListUnmanaged (u8 )) ! void {
97+ fn postEvent (self : * const LightPanda , events : [] LightPandaEvent , client : * std.http.Client , arr : * std .ArrayListUnmanaged (u8 )) ! void {
9798 defer arr .clearRetainingCapacity ();
98- try std .json .stringify (event , .{ .emit_null_optional_fields = false }, arr .writer (self .allocator ));
99+ var writer = arr .writer (self .allocator );
100+ for (events ) | event | {
101+ try std .json .stringify (event , .{ .emit_null_optional_fields = false }, writer );
102+ try writer .writeByte ('\n ' );
103+ }
99104
100105 var response_header_buffer : [2048 ]u8 = undefined ;
101106 const result = try client .fetch (.{
@@ -109,8 +114,24 @@ pub const LightPanda = struct {
109114 log .warn ("server error status: {}" , .{result .status });
110115 }
111116 }
117+
118+ fn collectBatch (self : * LightPanda , into : []LightPandaEvent ) []LightPandaEvent {
119+ var i : usize = 0 ;
120+ const node_pool = & self .node_pool ;
121+ while (self .pending .popFirst ()) | node | {
122+ into [i ] = node .data ;
123+ node_pool .destroy (node );
124+
125+ i += 1 ;
126+ if (i == MAX_BATCH_SIZE ) {
127+ break ;
128+ }
129+ }
130+ return into [0.. i ];
131+ }
112132};
113133
134+
114135const LightPandaEvent = struct {
115136 iid : ? []const u8 ,
116137 mode : App.RunMode ,
0 commit comments