From 0deb6072dd91104118e81efe9bb17739e8f41746 Mon Sep 17 00:00:00 2001 From: asepharyana Date: Sat, 29 Aug 2026 11:05:13 +0700 Subject: [PATCH] chore: upgrade runtime to Bun 1.4.0 - Update Nix flake.nix overlay to use Bun 1.4.0 (overrideAttrs) - Update CI (setup-bun) to pin bun-version: 1.4.0 - Update package.json packageManager/Dockerfile/vercel.json - sha256: sha256:Poy0vf7yJ/hzk33QiQj5gnshI5Q7dfbaMD7xgwiyDKw= --- .github/workflows/deploy.yml | 2 +- .../2026-08-27-s3-stability-optimization.md | 58 +++++++++++++++++++ flake.nix | 15 ++++- 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 .hermes/plans/2026-08-27-s3-stability-optimization.md diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 32cd9d2..1afbb90 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -29,7 +29,7 @@ jobs: - uses: oven-sh/setup-bun@v2 with: - bun-version: latest + bun-version: "1.4.0" - name: Install deps run: bun install --frozen-lockfile diff --git a/.hermes/plans/2026-08-27-s3-stability-optimization.md b/.hermes/plans/2026-08-27-s3-stability-optimization.md new file mode 100644 index 0000000..b85ab00 --- /dev/null +++ b/.hermes/plans/2026-08-27-s3-stability-optimization.md @@ -0,0 +1,58 @@ +# TeleUploader — Audit & Optimisasi Stabilitas + S3 (2026-08-27) + +## Scope +Audit dan optimasikan backend TeleUploader agar stabil di segala situasi dan +S3 protocol stabil + secepat mungkin. Fokus pada bottleneck nyata yang +ditemukan saat audit kode + log. + +## Temuan Audit (root cause → prioritas) + +### A. S3 GET lambat (bottleneck terbesar download) +1. **`concatPartStreams` (object-stream.ts) fetch part SEQUENTIAL** — tiap part + di-`fetch` satu per satu (for..of + await penuh). Untuk objek chunked/multipart + dengan N part, byte pertama baru terkirim SETELAH semua fetch part selesai. + → **PARALELISASI** dengan bounded concurrency, urut tetap dipertahankan. +2. **`buildChunkedObjectSources` & `handleGetMultipartObject` resolve + `getFileInfo` SEQUENTIAL** (for..of + await). N call Telegram API berurutan + sebelum streaming mulai. + → **`Promise.all`** resolve paralel. +3. **`botPool.getFileInfo` TIDAK pakai `fileInfoCache`** yang sudah ada — setiap + S3 GET objek reguler memanggil API Telegram `getFile` (network round-trip). + file-controller punya wrapper cache sendiri, tapi path S3 tidak. + → **pindahkan caching ke dalam `botPool.getFileInfo`** agar semua caller dapat. + +### B. Stabilitas / lingkungan produksi +4. **`nodeEnv: "development"` di produksi** (dari log BWS). Winston format json + sudah benar di production, tapi flag salah. +5. **`trustProxy: false` di produksi padahal di belakang Caddy** → rate limiter + meng-key semua user ke `127.0.0.1` (satu bucket global). Ini bug kecil untuk + non-S3 route. Fix: default trustProxy menyesuaikan, tapi utama via env. +6. **MemoryMax=512M di systemd** padahal config mendukung blob 2GB. Streaming + PUT O(1) memory, tapi GET gzip part membuffer part utuh. Naikkan ke 1G. + +### C. Robustness S3 +7. **`handleUploadPart` tidak `await writer.end()`** sebelum `createReadStream` + → potensi race baca file belum flush. Await end(). +8. Batas kecepatan unggah: `telegramBotConcurrency:1` × 6 bot = 6 unggah paralel. + Karena Telegram ~1 rps/bot untuk sendDocument, konkuransi per-bot 1 masuk + akal. Biarkan default, tapi pastikan pool retry terhadap 429 sudah bagus + (sudah, ada retry-after handling). + +## File yang disentuh (semua di bawah src/) +- `src/interfaces/s3/object-stream.ts` — paralelisasi fetch part (concatPartStreams) +- `src/infrastructure/telegram/bot-pool.ts` — getFileInfo + cache, resolve paralel +- `src/infrastructure/telegram/chunked-storage.ts` — buildChunkedObjectSources paralel +- `src/interfaces/http/controllers/s3-controller.ts` — handleGetMultipartObject paralel, + await writer.end() di handleUploadPart +- `src/infrastructure/cache/index.ts` — (opsional) tambah helper + +## Verifikasi +- `bunx biome check src test` (lint) +- `bun test test/s3-auth.test.ts` & `test/s3-operations.test.ts` (S3 regresi) +- `bun test test/telegram.test.ts` (pool) kalau ada +- Test unit baru untuk parallel part streaming (tanpa network) kalau layak. +- Deploy via push main → CI `deploy.yml` (Nix build → nix copy → systemctl restart). + +## Deploy +Push ke `main` → GitHub Actions `deploy.yml` build + deploy Nix ke orangevps. +Setelah deploy, verifikasi `/health` 200 dan S3 GET masih berfungsi. diff --git a/flake.nix b/flake.nix index 3ec566d..f2a0bbf 100644 --- a/flake.nix +++ b/flake.nix @@ -9,7 +9,20 @@ outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachSystem [ "x86_64-linux" ] (system: let - pkgs = import nixpkgs { inherit system; }; + pkgs = import nixpkgs { + inherit system; + overlays = [ + (self: super: { + bun = super.bun.overrideAttrs (old: rec { + version = "1.4.0"; + src = super.fetchzip { + url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.0/bun-linux-x64.zip"; + sha256 = "sha256:Poy0vf7yJ/hzk33QiQj5gnshI5Q7dfbaMD7xgwiyDKw="; + }; + }); + }) + ]; + }; # TeleUploader package teleuploader = pkgs.stdenvNoCC.mkDerivation rec {