Commit graph

42 commits

Author SHA1 Message Date
divyam234
bced800719
refactor: db logging 2026-02-03 04:00:55 +05:30
divyam234
b0f2265a98
feat: add pluggable session storage with PostgreSQL, BoltDB, and memory backends
Implement abstract session storage interface allowing multiple backend implementations:

New Features:
- Storage interface (tgstorage.Storage) wrapping gotd/td/session.Storage
- PostgreSQL backend with 30-minute session caching to reduce DB reads
- BoltDB backend for file-based session storage (single file with per-session buckets)
- Memory backend for development/testing (ephemeral)

Configuration:
- Session config moved under [tg.session] section (nested in TGConfig)
- Type: postgres (default), bolt, or memory
- BoltDB path auto-detects to ~/.teldrive/session.db if not specified
- Per-bot session isolation using bot token ID as key

Architecture:
- Factory pattern: NewSessionStorage(cfg, db, cache, key)
- Clean interface with LoadSession, StoreSession, Type(), Close()
- No breaking changes - defaults to PostgreSQL with existing behavior

Files Changed:
- internal/config/config.go: Add SessionStorageConfig to TGConfig
- internal/tgstorage/storage.go: Interface and factory
- internal/tgstorage/postgres.go: PostgreSQL with caching
- internal/tgstorage/bolt.go: BoltDB implementation
- internal/tgstorage/memory.go: Memory implementation
- internal/tgstorage/kv.go: Add nil cache check
- internal/tgc/tgc.go: Update BotClient signature
- internal/tgc/helpers.go: Update GetBotInfo signature
- internal/tgc/channel_manager.go: Simplified (no sessionCnf field)
- internal/tgc/client_pool.go: Simplified (no sessionCnf field)
- internal/tgstorage/session.go: Deleted (replaced by new interface)
- go.mod/go.sum: Add bbolt dependency

All call sites updated to use new simplified signatures.
2026-02-02 16:19:16 +05:30
divyam234
791363b0fd
feat: non-blocking server startup with proper shutdown handling
Major changes:
- Refactor server startup to run cache, events, and cron in background goroutines
- Main thread only handles server lifecycle and shutdown coordination
- Add proper error handling with os.Exit(1) instead of hanging lg.Fatal()

Database connection improvements:
- Add connect_timeout parameter to PostgreSQL DSN (10s default)
- Wrap gorm.Open in goroutine with timeout context for cancellable connections
- Improve retry logging with attempt counters and clear error messages
- Check context cancellation during retry delays

Event system:
- Add EventBroadcaster interface with Redis and Polling implementations
- Base broadcaster with DB worker pool, deduplication, and graceful drain
- Redis broadcaster for multi-instance setups with Pub/Sub
- Polling broadcaster for single-instance setups
- Configurable: DBWorkers, DBBufferSize, DeduplicationTTL

Other improvements:
- Add distributed locking for channel creation (PostgreSQL advisory locks)
- Fix KV storage upsert with ON CONFLICT
- Ensure process exits immediately on fatal errors
2026-02-02 15:41:49 +05:30
divyam234
1071a6e8b5
feat: add BLAKE3 tree hashing support for file integrity verification
Implements BLAKE3 cryptographic hashing to provide file integrity verification
and support for future deduplication features.
Key features:
- Tree hashing: Files are hashed in 16MB blocks, with block hashes stored
  concatenated in uploads table, then tree-hashed for final file hash
- BlockHasher: Streaming hasher that processes data in fixed-size blocks
  during upload without loading entire file into memory
- Fast verification: BLAKE3 is significantly faster than MD5/SHA256 while
  maintaining cryptographic security
Database changes:
- Added block_hashes (BYTEA) column to teldrive.uploads table for storing
  per-block hashes during upload process
- Added hash (TEXT) column to teldrive.files table for final tree hash
- Migration: 20260201144943_add_hash_column.sql
Code changes:
- New package: internal/hash/hash.go with BlockHasher implementation
- Updated models: File.Hash and Upload.BlockHashes fields
- Upload service: Computes block hashes during file upload when hashing enabled
- File service: Computes final tree hash from block hashes after upload complete
The hashing is designed to support:
- File integrity verification on download
- Deduplication across users (same file = same hash)
- Efficient verification of large files without full re-download
Note: Hash computation is optional and controlled by client 'hashing' parameter
2026-02-02 09:49:03 +05:30
divyam234
b05b919fb1
feat: add integration tests and improve indexing performance 2026-01-17 11:54:41 +05:30
divyam234
8368f5f878
refactor: logging and fix lint errors 2026-01-07 20:54:52 +05:30
divyam234
110a821836
feat: enhance check command with dry-run and verbose logging
- Add --dry-run flag to simulate check/clean without changes
- Add --verbose flag for detailed logging and debugging
- Add comprehensive summary output showing files checked, missing, orphans
- Rename --export to --export-file with custom path support
- Improve error messages with channel-specific context
- Add YAML configuration support with config.sample.yml
- Refactor reader for concurrent chunk fetching with buffer management
- Add config loader tests
- Update dependencies (gotd/td, gocron, redis, opentelemetry, etc.)
- Fix RegisterPlags typo to RegisterFlags in cmd/run.go
2025-12-24 10:37:46 +05:30
divyam234
616cf4ad4a
feat: Add Automatic Channel Rollover
- Implement channel rollover based on part count limits
- Auto-create channels and add bots as admins
- Add teldrive.kv key-value table
- Deduplicate bots and set (user_id, token) as primary key
2025-10-07 22:05:55 +05:30
divyam234
5c619a06d6
feat: add distributed cron job locking and new cron jobs
- Introduced a new configuration option `locker-instance` for distributed cron job locking.
- Updated the cron job service to use `gormlock` for distributed locking.
- Refactored the `StartCronJobs` function to initialize the locker and scheduler.
- Added new cron jobs for cleaning old events and logging actions for existing jobs.
2025-06-22 20:30:45 +05:30
divyam234
bfbeab6b59
refactor: replace interface{} with any type across codebase 2025-04-10 08:00:54 +02:00
divyam234
d66dc78e35
feat: add file operation event recording system 2025-04-08 20:02:45 +02:00
Bhunter
6f7bf17f51 refactor: remove hardcoded tg creds 2025-01-27 22:51:39 +01:00
divyam234
8e4dfd859c refactor: add index for listing channel files and impove check command 2025-01-21 21:21:59 +05:30
Bhunter
7b4fd14574 refactor: improve command flag registration 2025-01-16 21:20:30 +01:00
Bhunter
0540ae1075 feat: add new check command to check integrity of file parts 2025-01-16 14:46:25 +01:00
Bhunter
0d51fdfe0a refactor: migrate session db from bbolt to sqlite 2025-01-15 12:33:02 +01:00
Bhunter
4c5d5035f7 refactor: update logging configuration to use string levels 2025-01-14 11:58:04 +01:00
Bhunter
f0187f4052 refactor: standardize field naming and improve cache key generation 2025-01-14 11:22:41 +01:00
Bhunter
518435fcd3 refactor: replace kv usage with boltdb and update related configurations 2025-01-12 20:05:10 +05:30
Bhunter
7fa4a38197 refactor: add version endpoint 2025-01-11 15:35:24 +01:00
divyam234
a296b368de feat: add NTP configuration and improve middleware handling 2025-01-01 16:54:36 +05:30
divyam234
a1440b1d7a refactor: remove bg bots 2024-09-10 13:00:47 +05:30
divyam234
c7149701f1 refactor: Update dependencies and refactor multireader and workers 2024-08-10 00:08:06 +05:30
divyam234
9ce54b0a50 feat: added redis cache support 2024-07-26 21:20:26 +05:30
divyam234
e7865a7637 refactor: readd server timeouts 2024-07-09 23:41:05 +05:30
divyam234
758f0f82ea
refactor: Handle file updates in transaction (#292)
* feat: Add Stream Bots Offset flag to run command

* feat: Update Stream Bots Offset flag in run command

* update

* update

* update

* invalidate cache on update

* chore: Update cache key for file location deletion

* chore: Update cache key for file location retrieval

* chore: Refactor file service to optimize token processing

* chore: Optimize token processing in file service

* chore: Update file service to use PUT method for updating parts
2024-07-08 16:42:48 +05:30
divyam234
678de5bc63 chore: Remove statsviz monitoring 2024-07-07 12:53:28 +05:30
divyam234
c5b9c99005 chore: update stataviz monitoring 2024-07-06 11:23:30 +05:30
divyam234
1353ae7bd2 feat: added option to disable cron jobs 2024-07-01 17:11:55 +05:30
divyam234
04755c76e6 fix: memory leak in streams 2024-06-27 00:35:23 +05:30
divyam234
2c7db87b3f feat: option to close bg bots after a certain time 2024-06-24 17:30:25 +05:30
divyam234
825dc11fe1 feat: MultiThreaded stream support 2024-06-22 17:59:59 +05:30
divyam234
855a320f32 feat: buffered reader for fast streaming 2024-06-14 00:55:15 +05:30
divyam234
306a508319 refactor: db params 2024-06-04 17:49:13 +05:30
divyam234
943a5012b9 feat: added dc pooler and support 0 length file size 2024-06-02 23:11:18 +05:30
divyam234
6dd0fd2d80 refactor: improve error handling in file chunk upload 2024-05-28 11:07:06 +05:30
divyam234
c47eef0d70 feat: add support for HTTP and SOCKS5 proxy for telegram client 2024-05-15 17:24:44 +05:30
divyam234
a936ed7960 fix: remove server timeout 2024-02-14 18:37:22 +05:30
divyam234
4efc7c2591 feat: Add tg-session-file flag to run command 2024-02-13 00:29:06 +05:30
divyam234
82e4f4afe2 chore: Update dependencies and configuration files 2024-02-12 23:52:52 +05:30
divyam234
ddbc88f770 feat: add viper and cobra support 2024-02-12 21:32:55 +05:30
divyam234
7946f3e381 feat: Added tests , and change config structure 2024-02-12 02:25:01 +05:30