📝 document queued sink methods

This commit is contained in:
Nanaloveyuki
2026-06-13 22:42:41 +08:00
parent 86b99fe004
commit 0df565d454
5 changed files with 304 additions and 0 deletions
+4
View File
@@ -135,6 +135,10 @@ BitLogger API navigation.
- [buffered-sink-flush.md](./buffered-sink-flush.md)
- [queued-sink.md](./queued-sink.md)
- [queued-sink-type.md](./queued-sink-type.md)
- [queued-sink-pending-count.md](./queued-sink-pending-count.md)
- [queued-sink-dropped-count.md](./queued-sink-dropped-count.md)
- [queued-sink-drain.md](./queued-sink-drain.md)
- [queued-sink-flush.md](./queued-sink-flush.md)
- [queue-overflow-policy.md](./queue-overflow-policy.md)
- [filter-sink.md](./filter-sink.md)
- [filter-sink-type.md](./filter-sink-type.md)
+75
View File
@@ -0,0 +1,75 @@
---
name: queued-sink-drain
group: api
category: sink
update-time: 20260613
description: Drain queued records from a QueuedSink with an optional item limit.
key-word:
- sink
- queue
- drain
- public
---
## Queued-sink-drain
Drain queued records from a `QueuedSink[S]`. This is the direct sink-level queue delivery API when code owns a queued sink and wants controlled progress into the wrapped sink.
### Interface
```moonbit
pub fn[S : Sink] QueuedSink::drain(self : QueuedSink[S], max_items~ : Int = -1) -> Int {
```
#### input
- `self : QueuedSink[S]` - Queued sink whose pending records should be drained to the wrapped sink.
- `max_items : Int` - Optional upper bound on drained queued records. Negative values mean no explicit bound.
#### output
- `Int` - Number of drained records.
### Explanation
Detailed rules explaining key parameters and behaviors
- `max_items == 0` returns `0` immediately.
- Negative `max_items` means drain up to the current `pending_count()`.
- The method repeatedly pops from the queue and writes each record to the wrapped sink in order until the limit is reached or the queue becomes empty.
- The return value is the number of records actually drained.
### How to Use
Here are some specific examples provided.
#### When Need Bounded Queue Progress
When queued work should be advanced in chunks:
```moonbit
let drained = sink.drain(max_items=16)
```
In this example, callers cap how much queue backlog is forwarded in one step.
#### When Need Full Manual Drain
When a queued sink should be emptied explicitly:
```moonbit
ignore(sink.drain())
```
In this example, the queue is drained without an explicit item cap.
### Error Case
e.g.:
- If the queue is already empty, the method returns `0`.
- If callers only need the shorthand that drains the whole queue, `flush()` is the simpler API.
### Notes
1. Prefer this helper when queue progress should be bounded or directly observed.
2. Pair it with `pending_count()` to inspect remaining backlog after a drain step.
+76
View File
@@ -0,0 +1,76 @@
---
name: queued-sink-dropped-count
group: api
category: sink
update-time: 20260613
description: Read the cumulative dropped-record count from a QueuedSink.
key-word:
- sink
- queue
- dropped
- public
---
## Queued-sink-dropped-count
Read the cumulative dropped-record count from a `QueuedSink[S]`. This helper exposes the direct sink-level loss metric when a bounded queue overflows.
### Interface
```moonbit
pub fn[S] QueuedSink::dropped_count(self : QueuedSink[S]) -> Int {
```
#### input
- `self : QueuedSink[S]` - Queued sink whose cumulative dropped-record metric should be inspected.
#### output
- `Int` - Number of dropped records reported by the queue wrapper.
### Explanation
Detailed rules explaining key parameters and behaviors
- The counter value comes from `self.dropped_count.val`.
- It increases when the queue is full and an incoming record cannot be kept without applying overflow handling.
- The counter is cumulative for the lifetime of the concrete queued sink value.
- This helper does not explain why drops happened beyond exposing the count.
### How to Use
Here are some specific examples provided.
#### When Need Direct Queue Loss Visibility
When a bounded queue may discard records under pressure:
```moonbit
if sink.dropped_count() > 0 {
println("queue dropped records")
}
```
In this example, direct queue loss becomes visible without inspecting internal fields manually.
#### When Compare Overflow Tuning Changes
When queue capacity or overflow policy should be checked operationally:
```moonbit
ignore(sink.dropped_count())
```
In this example, callers can compare the cumulative drop metric across different queue settings.
### Error Case
e.g.:
- If the queue never overflows, this counter remains `0`.
- If callers need a higher-level wrapper around runtime-configured sinks, `RuntimeSink::dropped_count()` or `ConfiguredLogger::dropped_count()` may fit better.
### Notes
1. This helper reports cumulative loss, not the exact overflow moment.
2. Pair it with `pending_count()`, `drain()`, and queue configuration when investigating pressure.
+74
View File
@@ -0,0 +1,74 @@
---
name: queued-sink-flush
group: api
category: sink
update-time: 20260613
description: Flush a QueuedSink by draining its queued records into the wrapped sink.
key-word:
- sink
- queue
- flush
- public
---
## Queued-sink-flush
Flush a `QueuedSink[S]` by draining its queued records into the wrapped sink. This is the direct sink-level shorthand for draining the whole queue.
### Interface
```moonbit
pub fn[S : Sink] QueuedSink::flush(self : QueuedSink[S]) -> Int {
```
#### input
- `self : QueuedSink[S]` - Queued sink whose pending records should be fully drained to the wrapped sink.
#### output
- `Int` - Number of drained records.
### Explanation
Detailed rules explaining key parameters and behaviors
- This method delegates directly to `self.drain()`.
- It drains as many queued records as are currently pending.
- The return value is the number of records forwarded to the wrapped sink.
- This helper is the sink-level shorthand for a full manual queue drain.
### How to Use
Here are some specific examples provided.
#### When Need Full Queue Flush Semantics
When a queued sink should forward all pending records:
```moonbit
let flushed = sink.flush()
```
In this example, callers can observe how many queued records were forwarded.
#### When Need A Simpler Alternative To Explicit Drain Limits
When bounded draining is unnecessary and the whole queue should be released:
```moonbit
ignore(sink.flush())
```
In this example, the queue is drained completely without specifying `max_items`.
### Error Case
e.g.:
- If the queue is already empty, the method returns `0`.
- If callers need bounded progress instead of a full drain, `drain(max_items=...)` is the better API.
### Notes
1. Use this helper when code owns a `QueuedSink` directly and wants a full manual queue release.
2. This helper returns a count, unlike `BufferedSink::flush()`.
+75
View File
@@ -0,0 +1,75 @@
---
name: queued-sink-pending-count
group: api
category: sink
update-time: 20260613
description: Read the current queued-record count from a QueuedSink.
key-word:
- sink
- queue
- backlog
- public
---
## Queued-sink-pending-count
Read the current queued-record count from a `QueuedSink[S]`. This is the direct sink-level backlog metric for records that have been queued but not yet drained to the wrapped sink.
### Interface
```moonbit
pub fn[S] QueuedSink::pending_count(self : QueuedSink[S]) -> Int {
```
#### input
- `self : QueuedSink[S]` - Queued sink whose current pending backlog should be inspected.
#### output
- `Int` - Current number of queued records.
### Explanation
Detailed rules explaining key parameters and behaviors
- The return value is `self.queue.length()` at the time of the call.
- This is a point-in-time metric and may change immediately after it is read.
- It reflects queued records that have not yet been drained or flushed to the wrapped sink.
- This helper does not mutate the sink.
### How to Use
Here are some specific examples provided.
#### When Need Direct Queue Backlog Visibility
When code is working with a `QueuedSink` value directly and wants to inspect backlog pressure:
```moonbit
let pending = sink.pending_count()
```
In this example, callers can inspect the current queue size without draining it.
#### When Verify Drain Progress
When manual draining should be checked operationally:
```moonbit
ignore(sink.drain(max_items=8))
ignore(sink.pending_count())
```
In this example, the metric helps verify whether a drain step reduced queued backlog.
### Error Case
e.g.:
- This helper does not have a normal failure mode; it only reads current queue length.
- If callers only need simple threshold buffering instead of an explicit queue, `BufferedSink::pending_count()` may be the better API.
### Notes
1. Use this helper for direct visibility into queue backlog on `QueuedSink` values.
2. Pair it with `dropped_count()` when investigating queue pressure.