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.
- master branch is stable now, you can use it directly.
- the project has been stable for two years running in our system
- i wrote this project when i was an erlang learner, so the code style may be not good -:(
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.
- Add a good tutorial.
- Add a lisence file, maybe BSD like license is good choice.