thinkingdata-core 0.1.1

Core implementation of the ThinkingData Analytics Rust SDK: data model, validation, and log/batch/debug consumers with blocking and async APIs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Copyright 2026 houseme
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The batch consumer: buffers events and uploads them to the receiver.
//!
//! Architecture (ported from the Go SDK, made async-native): `add` appends the
//! serialized event onto an in-memory buffer; a single sender task owns the
//! upload pipeline, which serializes concurrent flushes, retries transient
//! failures three times, keeps undelivered batches in a bounded cache, and
//! optionally auto-flushes on a timer.

use std::collections::VecDeque;
use std::io::Write;
use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;

use tokio::sync::{mpsc, oneshot};

use crate::data::Data;
use crate::error::{Result, TDError};
use crate::log::td_log_info;
use crate::utils::parse_time;

/// Event count threshold for triggering a flush.
pub const DEFAULT_BATCH_SIZE: usize = 20;
/// Hard upper bound for the batch size.
pub const MAX_BATCH_SIZE: usize = 200;
/// Request deadline in milliseconds.
pub const DEFAULT_TIMEOUT_MS: u64 = 30_000;
/// Auto flush spacing in seconds.
pub const DEFAULT_INTERVAL_SECS: u64 = 30;
/// Maximum number of cached (undelivered) batches.
pub const DEFAULT_CACHE_CAPACITY: usize = 50;

/// Configuration for [`BatchConsumer`], mirroring the Go SDK's
/// `TDBatchConfig`.
#[derive(Debug, Clone)]
pub struct BatchConsumerConfig {
    /// Receiver URL, e.g. `https://receiver.thinkingdata.io`. The path is
    /// always rewritten to `/sync_server`.
    pub server_url: String,
    /// ThinkingData APP ID.
    pub app_id: String,
    /// Event count threshold for triggering a flush; `0` selects
    /// [`DEFAULT_BATCH_SIZE`], values above [`MAX_BATCH_SIZE`] are clamped.
    pub batch_size: usize,
    /// Request deadline in milliseconds; `0` selects [`DEFAULT_TIMEOUT_MS`].
    pub timeout_ms: u64,
    /// Whether to gzip-compress request bodies (default: enabled).
    pub compress: bool,
    /// Whether a background task flushes on a fixed interval.
    pub auto_flush: bool,
    /// Auto flush spacing in seconds; `0` selects [`DEFAULT_INTERVAL_SECS`].
    pub interval_secs: u64,
    /// Maximum number of cached (undelivered) batches; `0` selects
    /// [`DEFAULT_CACHE_CAPACITY`].
    pub cache_capacity: usize,
}

impl Default for BatchConsumerConfig {
    fn default() -> Self {
        Self {
            server_url: String::new(),
            app_id: String::new(),
            batch_size: DEFAULT_BATCH_SIZE,
            timeout_ms: DEFAULT_TIMEOUT_MS,
            compress: true,
            auto_flush: false,
            interval_secs: DEFAULT_INTERVAL_SECS,
            cache_capacity: DEFAULT_CACHE_CAPACITY,
        }
    }
}

/// Buffered events waiting for delivery.
#[derive(Default)]
struct Queue {
    /// Serialized events not yet assigned to a batch.
    buffer: Vec<Vec<u8>>,
    /// Complete batches that failed delivery and will be retried.
    cache: VecDeque<Vec<Vec<u8>>>,
    /// Set when the consumer is closed; stops admission.
    closed: bool,
}

/// A request for the streaming sender task.
enum Request {
    Flush {
        all: bool,
        respond: oneshot::Sender<Result<()>>,
    },
    Close {
        respond: oneshot::Sender<Result<()>>,
    },
}

/// Shared state of the batch consumer.
struct Inner {
    server_url: String,
    app_id: String,
    compress: bool,
    batch_size: usize,
    cache_capacity: usize,
    client: reqwest::Client,
    queue: Mutex<Queue>,
    req_tx: mpsc::UnboundedSender<Request>,
    close_result: OnceLock<Result<()>>,
}

/// Buffer events and upload them in batches to the ThinkingData receiver over
/// HTTPS.
///
/// This is the Rust counterpart of the Go SDK's `TDBatchConsumer`. When the
/// buffer reaches `batch_size`, or when cached batches exist, the next add
/// (or an explicit `flush`) triggers a delivery. Delivered batches are
/// dropped; undelivered batches stay in a bounded cache and are retried by
/// subsequent flushes.
///
/// ```no_run
/// # use thinkingdata_core::consumer::{BatchConsumer, Consumer};
///
/// # #[cfg(feature = "blocking")]
/// # fn main() -> Result<(), thinkingdata_core::TDError> {
/// # let consumer = BatchConsumer::new("https://receiver.thinkingdata.io", "your_app_id")?;
/// # let data = thinkingdata_core::Data::new(thinkingdata_core::DataType::Track);
/// # consumer.add(&data)?;
/// # consumer.close()?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct BatchConsumer {
    inner: Arc<Inner>,
}

impl std::fmt::Debug for BatchConsumer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BatchConsumer")
            .field("server_url", &self.inner.server_url)
            .field("app_id", &self.inner.app_id)
            .field("batch_size", &self.inner.batch_size)
            .field("cache_capacity", &self.inner.cache_capacity)
            .field("compress", &self.inner.compress)
            .finish_non_exhaustive()
    }
}

impl BatchConsumer {
    /// Create a batch consumer with default settings (gzip compression
    /// enabled, batch size 20).
    pub fn new(server_url: &str, app_id: &str) -> Result<Self> {
        Self::with_config(BatchConsumerConfig {
            server_url: server_url.to_string(),
            app_id: app_id.to_string(),
            ..BatchConsumerConfig::default()
        })
    }

    /// Create a batch consumer with an explicit batch size.
    pub fn with_batch_size(server_url: &str, app_id: &str, batch_size: usize) -> Result<Self> {
        Self::with_config(BatchConsumerConfig {
            server_url: server_url.to_string(),
            app_id: app_id.to_string(),
            batch_size,
            ..BatchConsumerConfig::default()
        })
    }

    /// Create a batch consumer with explicit compression control.
    pub fn with_compress(server_url: &str, app_id: &str, compress: bool) -> Result<Self> {
        Self::with_config(BatchConsumerConfig {
            server_url: server_url.to_string(),
            app_id: app_id.to_string(),
            compress,
            ..BatchConsumerConfig::default()
        })
    }

    /// Create a batch consumer from a full configuration.
    pub fn with_config(config: BatchConsumerConfig) -> Result<Self> {
        if config.server_url.is_empty() {
            return Err(TDError::msg("ServerUrl not be empty"));
        }
        let mut server_url = url::Url::parse(&config.server_url)?;
        server_url.set_path("/sync_server");

        let batch_size = if config.batch_size > MAX_BATCH_SIZE {
            MAX_BATCH_SIZE
        } else if config.batch_size == 0 {
            DEFAULT_BATCH_SIZE
        } else {
            config.batch_size
        };
        let cache_capacity = if config.cache_capacity == 0 {
            DEFAULT_CACHE_CAPACITY
        } else {
            config.cache_capacity
        };
        let timeout_ms = if config.timeout_ms == 0 {
            DEFAULT_TIMEOUT_MS
        } else {
            config.timeout_ms
        };
        let interval_secs = if config.interval_secs == 0 {
            DEFAULT_INTERVAL_SECS
        } else {
            config.interval_secs
        };

        let client = reqwest::Client::builder()
            .timeout(Duration::from_millis(timeout_ms))
            .build()?;
        let (req_tx, req_rx) = mpsc::unbounded_channel::<Request>();

        let inner = Arc::new(Inner {
            server_url: server_url.to_string(),
            app_id: config.app_id.clone(),
            compress: config.compress,
            batch_size,
            cache_capacity,
            client,
            queue: Mutex::new(Queue::default()),
            req_tx,
            close_result: OnceLock::new(),
        });

        // Spawn the streaming sender task that owns all uploads.
        let task_inner = inner.clone();
        let auto_flush = config.auto_flush;
        let interval = Duration::from_secs(interval_secs);
        crate::consumer::shared_runtime().spawn(async move {
            sender_loop(task_inner, req_rx, auto_flush, interval).await;
        });

        td_log_info(&format!(
            "Mode: batch consumer, appId: {}, serverUrl: {}",
            config.app_id, inner.server_url
        ));

        Ok(Self { inner })
    }

    /// Enqueue one event; flushes when the buffer reaches the batch size or
    /// when cached batches are waiting for retry.
    async fn do_add(&self, data: &Data) -> Result<()> {
        let json = serde_json::to_vec(data)?;
        let enqueue_log = parse_time(&String::from_utf8_lossy(&json));
        let (ready, has_cache) = {
            let mut queue = self
                .inner
                .queue
                .lock()
                .expect("ThinkingData queue poisoned");
            if queue.closed {
                return Err(TDError::ConsumerClosed);
            }
            queue.buffer.push(json);
            (
                queue.buffer.len() >= self.inner.batch_size,
                !queue.cache.is_empty(),
            )
        };
        td_log_info(&format!("Enqueue event data: {enqueue_log}"));
        if ready || has_cache {
            self.do_flush_inner(false).await
        } else {
            Ok(())
        }
    }

    /// Request a delivery of one (or, with `all`, every) pending batch.
    async fn do_flush_inner(&self, all: bool) -> Result<()> {
        let (respond_tx, respond_rx) = oneshot::channel();
        self.inner
            .req_tx
            .send(Request::Flush {
                all,
                respond: respond_tx,
            })
            .map_err(|_| TDError::ConsumerClosed)?;
        respond_rx.await.map_err(|_| TDError::ConsumerClosed)?
    }

    /// Deliver every currently pending batch.
    pub async fn flush_all_async(&self) -> Result<()> {
        self.do_flush_inner(true).await
    }

    /// Stop admission, deliver every pending batch and shut the sender task
    /// down. Repeated calls return the same result and do not resend failed
    /// batches.
    async fn do_close(&self) -> Result<()> {
        if let Some(result) = self.inner.close_result.get() {
            return result.clone();
        }
        let (respond_tx, respond_rx) = oneshot::channel();
        self.inner
            .req_tx
            .send(Request::Close {
                respond: respond_tx,
            })
            .map_err(|_| TDError::ConsumerClosed)?;
        let result = respond_rx.await.map_err(|_| TDError::ConsumerClosed)?;
        let _ = self.inner.close_result.set(result.clone());
        result
    }
}

/// The streaming sender task: serializes all deliveries, runs the auto-flush
/// timer and processes close.
async fn sender_loop(
    inner: Arc<Inner>,
    mut req_rx: mpsc::UnboundedReceiver<Request>,
    auto_flush: bool,
    interval: Duration,
) {
    loop {
        let request = if auto_flush {
            tokio::select! {
                request = req_rx.recv() => request,
                _ = tokio::time::sleep(interval) => {
                    // Timer-triggered flush never surfaces errors to a caller.
                    let _ = flush_batches(&inner, false, false).await;
                    continue;
                }
            }
        } else {
            req_rx.recv().await
        };

        match request {
            None => return,
            Some(Request::Flush { all, respond }) => {
                let result = flush_batches(&inner, all, false).await;
                let _ = respond.send(result);
            }
            Some(Request::Close { respond }) => {
                {
                    let mut queue = inner.queue.lock().expect("ThinkingData queue poisoned");
                    queue.closed = true;
                }
                let result = flush_batches(&inner, true, true).await;
                let _ = inner.close_result.set(result.clone());
                let _ = respond.send(result);
                return;
            }
        }
    }
}

/// Move the buffer into the cache, deliver pending batches, and put failed
/// batches back (bounded by the cache capacity, keeping the newest).
async fn flush_batches(inner: &Arc<Inner>, all: bool, closing: bool) -> Result<()> {
    let pending: Vec<Vec<Vec<u8>>> = {
        let mut queue = inner.queue.lock().expect("ThinkingData queue poisoned");
        if !closing && queue.closed {
            return Err(TDError::ConsumerClosed);
        }
        if !queue.buffer.is_empty()
            && (all || queue.cache.is_empty() || queue.buffer.len() >= inner.batch_size)
        {
            let buffer = std::mem::take(&mut queue.buffer);
            queue.cache.push_back(buffer);
        }
        let count = if all {
            queue.cache.len()
        } else {
            queue.cache.len().min(1)
        };
        queue.cache.drain(..count).collect()
    };

    let mut first_err: Option<TDError> = None;
    let mut retained: Vec<Vec<Vec<u8>>> = Vec::new();
    for batch in pending {
        let (discard, err) = upload_batch(inner, &batch).await;
        if !discard {
            retained.push(batch);
        }
        if err.is_some() && first_err.is_none() {
            first_err = err;
        }
    }

    let mut queue = inner.queue.lock().expect("ThinkingData queue poisoned");
    // Re-add failed batches in their original order, ahead of newer batches.
    for batch in retained.into_iter().rev() {
        queue.cache.push_front(batch);
    }
    // Keep only the newest `cache_capacity` batches.
    while queue.cache.len() > inner.cache_capacity {
        queue.cache.pop_front();
    }
    first_err.map_or(Ok(()), Err)
}

/// Deliver one batch: gzip-encode (optional), POST with retries and interpret
/// the receiver's response code.
///
/// `discard` is true for every HTTP 200 response, preserving the deletion
/// policy of the Go SDK: batches acknowledged with a data error are dropped.
async fn upload_batch(inner: &Arc<Inner>, batch: &[Vec<u8>]) -> (bool, Option<TDError>) {
    let mut payload = Vec::new();
    payload.push(b'[');
    for (index, event) in batch.iter().enumerate() {
        if index > 0 {
            payload.push(b',');
        }
        payload.extend_from_slice(event);
    }
    payload.push(b']');
    let params = parse_time(&String::from_utf8_lossy(&payload));

    for attempt in 0..3 {
        let (status, code, err) = send_once(inner, &params, batch.len()).await;
        if status == 200 {
            if let Some(err) = err {
                return (true, Some(err));
            }
            return match code {
                Some(0) => {
                    td_log_info(&format!("send success: {params}"));
                    (true, None)
                }
                Some(1) | Some(-1) => (true, Some(TDError::msg("invalid data format"))),
                Some(-2) => (true, Some(TDError::msg("APP ID doesn't exist"))),
                Some(-3) => (true, Some(TDError::msg("invalid ip transmission"))),
                _ => (true, Some(TDError::msg("unknown error"))),
            };
        }
        // Transport errors surface immediately without a retry, matching Go.
        if let Some(err) = err {
            return (false, Some(err));
        }
        if attempt == 2 {
            return (
                false,
                Some(TDError::msg(format!(
                    "network error, but err is nil. Status code is: {status}"
                ))),
            );
        }
    }
    (false, None)
}

/// The receiver's response envelope: `{"code": 0}`.
#[derive(Debug, serde::Deserialize)]
struct BatchResponse {
    code: Option<i64>,
}

/// POST one request; returns `(status, code, error)`.
async fn send_once(
    inner: &Arc<Inner>,
    params: &str,
    size: usize,
) -> (u16, Option<i64>, Option<TDError>) {
    let body = if inner.compress {
        match gzip_encode(params.as_bytes()) {
            Ok(encoded) => encoded,
            Err(err) => return (0, None, Some(err)),
        }
    } else {
        params.as_bytes().to_vec()
    };

    let request = inner
        .client
        .post(&inner.server_url)
        .header("appid", &inner.app_id)
        .header("user-agent", "ta-rust-sdk")
        .header("version", crate::SDK_VERSION)
        .header("compress", if inner.compress { "gzip" } else { "none" })
        .header("TA-Integration-Type", crate::LIB_NAME)
        .header("TA-Integration-Version", crate::SDK_VERSION)
        .header("TA-Integration-Count", size.to_string())
        .header("Content-Type", "application/json")
        .body(body);

    let response = match request.send().await {
        Ok(response) => response,
        Err(err) => return (0, None, Some(err.into())),
    };
    let status = response.status().as_u16();
    if status != 200 {
        return (status, None, None);
    }

    let body = match response.text().await {
        Ok(body) => body,
        Err(err) => {
            return (
                status,
                Some(1),
                Some(TDError::msg(format!("read batch response: {err}"))),
            );
        }
    };
    let parsed: BatchResponse = match serde_json::from_str(&body) {
        Ok(parsed) => parsed,
        Err(err) => {
            return (
                status,
                Some(1),
                Some(TDError::msg(format!("decode batch response: {err}"))),
            );
        }
    };
    match parsed.code {
        Some(code) => (status, Some(code), None),
        None => (
            status,
            Some(1),
            Some(TDError::msg(
                "invalid batch response: code is missing or null",
            )),
        ),
    }
}

/// Gzip-compress a request body.
fn gzip_encode(data: &[u8]) -> Result<Vec<u8>> {
    let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
    encoder.write_all(data)?;
    encoder.finish().map_err(Into::into)
}

#[cfg(feature = "blocking")]
impl crate::consumer::Consumer for BatchConsumer {
    /// Enqueue one event, blocking until a triggered delivery finishes.
    fn add(&self, data: &Data) -> Result<()> {
        crate::consumer::shared_runtime().block_on(self.do_add(data))
    }

    /// Deliver the next pending batch, blocking until it completes.
    fn flush(&self) -> Result<()> {
        crate::consumer::shared_runtime().block_on(self.do_flush_inner(false))
    }

    /// Stop admission, deliver every pending batch and shut down. Repeated
    /// calls return the same result.
    fn close(&self) -> Result<()> {
        crate::consumer::shared_runtime().block_on(self.do_close())
    }
}

#[cfg(feature = "blocking")]
impl BatchConsumer {
    /// Deliver every currently pending batch.
    pub fn flush_all(&self) -> Result<()> {
        crate::consumer::shared_runtime().block_on(self.flush_all_async())
    }
}

#[cfg(feature = "async")]
#[async_trait::async_trait]
impl crate::consumer::AsyncConsumer for BatchConsumer {
    /// Enqueue one event; flushes when the buffer reaches the batch size or
    /// when cached batches are waiting for retry.
    async fn add_async(&self, data: &Data) -> Result<()> {
        self.do_add(data).await
    }

    /// Deliver the next pending batch.
    async fn flush_async(&self) -> Result<()> {
        self.do_flush_inner(false).await
    }

    /// Stop admission, deliver every pending batch and shut the sender task
    /// down. Repeated calls return the same result.
    async fn close_async(&self) -> Result<()> {
        self.do_close().await
    }
}