From 343f479127a67798e8c25b0a2f81581dbedba7ab Mon Sep 17 00:00:00 2001 From: dimgigov Date: Mon, 18 May 2026 19:41:21 +0300 Subject: [PATCH] docs(bg,en): fix ports and WebSocket example for new server model - Update architecture diagram to include WebSocket server (port 9913) - Fix HTTP port from 9470 to 9912 (TCP + 440) in protocol docs - Fix WebSocket port from 9471 to 9913 (TCP + 441) in protocol docs - Fix WebSocket Nim example: remove non-existent onMessage API and unsafe cast[string](data), use correct WsServer API with safe helpers - Clarify TCP_NODELAY is also handled internally by hunos (HTTP) - Note that bytesToString/stringToBytes are wire-protocol helpers --- docs/bg/architecture.md | 15 ++++++++++----- docs/bg/protocol.md | 6 +++--- docs/en/architecture.md | 15 ++++++++++----- docs/en/protocol.md | 22 +++++++++++++--------- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/docs/bg/architecture.md b/docs/bg/architecture.md index 04af52b..39501d7 100644 --- a/docs/bg/architecture.md +++ b/docs/bg/architecture.md @@ -45,15 +45,20 @@ TCP и HTTP сървърите споделят един LSMTree инстанс ├────────────────────────┬────────────────────────────────┤ │ TCP Сървър │ HTTP Сървър │ │ (Бинарен Протокол) │ (REST API) │ -│ Порт: 9472 │ Порт: 9912 │ +│ Порт: 9472 │ Порт: 9912 (TCP + 440) │ │ TCP_NODELAY: ВКЛ │ Multi-threaded │ -└────────────────────────┴────────────────────────────────┘ +├────────────────────────┴────────────────────────────────┤ +│ WebSocket Сървър │ +│ (Streaming/Pub-Sub) │ +│ Порт: 9913 (TCP + 441) │ +│ TCP_NODELAY: ВКЛ │ +└─────────────────────────────────────────────────────────┘ ``` **Ключови оптимизации:** -- **Споделен LSMTree** — И двата сървъра работят върху един и същ database инстанс, елиминирайки несъответствие в данните -- **TCP_NODELAY** — Включен както на слушащия, така и на клиентските сокети за по-ниска латентност при малки съобщения -- **Безопасна конверсия на байтове** — Правилни `bytesToString`/`stringToBytes` функции вместо unsafe `cast` операции +- **Споделен LSMTree** — Всички сървъри работят върху един и същ database инстанс, елиминирайки несъответствие в данните +- **TCP_NODELAY** — Включен на слушащите и клиентските сокети в TCP/WebSocket сървърите за по-ниска латентност при малки съобщения. HTTP сървърът (чрез hunos) също управлява `TCP_NODELAY` вътрешно. +- **Безопасна конверсия на байтове** — Правилни `bytesToString`/`stringToBytes` функции вместо unsafe `cast` операции във wire протокола ### Управление на Връзки diff --git a/docs/bg/protocol.md b/docs/bg/protocol.md index 66057e6..e7be381 100644 --- a/docs/bg/protocol.md +++ b/docs/bg/protocol.md @@ -110,7 +110,7 @@ BaraDB поддържа множество протоколи за клиент ## HTTP/REST API -Базов URL: `http://localhost:9470/api/v1` +Базов URL: `http://localhost:9912/api/v1` (HTTP порт = TCP порт + 440) ### Rate Limiting @@ -271,7 +271,7 @@ POST /admin/check ## WebSocket Протокол -URL: `ws://localhost:9471` +URL: `ws://localhost:9913` (WebSocket порт = TCP порт + 441) ### Формат на Frame @@ -301,7 +301,7 @@ WebSocket текстови frame-ове съдържат JSON съобщения ### Pub/Sub Пример ```javascript -const ws = new WebSocket('ws://localhost:9471'); +const ws = new WebSocket('ws://localhost:9913'); ws.onopen = () => { // Абониране за промени в таблица diff --git a/docs/en/architecture.md b/docs/en/architecture.md index 24b4165..1ba92e6 100644 --- a/docs/en/architecture.md +++ b/docs/en/architecture.md @@ -45,15 +45,20 @@ The TCP and HTTP servers share a single LSMTree instance to ensure data consiste ├────────────────────────┬────────────────────────────────┤ │ TCP Server │ HTTP Server │ │ (Binary Protocol) │ (REST API) │ -│ Port: 9472 │ Port: 9912 │ +│ Port: 9472 │ Port: 9912 (TCP + 440) │ │ TCP_NODELAY: ON │ Multi-threaded │ -└────────────────────────┴────────────────────────────────┘ +├────────────────────────┴────────────────────────────────┤ +│ WebSocket Server │ +│ (Streaming/Pub-Sub) │ +│ Port: 9913 (TCP + 441) │ +│ TCP_NODELAY: ON │ +└─────────────────────────────────────────────────────────┘ ``` **Key optimizations:** -- **Shared LSMTree** — Both servers operate on the same database instance, eliminating data inconsistency -- **TCP_NODELAY** — Enabled on both listening and client sockets for lower latency on small messages -- **Safe byte conversion** — Proper `bytesToString`/`stringToBytes` functions instead of unsafe `cast` operations +- **Shared LSMTree** — All servers operate on the same database instance, eliminating data inconsistency +- **TCP_NODELAY** — Enabled on listening and client sockets in TCP/WebSocket servers for lower latency on small messages. The HTTP server (via hunos) also handles `TCP_NODELAY` internally. +- **Safe byte conversion** — Proper `bytesToString`/`stringToBytes` functions instead of unsafe `cast` operations in the wire protocol ### Connection Management diff --git a/docs/en/protocol.md b/docs/en/protocol.md index 6579d61..32b2fff 100644 --- a/docs/en/protocol.md +++ b/docs/en/protocol.md @@ -145,7 +145,7 @@ printf '\x00\x00\x00\x12\x01\x02\x00\x00\x00\x00\x08SELECT 1' > /dev/tcp/localho ## HTTP/REST API -Base URL: `http://localhost:9470/api/v1` +Base URL: `http://localhost:9912/api/v1` (HTTP port = TCP port + 440) ### Rate Limiting @@ -321,7 +321,7 @@ POST /admin/check ## WebSocket Protocol -URL: `ws://localhost:9471` +URL: `ws://localhost:9913` (WebSocket port = TCP port + 441) ### Frame Format @@ -351,7 +351,7 @@ WebSocket text frames contain JSON messages: ### Pub/Sub Example ```javascript -const ws = new WebSocket('ws://localhost:9471'); +const ws = new WebSocket('ws://localhost:9913'); ws.onopen = () => { // Subscribe to table changes @@ -403,7 +403,7 @@ let error = makeErrorMessage(1, 42, "Syntax error") ```nim import barabadb/protocol/http -var router = newHttpRouter(port = 9470) +var router = newHttpRouter(port = 9912) router.get("/api/users", proc(req: Request): Future[JsonNode] {.async.} = return %*[ @@ -422,11 +422,15 @@ router.post("/api/users", proc(req: Request): Future[JsonNode] {.async.} = ```nim import barabadb/core/websocket -var server = newWsServer(port = 9471) -server.onMessage = proc(ws: WebSocket, data: seq[byte]) {.gcsafe.} = - echo "Received: ", cast[string](data) - asyncCheck ws.send(cast[string](data)) # Echo -asyncCheck server.run() +proc bytesToString(data: seq[byte]): string = + result = newString(data.len) + for i in 0..