Setup Worker Instances (Optional)¶
This step configures multiple TGFS worker instances for load balancing.
It allows:
- Running multiple server instances
- Distributing traffic using Nginx
⚠ This step is optional. You can skip it if a single instance is sufficient.
1. systemd Setup¶
Service Architecture¶
TG-FileStream uses:
tgfs.service→ Main instancetgfs-worker@.service→ Worker instances (templated)
Each worker:
- Runs on a different port
- Uses a separate session
- Can be load balanced via Nginx
Create Worker Template Service¶
Create:
Paste:
[Unit]
Description=TG-FileStream Worker
After=network.target
Requires=tgfs.service
[Service]
Type=simple
User=<username>
Group=<group>
WorkingDirectory=<TG-FileStream Clone Path>
ExecStart=<TG-FileStream Clone Path>/<virtual-env-dir>/bin/python -m tgfs --port %i --no-update --session %i
Restart=always
RestartSec=3
NoNewPrivileges=true
PrivateTmp=true
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
Adjust:
- Replace <username> with your system username.
- Replace <group> with your group name (often same as username).
- Replace <TG-FileStream Clone Path> with the absolute path to your cloned TG-FileStream directory.
- Replace <virtual-env-dir> with the name of your virtual environment directory (usually venv).
Example
[Unit]
Description=TG-FileStream Worker
After=network.target
Requires=tgfs.service
[Service]
Type=simple
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/tg-filestream
ExecStart=/home/ubuntu/tg-filestream/venv/bin/python -m tgfs --port %i --no-update --session %i
Restart=always
RestartSec=3
NoNewPrivileges=true
PrivateTmp=true
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
How the Worker Template Works¶
%i is replaced by the instance name.
Example:
This runs:
Reload systemd¶
Start Worker Instances¶
Example:
sudo systemctl start tgfs-worker@8081
sudo systemctl start tgfs-worker@8082
sudo systemctl start tgfs-worker@8083
Enable at boot:
sudo systemctl enable tgfs-worker@8081
sudo systemctl enable tgfs-worker@8082
sudo systemctl enable tgfs-worker@8083
Check Status¶
View Logs¶
Restart & Stop Workers¶
Restart worker:
or
Stop worker:
or
2. Nginx Setup for Workers¶
Modify existing Nginx configuration:
Update the upstream block:
upstream aiohttp_backend {
least_conn;
keepalive 32;
server <Bind Address>:<PORT> max_fails=3 fail_timeout=30s;
server <Bind Address>:<PORT> max_fails=3 fail_timeout=30s;
server <Bind Address>:<PORT> max_fails=3 fail_timeout=30s;
}
Replace:
<Bind Address>withHOST<PORT>with worker ports (e.g., 8080, 8081, 8082)
Example
Test Configuration¶
How Load Balancing Works¶
User → Nginx (Port 80) → aiohttp_backend (least_conn) → Workers → Telegram
Nginx distributes traffic based on active connections.
3. Scaling Workers¶
To add a new worker:
- Start it:
- Add it to the Nginx upstream block:
- Reload Nginx: