Do you really want to do this? Does your motive annoy your users? (this is a legitimate motive - see any of several free download sites)
Limiting bandwidth like this is not a good way to protect your server from congestion. people will find multithreaded clients and open concurrent FTP sessions ...
Are there any library functions for this?
Perhaps not, bandwidth shaping is an OS task, not a service task.
which algorithm?
The one you describe sounds pretty effective.
, , , . , : , sleep() . .. .
usleep nanosleep
posix, OSX * BSD linux.