A traffic control application written in erlang, you can use it to control the rate of flow, now it is implemented by using token buckets algorithm.
It can be used by block or non-block your application, and you can find demo in src/demo_worker.erl.
start(Pid) ->
start(1000000, Pid).
start(N, Pid) ->
spawn(fun() ->
for(N, fun() -> tokens_queue:request_tokens(Pid) end)
end).
for (0, _Func) ->
ok;
for (N, Func) ->
Func(),
for (N - 1, Func).
{ok, Pid} = tokens_queue_manager:new_tokens_queue(100),
demo_worker:start(Pid).
%% show status
tokens_queue:infos(Pid).
start_with_callback(Pid) ->
spawn(fun() ->
Self = self(),
tokens_queue:set_callback_function(Pid, fun() -> Self ! wakeup end),
tokens_queue:request_tokens_cast(Pid),
loop(Pid)
end).
loop(Pid) ->
receive
wakeup ->
tokens_queue:request_tokens_cast(Pid),
loop(Pid);
_Other ->
io:format("I am fine!~n"),
loop(Pid)
end.
{ok, Pid} = tokens_queue_manager:new_tokens_queue(100),
UserPid = demo_worker:start_with_callback(Pid).
%% show status
tokens_queue:infos(Pid).
This application is used in proxy module in Unified MySQL Platform from taobao to control sql query per second.
If you want to use this application, please using branch stable.
- Add a good tutorial.
- Add a lisence file, maybe BSD like license is good choice.