|
| 1 | +# Bandwidth Quota TURN Server Example |
| 2 | + |
| 3 | +This example demonstrates how to implement per-user bandwidth quotas in the TURN server. Each user (identified by username+realm) gets their own rate limiter that caps their total bandwidth usage across all relay connections. |
| 4 | + |
| 5 | +``` mermaid |
| 6 | +flowchart LR |
| 7 | + A[iperf-client] -->|testPort\n5000| B[TURN client-proxy] |
| 8 | + B --> C[TURN server] |
| 9 | + C -->|peerAddr\nlocalhost:5001| D[iperf server] |
| 10 | +``` |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +### Start the Server |
| 15 | + |
| 16 | +```bash |
| 17 | +go run main.go -public-ip=<YOUR_PUBLIC_IP> -users=user1=pass1,user2=pass2 -bw-limit=12500 |
| 18 | +``` |
| 19 | + |
| 20 | +**Flags:** |
| 21 | +- `-public-ip`: Server's public IP address (required) |
| 22 | +- `-port`: TURN server port (default: 3478) |
| 23 | +- `-users`: Comma-separated list of user=password pairs (required) |
| 24 | +- `-realm`: Authentication realm (default: "pion.ly") |
| 25 | +- `-bw-limit`: Bandwidth limit in bytes/sec per user (default: 12500 = 100 Kbps) |
| 26 | +- `-test`: Enable test mode with built-in TURN client and UDP echo server |
| 27 | +- `-test-port`: UDP port for test echo server (default: 15000) |
| 28 | + |
| 29 | +### Test Mode |
| 30 | + |
| 31 | +The `-test` flag starts a built-in TURN client that allocates a relay and exposes a UDP echo server for easy testing: |
| 32 | + |
| 33 | +```bash |
| 34 | +go run main.go -public-ip=127.0.0.1 -users=testuser=testpass -bw-limit=12500 -test |
| 35 | +``` |
| 36 | + |
| 37 | +## Testing with iperf |
| 38 | + |
| 39 | +You can verify the bandwidth quota is working using `iperf` UDP mode. |
| 40 | + |
| 41 | +### Setup |
| 42 | + |
| 43 | +1. Start the TURN server, `-test` means to also start a TURN client that serves as a proxy for the |
| 44 | + load generator (`iperf` in this case): |
| 45 | + ```bash |
| 46 | + go run main.go -public-ip=127.0.0.1 -users=user1=pass1 -bw-limit=12500 -test |
| 47 | + ``` |
| 48 | + |
| 49 | +2. Start iperf server (simulates a peer): |
| 50 | + ```bash |
| 51 | + iperf -s -u -p 5001 |
| 52 | + ``` |
| 53 | + |
| 54 | +3. Run a TURN client that connects to the peer through the relay. This will run for 10 seconds, |
| 55 | + generating 1 Mbps load traffic. The summary log however shows only ~100 Kbps throughput instead |
| 56 | + of the requested 1 Mbps as the rate-limiter drops traffic exceeding the bandwidth quota. |
| 57 | + ```bash |
| 58 | + iperf -c <proxy-address> -u -p <proxy-port> -b 1M -t 10 |
| 59 | + [ ID] Interval Transfer Bandwidth Jitter Lost/Total Datagrams |
| 60 | + [ 1] 0.0000-10.0596 sec 135 KBytes 110 Kbits/sec 0.000 ms 804/898 (0%) |
| 61 | + ``` |
| 62 | + |
| 63 | + |
| 64 | + |
0 commit comments