As noted in the comments, you cannot do it well enough to make any warranties. But suppose you want to do your best, anyway.
This problem has two parts:
- Determine the available bandwidth.
- Bandwidth management
Proper bandwidth management can be done in the user space program by limiting the speed at which you read the socket. The TCP / IP stack will notify the other end of the connection that is queued on your application on behalf of your application and it will no longer be sent. A convenient way to implement this speed limit is in token tokens .
Fast implementation of slave token:
int bucket = 0; start_thread({ while(transfer_in_progress) { bucket += bytes_per_second_limit; sleep(1); }); while(transfer_in_progress) { bytesread = read(socket, buffer, min(bucket, buffersize), ); bucket -= bytesread; }
If bytes_per_second_limit set to approximately the bandwidth expressed in bytes / second, then this should be read as fast as the connection allows. If the connection is faster, you will be limited bytes_per_second_limit . If the connection is slower, then the bucket will grow forever with a speed proportional to the difference between the speed limit and the available bandwidth.
Hm!
If you start another thread and watch the bucket , you can observe two conditions:
- If the
bucket always 0, then there is more bandwidth available and you can increase bytes_per_second_limit , perhaps by your last best guess for the available bandwidth (from # 2). Or run an additional download. - If the
bucket larger than the last time you looked, and the last few seconds of the data points indicate continued growth (possibly linear regression, whatever you want), the growth rate is expressed in bytes / second, as far as you can reduce bytes_per_second_limit to match download speed with available bandwidth.
The problem is that there is no guarantee that your bandwidth will remain constant. Monitoring bucket threads can bounce back and forth between speed increase and limitation. I suggest starting with averaging at least 10 or 20 seconds before making changes to the speed limit.
source share