<div dir="ltr">BTW, you can of course use the shorthand notation for this as well.<div><br></div><div>7> jobs:add_queue(otto, [{standard_counter,3},{max_size,3}]).</div><div>ok<br>8> [spawn(fun() -> Res = jobs:ask(otto), io:fwrite("~p --> ~p: ~p~n", [Res, time(), self()]), timer:sleep(3000), exit(normal) end) || _ <- lists:seq(1,10)]. <br>{ok,{#Ref<0.4079473062.3785097217.122736>,<br> [{counters,[{{counter,otto,1},1}]}]}} --> {7,12,39}: <0.4238.0><br>{ok,{#Ref<0.4079473062.3785097217.122740>,<br> [{counters,[{{counter,otto,1},1}]}]}} --> {7,12,39}: <0.4239.0><br>{ok,{#Ref<0.4079473062.3785097217.122742>,<br> [{counters,[{{counter,otto,1},1}]}]}} --> {7,12,39}: <0.4240.0><br>{error,rejected} --> {7,12,39}: <0.4244.0><br>{error,rejected} --> {7,12,39}: <0.4245.0><br>{error,rejected} --> {7,12,39}: <0.4246.0><br>{error,rejected} --> {7,12,39}: <0.4247.0><br>[<0.4238.0>,<0.4239.0>,<0.4240.0>,<0.4241.0>,<0.4242.0>,<br> <0.4243.0>,<0.4244.0>,<0.4245.0>,<0.4246.0>,<0.4247.0>]<br>{ok,{#Ref<0.4079473062.3785097217.122752>,<br> [{counters,[{{counter,otto,1},1}]}]}} --> {7,12,42}: <0.4241.0><br>{ok,{#Ref<0.4079473062.3785097217.122753>,<br> [{counters,[{{counter,otto,1},1}]}]}} --> {7,12,42}: <0.4242.0><br>{ok,{#Ref<0.4079473062.3785097217.122754>,<br> [{counters,[{{counter,otto,1},1}]}]}} --> {7,12,42}: <0.4243.0><br></div><div><br></div><div>And you can limit the queue also based on time - and both size and time at the same time.</div><div><br></div><div>9> jobs:add_queue(jane, [{standard_counter,3},{max_size,3},{max_time,2000}]).</div><div>ok<br>10> [spawn(fun() -> Res = jobs:ask(jane), io:fwrite("~p --> ~p: ~p~n", [Res, time(), self()]), timer:sleep(3000), exit(normal) end) || _ <- lists:seq(1,10)]. <br>{ok,{#Ref<0.4079473062.3785097217.122770>,<br> [{counters,[{{counter,jane,1},1}]}]}} --> {7,14,46}: <0.4262.0><br>{ok,{#Ref<0.4079473062.3785097217.122774>,<br> [{counters,[{{counter,jane,1},1}]}]}} --> {7,14,46}: <0.4263.0><br>{ok,{#Ref<0.4079473062.3785097217.122776>,<br> [{counters,[{{counter,jane,1},1}]}]}} --> {7,14,46}: <0.4264.0><br>{error,rejected} --> {7,14,46}: <0.4268.0><br>{error,rejected} --> {7,14,46}: <0.4269.0><br>{error,rejected} --> {7,14,46}: <0.4270.0><br>{error,rejected} --> {7,14,46}: <0.4271.0><br>[<0.4262.0>,<0.4263.0>,<0.4264.0>,<0.4265.0>,<0.4266.0>,<br> <0.4267.0>,<0.4268.0>,<0.4269.0>,<0.4270.0>,<0.4271.0>]<br>{error,timeout} --> {7,14,48}: <0.4267.0><br>{error,timeout} --> {7,14,48}: <0.4266.0><br>{error,timeout} --> {7,14,48}: <0.4265.0><br></div><div><br></div><div>BR,</div><div>Ulf W</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Nov 15, 2021 at 7:11 AM Ulf Wiger <<a href="mailto:ulf@wiger.net">ulf@wiger.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Well, you can.<div><br></div><div>4> jobs:add_queue(becka, [{regulators,[{counter,[{limit,3}]}]},{max_size,3}]).</div><div>ok</div>5> [spawn(fun() -> Res = jobs:ask(becka), io:fwrite("~p --> ~p: ~p~n", [Res, time(), self()]), timer:sleep(3000), exit(normal) end) || _ <- lists:seq(1,10)].<br>{ok,{#Ref<0.4079473062.3785097227.122199>,<br> [{counters,[{{counter,becka,1},1}]}]}} --> {7,6,46}: <0.4192.0><br>{ok,{#Ref<0.4079473062.3785097227.122196>,<br> [{counters,[{{counter,becka,1},1}]}]}} --> {7,6,46}: <0.4191.0><br>{ok,{#Ref<0.4079473062.3785097227.122202>,<br> [{counters,[{{counter,becka,1},1}]}]}} --> {7,6,46}: <0.4193.0><br>{error,rejected} --> {7,6,46}: <0.4197.0><br>{error,rejected} --> {7,6,46}: <0.4198.0><br>{error,rejected} --> {7,6,46}: <0.4199.0><br>{error,rejected} --> {7,6,46}: <0.4200.0><br>[<0.4191.0>,<0.4192.0>,<0.4193.0>,<0.4194.0>,<0.4195.0>,<br> <0.4196.0>,<0.4197.0>,<0.4198.0>,<0.4199.0>,<0.4200.0>]<br>{ok,{#Ref<0.4079473062.3785097217.122718>,<br> [{counters,[{{counter,becka,1},1}]}]}} --> {7,6,49}: <0.4194.0><br>{ok,{#Ref<0.4079473062.3785097217.122720>,<br> [{counters,[{{counter,becka,1},1}]}]}} --> {7,6,49}: <0.4195.0><br>{ok,{#Ref<0.4079473062.3785097217.122721>,<br> [{counters,[{{counter,becka,1},1}]}]}} --> {7,6,49}: <0.4196.0><br><div> <br></div><div>So, basically, the first three requests were served and removed from the queue. Meanwhile, a bunch of requests were rejected due to the size limit. Eventually, a few new requests were able to enter. After 3 seconds (when the first 3 workers woke up and finished), they were able to run. Now the queue is empty again.</div><div><br></div><div>BR,</div><div>UIf W</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Nov 14, 2021 at 11:13 PM bengt <<a href="mailto:cean.ebengt@gmail.com" target="_blank">cean.ebengt@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>Thank you very much. With this explanation I should get my application properly limited.<br><br>One thing though, is it not possible to get an error from jobs:ask/1 ? Like <span style="color:rgb(110,119,129);font-family:ui-monospace,SFMono-Regular,"SF Mono",Menlo,Consolas,"Liberation Mono",monospace;white-space:pre-wrap;background-color:rgb(255,255,255)">{error, Reason}</span><br><div>when the queue is full? That is, jobs:done/1 have not been called for the previous 3 {ok, X}. <br><br>bengt<br><br><blockquote type="cite"><div>On 14 Nov 2021, at 19:55, Ulf Wiger <<a href="mailto:ulf@wiger.net" target="_blank">ulf@wiger.net</a>> wrote:</div><br><div><div dir="ltr">In jobs, `max_size` has to do with how large the queue is allowed to get.<div>What you appear to be looking for is `counter`.<br><br>Example:<br>Eshell V10.7.2.8 (abort with ^G)<br>1> application:ensure_all_started(jobs).<br>{ok,[jobs]}<br>2> jobs:add_queue(kalle, [{standard_counter,3}]).<br>ok<br>3> [spawn(fun() -> jobs:ask(kalle), io:fwrite("--> ~p: ~p~n", [time(), self()]), timer:sleep(3000), exit(normal) end) || _ <- lists:seq(1,10)]. <br>--> {19,53,29}: <0.128.0><br>--> {19,53,29}: <0.129.0><br>--> {19,53,29}: <0.130.0><br>[<0.128.0>,<0.129.0>,<0.130.0>,<0.131.0>,<0.132.0>,<br> <0.133.0>,<0.134.0>,<0.135.0>,<0.136.0>,<0.137.0>]<br>--> {19,53,32}: <0.131.0><br>--> {19,53,32}: <0.133.0><br>--> {19,53,32}: <0.132.0><br>--> {19,53,35}: <0.134.0><br>--> {19,53,35}: <0.135.0><br>--> {19,53,35}: <0.136.0><br>--> {19,53,38}: <0.137.0><br></div><div><br></div><div>From the docs:</div><div>"{standard_counter, C} - equivalent to [{regulators,[{counter,[{limit,C}, {modifiers,[{cpu,10},{memory,10}]}]}]}]"</div><div><br></div><div>(Usually, one doesn't specify the modifiers)</div><div><br></div><div>BR,</div><div>Ulf</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Nov 13, 2021 at 6:12 PM bengt <<a href="mailto:cean.ebengt@gmail.com" target="_blank">cean.ebengt@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Greetings,<br>
<br>
I would like to allow at most 3 processes to call a gen_server at the same time. I could check the message queue length, but why not use jobs (<a href="https://github.com/uwiger/jobs" rel="noreferrer" target="_blank">https://github.com/uwiger/jobs</a>) ?<br>
<br>
Jobs is already in use by something I depend on, so it should be working. I test from the (iex) shell. As can be seen below I am making a mistake somewhere. Probably config, so how should I configure to only get 3 of the 5 jobs:ask/1 to be approved and queued? Or I have misunderstood the use, and an explanation of how to achieve the 3 caller limitation would be appreciated.<br>
<br>
:jobs.add_queue(:kalle, [{:max_size, 3}])<br>
for _ <- [1,2,3,4,5], do: :jobs.ask(:kalle)<br>
IO.inspect :jobs.queue_info(:kalle)<br>
<br>
name: :kalle,<br>
max_size: 3,<br>
approved: 5,<br>
queued: 5,<br>
latest_dispatch: 378734152926266,<br>
mod: :jobs_queue,<br>
type: :fifo,<br>
group: :undefined,<br>
regulators: [],<br>
max_time: :undefined,<br>
check_interval: :infinity,<br>
oldest_job: :undefined,<br>
timer: :undefined,<br>
link_ref: :undefined,<br>
check_counter: 0,<br>
empty: false,<br>
depleted: false,<br>
waiters: [],<br>
stateful: :undefined,<br>
st: {:st, #Reference<0.2285429207.3584425985.10312>},<br>
producers: []<br>
<br>
Best Wishes,<br>
bengt</blockquote></div>
</div></blockquote></div><br></div></blockquote></div>
</blockquote></div>