| Author |
Message |
< Erlang ~ Its this scenario correct and fault proof ? |
| forcewill |
Posted: Wed Feb 09, 2011 6:43 pm |
|
|
|
User
Joined: 12 Feb 2010
Posts: 19
|
Scenario:
"To have a gen server which will handle concurrent requests but limits the current amount of concurrent requests it is handling."
I have implemented like this
handle_cast(concurrent_request, #st{max_push = Max, cur_push = Max} ->
%%OK im at the treshold
{noreply, St};
handle_cast(concurrent_request, St = #st{cur_requests = Cur}) ->
Pid = proc_lib:spawn_link(fun() -> .... end),
erlang:monitor(process, Pid),
{noreply, St#st{cur_requests = Cur + 1}
And then in handle_info
handle_info(_Info, State = #state{cur_requests = Cur}) ->
{noreply, State#state{cur_requests = Cur - 1}}.
My question is if this implementation is bullet prof ? Im asking this because if it isn't my server may stop handling requests. |
|
|
| Back to top |
|
| rvirding |
Posted: Fri Feb 11, 2011 12:04 am |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
I just put a code wrapper around your code make it easier to read.
Code:
handle_cast(concurrent_request, #st{max_push = Max, cur_push = Max} ->
%%OK im at the treshold
{noreply, St};
handle_cast(concurrent_request, St = #st{cur_requests = Cur}) ->
Pid = proc_lib:spawn_link(fun() -> .... end),
erlang:monitor(process, Pid),
{noreply, St#st{cur_requests = Cur + 1}}.
handle_info(_Info, State = #state{cur_requests = Cur}) ->
{noreply, State#state{cur_requests = Cur - 1}}.
I assume either cur_push or cur_requests is a typo otherwise you will be counting and testing different fields in the state.
The basic idea will work but there are details which must be worked out.
- You are linking, through proc_lib:spawn_link, to the worker processes, so if the gen_server is not trapping exits and a worker crashes then the gen_server and all the other workers will die. However, when a worker terminates normally then gen_server will not notice this. This is the standard error handling mechanism.
- You are both linking and monitoring the worker processes so if you are trapping exits you will receive two messages notifying you about this: one from the link, and one from the monitor.
- Why are you spawning the workers using proc_lib? There is nothing wrong with this but it does cost a little more, which might be unnecessary unless you need the extra functionality (for example an error log if they crash).
- It is very unsafe not to test in handle_info what message you are receiving, it might be an exit/monitor message from a worker. The safest would be to keep a list of current workers and test if it is the right exit/monitor message from a known worker.
- You need to decide how to handle errors in the both the gen_server and in the workers. If an error occurs do you crash everything and restart, or do you carry on bravely and hope for the best?
Those are the immediate points. |
_________________ Robert Virding, Erlang Solutions Ltd. |
|
| Back to top |
|
| forcewill |
Posted: Fri Feb 11, 2011 10:21 am |
|
|
|
User
Joined: 12 Feb 2010
Posts: 19
|
Thanks for the reply, its not a typo i use cur_requests to keep track of the number of "workers" and max_push as a configuration value to limit the max amount of workers
so that when cur_push equals max_push i dont allow any new worker to do its work.
I'm using proc_lib because sometime ago i read somewhere that it was the way to do it with OTP because it would get me a good log about whats happened if i don't use proc lib i just get a "Pid crashed at node blá blá" i don't know the context of that Pid, like what process spawned it right ? |
|
|
| Back to top |
|
| rvirding |
Posted: Mon Feb 14, 2011 12:37 am |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
forcewill wrote: Thanks for the reply, its not a typo i use cur_requests to keep track of the number of "workers" and max_push as a configuration value to limit the max amount of workers
so that when cur_push equals max_push i dont allow any new worker to do its work.
I realised that max_push is a configuration value but I meant that while you increment cur_requests when you start a new "worker" you use cur_push to test if you have reached the limit of number of workers. In your example you don't show how cur_requests become cur_push. That is what I meant by counting and testing different values. |
_________________ Robert Virding, Erlang Solutions Ltd. |
|
| Back to top |
|
| forcewill |
Posted: Mon Feb 14, 2011 11:39 am |
|
|
|
User
Joined: 12 Feb 2010
Posts: 19
|
Oh yes sorry you are correct cur_push is a typo
i only have max_push and cur_requests . |
|
|
| Back to top |
|
| dongdongwu |
Posted: Thu Sep 20, 2012 6:45 am |
|
|
|
User
Joined: 19 Sep 2012
Posts: 236
|
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word. |
|
|
| Back to top |
|
|
|