feat: readmes for rate limit and segmentation

This commit is contained in:
pablo 2025-09-14 20:02:18 +03:00
parent ef6add52be
commit ec07ec4096
No known key found for this signature in database
GPG Key ID: 78F35FCC60FDC63A
4 changed files with 83 additions and 0 deletions

1
.gitignore vendored
View File

@ -26,6 +26,7 @@ tests/*
!*.nim
ratelimit/*
!*.nim
!*.md
!*.proto
nimble.develop
nimble.paths

View File

@ -18,6 +18,11 @@ make build-go # Verify Go bindings compile
make run-go-example
```
## Modules
[Rate limit](ratelimit/README.md)
[Segmentation](ratelimit/README.segmentation.md)
## TODO
- [ ] Follow closer the integrations from [this repo](https://github.com/logos-co/nim-c-library-guide)

32
README.segmentation.md Normal file
View File

@ -0,0 +1,32 @@
# Message Segmentation
Split large payloads into Waku-sized segments with optional ReedSolomon parity for robust reconstruction. Inbound segmented messages are reassembled and persisted until complete.
## Features
- **Segmentation**: Outbound payloads over the configured size are split into segments; smaller payloads pass through unchanged.
- **Reconstruction**: Inbound segments can be processed out of order and reassembled into the original message.
- **Erasure coding**: Adds parity segments at 12.5% of data segments (≤ SegmentsReedsolomonMaxCount), enabling recovery with ~87.5% of segments.
- **Persistence**: Segments and completion state are stored (SQLite) and survive restarts.
- **API simplicity**: Main parameter is the maximum segment size; inject your persistence object.
## Essential API
- **Types**: `SegmentationHander`, `Chunk`, `Message`
- **Outbound**: `segmentMessage(handler, chunk: Chunk): Result[seq[Chunk], string]`
- **Inbound**: `handleSegmentationLayer(handler, message: var Message): Result[void, string]`
- **Helpers**: `isParityMessage(segment: SegmentMessage): bool`
- **Parameters**: `segmentSize` (bytes); provide `SegmentationPersistence` instance
Notes
- Parity rate is 12.5% (`SegmentsParityRate = 0.125`).
## Wire format
Segments are protobuf-encoded (`segment_message.proto`):
## Tests
See `tests/test_segmentation.nim` for examples covering in-order/out-of-order and parity recovery paths.

45
ratelimit/README.md Normal file
View File

@ -0,0 +1,45 @@
# Rate-Limited Message Manager (Nim)
Rate limiter for sending messages to a provided delivery service. Implements a token-bucket per epoch, queues by priority, and persists queue and quota across restarts. Intended to provide a good UX for sending messages in an RLN-protected network, so that developers can queue, prioritize and inform end users when quotas might be close to being consumed.
## Features
- **Rate limit per epoch**: Fixed-capacity token bucket (messages per epoch, RLN-style window).
- **Priorities**: **Critical**, **Normal**, **Optional**.
- **Low/empty quota behavior**:
- Low quota: Critical sent, Normal queued, Optional dropped.
- Exhausted: Critical queued, Normal queued, Optional dropped.
- **Batch all-or-none**: Pass messages as a batch; either the whole batch is sent, queued, or dropped.
- **Status tracking**: `PassedToSender`, `Enqueued`, `Dropped`, `DroppedBatchTooLarge`, `DroppedFailedToEnqueue`.
- **Persistence**: Queues and quota state survive restarts (SQLite).
## Install
- Built as part of this Nimble project. Add this repo as a dependency or develop locally, then import `ratelimit/ratelimit_manager` and `ratelimit/store`.
## API (essentials)
- **Create**: `await RateLimitStore[T].new(db)`; `await RateLimitManager[T].new(store, sender, capacity, duration, sleepDuration)`
- **Run**: `await manager.start()` / `await manager.stop()`
- **Send**: `await manager.sendOrEnqueue(@[(msgId, msg), ...], Priority)`
- **Quota**: `manager.getQuota()``(remaining, total)`
- **Message Status**: `await manager.getMessageStatus(msgId)`
Notes:
- Batches larger than 30% of total capacity are dropped to prevent starvation (`DroppedBatchTooLarge`).
- Sender is user-provided. Message outcome is recorded via `MessageStatus`.
## Persistence
Backed by SQLite. Migrations create the following tables:
- `kv_store` (stores bucket state)
- `ratelimit_queues` (persisted batches per priority)
- `ratelimit_message_status` (latest status per `msg_id`)
Apply migrations via `chat_sdk/migration.runMigrations(db)`.
## Tests
See `tests/test_ratelimit_manager.nim` for usage and expected behaviors.