mb-rpcd Service¶
The Mailborder RPC Daemon - core service handling all inter-process communication.
Overview¶
mb-rpcd is the central nervous system of Mailborder, providing:
- RPC Interface - Remote procedure call handling
- Database Abstraction - Centralized database access
- Configuration Management - System-wide settings
- Session Management - User authentication and sessions
- Request Handling - Fork-per-request isolation
- API Endpoints - Web interface backend
- Service Coordination - Communication between daemons
All other Mailborder services communicate through mb-rpcd.
Architecture¶
Process Model¶
mb-rpcd (main process)
├── Master Process (PID 1234)
├── Worker 1 (handles requests)
├── Worker 2 (handles requests)
├── Worker 3 (handles requests)
└── Worker N (handles requests)
Fork-per-request isolation: 1. Request arrives (web interface or daemon) 2. Master process forks worker 3. Worker handles request in isolation 4. Worker completes and exits 5. Next request gets fresh worker
Benefits: - Memory leaks contained to single request - Crash isolation (worker crash doesn't affect master) - Clean state for each request - No shared state between requests
Communication¶
Inbound: - Unix socket: /var/run/mailborder/mb-rpcd.sock - TCP socket: 127.0.0.1:10040 (optional)
Outbound: - Database: MariaDB connection - Cache: Redis connection - Other services: Unix socket IPC
Configuration¶
Service File¶
Location: /etc/systemd/system/mb-rpcd.service
[Unit]
Description=Mailborder RPC Daemon
After=network.target mariadb.service redis-server.service
Requires=mariadb.service redis-server.service
[Service]
Type=forking
User=mailborder
Group=mailborder
ExecStart=/usr/libexec/mailborder/php_enc/mb-rpcd start
ExecStop=/usr/libexec/mailborder/php_enc/mb-rpcd stop
ExecReload=/usr/libexec/mailborder/php_enc/mb-rpcd reload
PIDFile=/var/run/mailborder/mb-rpcd.pid
Restart=always
RestartSec=10
# Security hardening
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/var/run/mailborder /var/log/mailborder /var/spool/mailborder
# Resource limits
LimitNOFILE=65536
MemoryLimit=2G
[Install]
WantedBy=multi-user.target
Runtime Configuration¶
Worker pool settings:
sudo mb-config set rpcd.max_workers 20
sudo mb-config set rpcd.min_workers 5
sudo mb-config set rpcd.max_requests_per_worker 500
Timeouts:
Apply changes:
Operations¶
Start/Stop/Restart¶
Start service:
Stop service:
Restart service:
Reload configuration (no downtime):
Check status:
Process Management¶
View processes:
Example output:
1234 /usr/libexec/mailborder/php_enc/mb-rpcd: master
1235 /usr/libexec/mailborder/php_enc/mb-rpcd: worker
1236 /usr/libexec/mailborder/php_enc/mb-rpcd: worker
1237 /usr/libexec/mailborder/php_enc/mb-rpcd: worker
Kill specific worker (emergency):
Graceful restart:
sudo kill -USR1 $(cat /var/run/mailborder/mb-rpcd.pid)
# Signals: USR1=reload, HUP=restart workers, TERM=shutdown
Monitoring¶
Health Check¶
Quick health check:
Example output:
Mailborder RPC Daemon Health Check
===================================
Process Status:
Master PID: 1234 ✓
Workers: 4 active ✓
Socket: /var/run/mailborder/mb-rpcd.sock ✓
Connectivity:
Database: Connected ✓
Redis: Connected ✓
Performance:
Average response time: 45ms
Requests/sec: 127
Memory usage: 412 MB / 2048 MB (20%)
Status: HEALTHY
Real-time Monitoring¶
Watch log:
Watch with journalctl:
Monitor requests:
# Enable request logging
sudo mb-config set rpcd.log_requests true
sudo systemctl reload mb-rpcd
# Watch requests
sudo tail -f /var/log/mailborder/mb-rpcd.log | grep REQUEST
Example log entries:
2025-01-13 14:23:45 [INFO] Worker 1235 handling request from web interface
2025-01-13 14:23:45 [DEBUG] REQUEST: user.login (192.168.1.50)
2025-01-13 14:23:45 [INFO] Request completed in 42ms
2025-01-13 14:23:46 [INFO] Worker 1235 exiting (handled 1 request)
Performance Metrics¶
View statistics:
Example output:
mb-rpcd Statistics
==================
Uptime: 5 days, 12 hours
Total Requests: 1,247,892
Current Load: 127 req/sec
Workers:
Active: 4
Idle: 1
Total Spawned: 45,678
Average Lifetime: 234 requests
Response Times:
Average: 45ms
Median: 32ms
95th Percentile: 156ms
99th Percentile: 289ms
Top Endpoints (last hour):
1. user.authenticate - 4,567 calls
2. email.check_spam - 3,234 calls
3. quarantine.list - 1,890 calls
4. settings.get - 1,234 calls
Performance over time:
Logging¶
Log Files¶
Main log:
Error log:
Access log (if enabled):
Log Levels¶
Set log level:
Temporary debug mode:
sudo mb-rpcd-debug enable
# Enables debug logging for 1 hour
# Disable early
sudo mb-rpcd-debug disable
Log Analysis¶
Recent errors:
Request frequency:
Slow requests (>1 second):
Database Connection¶
Connection Pool¶
Configuration:
sudo mb-config set rpcd.db_pool_min 5
sudo mb-config set rpcd.db_pool_max 50
sudo mb-config set rpcd.db_idle_timeout 300
Monitor connections:
# Active connections
sudo mysql -u mailborder -p mailborder -e "SHOW PROCESSLIST"
# Connection count
sudo mysql -u mailborder -p mailborder -e "SHOW STATUS LIKE 'Threads_connected'"
Connection Issues¶
Test database connectivity:
Example output:
Database Connection Test
========================
Host: localhost
Port: 3306
Database: mailborder
Connection: SUCCESS ✓
Query Test: SUCCESS ✓
Response Time: 12ms
Pool Status:
Min Connections: 5
Max Connections: 50
Active: 8
Idle: 2
Force reconnect:
Security¶
Socket Permissions¶
Check socket:
Expected permissions:
Fix permissions:
sudo chown mailborder:mailborder /var/run/mailborder/mb-rpcd.sock
sudo chmod 660 /var/run/mailborder/mb-rpcd.sock
Authentication¶
API key validation:
All RPC requests require authentication: - User session (web interface) - API key (daemons and API clients) - Internal auth token (service-to-service)
Generate API key:
Request Isolation¶
Fork-per-request model provides: - Memory isolation - No shared state - Crash containment - Security boundaries
Verify isolation:
Troubleshooting¶
Service Won't Start¶
Check dependencies:
Check logs:
Common issues:
-
Database connection failed:
-
Redis connection failed:
-
Socket already in use:
-
Permission error:
Service Crashes¶
Check crash log:
Check core dumps:
Enable core dumps:
Add:
High Memory Usage¶
Check memory:
Reduce worker lifetime:
Increase limits if legitimate:
Add:
Slow Response Times¶
Check worker count:
Increase workers:
Check database performance:
Enable query profiling:
Connection Refused¶
Check if running:
Check socket exists:
Test connection:
echo '{"method":"system.ping"}' | sudo nc -U /var/run/mailborder/mb-rpcd.sock
# Expected: {"result":"pong"}
API Reference¶
Core Methods¶
Authentication: - user.login - User authentication - user.logout - End session - user.validate_session - Check session validity
Configuration: - settings.get - Get setting value - settings.set - Update setting - settings.list - List all settings
Email Management: - email.check_spam - Spam score email - email.scan_virus - Virus scan email - email.apply_policy - Policy evaluation
Quarantine: - quarantine.list - List quarantine - quarantine.release - Release email - quarantine.delete - Delete email
Example Request¶
Via Unix socket:
Response:
{
"status": "success",
"result": {
"version": "6.0.0",
"uptime": 478923,
"workers": 4,
"requests_handled": 1247892
}
}
Best Practices¶
Performance Optimization¶
- Worker Pool Sizing
- Start with CPU core count
- Monitor load and adjust
-
Don't exceed 2x CPU cores
-
Database Connection Pool
- Min: 5 connections
- Max: 50 connections (typical)
-
Idle timeout: 5 minutes
-
Request Timeout
- Default: 30 seconds
- Increase for slow operations
- Monitor slow query log
Reliability¶
-
Monitor continuously
-
Log rotation
- Automatic via logrotate
- Keep 30 days
-
Compress old logs
-
Regular restarts
- Weekly restart during maintenance window
- Clears accumulated state
- Fresh start for workers
Security¶
- Socket permissions
- Only mailborder group access
-
Never world-readable
-
API authentication
- Validate all requests
- Use API keys for services
-
Rotate keys regularly
-
Resource limits
- Memory limits prevent OOM
- File descriptor limits
- CPU time limits
See Also¶
- Service Management - General service operations
- Service Architecture - Overall architecture
- mb-filter - Email filtering service
- mb-milter - Milter protocol service