forked from dashbitco/nimble_pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnimble_pool.ex
1208 lines (954 loc) · 38.8 KB
/
nimble_pool.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule NimblePool do
@external_resource "README.md"
@moduledoc "README.md"
|> File.read!()
|> String.split("<!-- MDOC !-->")
|> Enum.fetch!(1)
use GenServer
require Logger
@type from :: {pid, reference}
@type init_arg :: term
@type pool_state :: term
@type worker_state :: term
@type client_state :: term
@type user_reason :: term
@typedoc since: "1.1.0"
@type pool :: GenServer.server()
@doc """
Initializes the worker.
It receives the worker argument passed to `start_link/1` if `c:init_pool/1` is
not implemented, otherwise the pool state returned by `c:init_pool/1`. It must
return `{:ok, worker_state, pool_state}` or `{:async, fun, pool_state}`, where the `fun`
is a zero-arity function that must return the worker state.
If this callback returns `{:async, fun, pool_state}`, `fun` is executed in a **separate
one-off process**. Because of this, if you start resources that the pool needs to "own",
you need to transfer ownership to the pool process. For example, if your async `fun`
opens a `:gen_tcp` socket, you'll have to use `:gen_tcp.controlling_process/2` to transfer
ownership back to the pool.
> #### Blocking the pool {: .warning}
>
> This callback is synchronous and therefore will block the pool, potentially
> for a significant amount of time since it's executed in the pool process once
> per worker. > If you need to perform long initialization, consider using the
> `{:async, fun, pool_state}` return type.
"""
@doc callback: :worker
@callback init_worker(pool_state) ::
{:ok, worker_state, pool_state} | {:async, (-> worker_state), pool_state}
@doc """
Initializes the pool.
It receives the worker argument passed to `start_link/1` and must
return `{:ok, pool_state}` upon successful initialization,
`:ignore` to exit normally, or `{:stop, reason}` to exit with `reason`
and return `{:error, reason}`.
This is a good place to perform a registration, for example.
It must return the `pool_state`. The `pool_state` is given to
`init_worker`. By default, it simply returns the given arguments.
This callback is optional.
## Examples
@impl NimblePool
def init_pool(options) do
Registry.register(options[:registry], :some_key, :some_value)
end
"""
@doc callback: :pool
@callback init_pool(init_arg) :: {:ok, pool_state} | :ignore | {:stop, reason :: any()}
@doc """
Checks a worker out.
The `maybe_wrapped_command` is the `command` passed to `checkout!/4` if the worker
doesn't implement the `c:handle_enqueue/2` callback, otherwise it's the possibly-wrapped
command returned by `c:handle_enqueue/2`.
This callback must return one of:
* `{:ok, client_state, worker_state, pool_state}` — the client state is given to
the callback function passed to `checkout!/4`. `worker_state` and `pool_state`
can potentially update the state of the checked-out worker and the pool.
* `{:remove, reason, pool_state}` — `NimblePool` will remove the checked-out worker and
attempt to checkout another worker.
* `{:skip, Exception.t(), pool_state}` — `NimblePool` will skip the checkout, the client will
raise the returned exception, and the worker will be left ready for the next
checkout attempt.
> #### Blocking the pool {: .warning}
>
> This callback is synchronous and therefore will block the pool.
> Avoid performing long work in here. Instead, do as much work as
> possible on the client.
Once the worker is checked out, the worker won't handle any
messages targeted to `c:handle_info/2`.
"""
@doc callback: :worker
@callback handle_checkout(maybe_wrapped_command :: term, from, worker_state, pool_state) ::
{:ok, client_state, worker_state, pool_state}
| {:remove, user_reason, pool_state}
| {:skip, Exception.t(), pool_state}
@doc """
Checks a worker back in the pool.
It receives the potentially-updated `client_state`, returned by the `checkout!/4`
anonymous function, and it must return either
`{:ok, worker_state, pool_state}` or `{:remove, reason, pool_state}`.
> #### Blocking the pool {: .warning}
>
> This callback is synchronous and therefore will block the pool.
> Avoid performing long work in here, instead do as much work as
> possible on the client.
Once the connection is checked in, it may immediately be handed
to another client, without traversing any of the messages in the
pool inbox.
This callback is optional.
"""
@doc callback: :worker
@callback handle_checkin(client_state, from, worker_state, pool_state) ::
{:ok, worker_state, pool_state} | {:remove, user_reason, pool_state}
@doc """
Handles an update instruction from a checked out worker.
See `update/2` for more information.
This callback is optional.
"""
@doc callback: :worker
@callback handle_update(message :: term, worker_state, pool_state) ::
{:ok, worker_state, pool_state}
@doc """
Receives a message in the pool and handles it as each worker.
It receives the `message` and it must return either
`{:ok, worker_state}` to update the worker state, or `{:remove, reason}` to
remove the worker.
Since there is only a single pool process that can receive messages, this
callback is executed once for every worker when the pool receives `message`.
> #### Blocking the pool {: .warning}
>
> This callback is synchronous and therefore will block the pool while it
> executes for each worker. Avoid performing long work in here.
This callback is optional.
"""
@doc callback: :worker
@callback handle_info(message :: term, worker_state) ::
{:ok, worker_state} | {:remove, user_reason}
@doc """
Executed by the pool whenever a request to check out a worker is enqueued.
The `command` argument should be treated as an opaque value, but it can be
wrapped with some data to be used in `c:handle_checkout/4`.
It must return either `{:ok, maybe_wrapped_command, pool_state}` or
`{:skip, Exception.t(), pool_state}` if checkout is to be skipped.
> #### Blocking the pool {: .warning}
>
> This callback is synchronous and therefore will block the pool.
> Avoid performing long work in here.
This callback is optional.
## Examples
@impl NimblePool
def handle_enqueue(command, pool_state) do
{:ok, {:wrapped, command}, pool_state}
end
"""
@doc callback: :pool
@callback handle_enqueue(command :: term, pool_state) ::
{:ok, maybe_wrapped_command :: term, pool_state}
| {:skip, Exception.t(), pool_state}
@doc """
Terminates a worker.
The `reason` argument is:
* `:DOWN` whenever the client link breaks
* `:timeout` whenever the client times out
* one of `:throw`, `:error`, `:exit` whenever the client crashes with one
of the reasons above.
* `reason` if at any point you return `{:remove, reason}`
* if any callback raises, the raised exception will be given as `reason`.
It receives the latest known `worker_state`, which may not
be the latest state. For example, if a client checks out the
state and crashes, we don't fully know the `client_state`,
so the `c:terminate_worker/3` callback needs to take such scenarios
into account.
This callback must always return `{:ok, pool_state}` with the potentially-updated
pool state.
This callback is optional.
"""
@doc callback: :pool
@callback terminate_worker(
reason :: :DOWN | :timeout | :throw | :error | :exit | user_reason,
worker_state,
pool_state
) ::
{:ok, pool_state}
@doc """
Handle pings due to inactivity on the worker.
Executed whenever the idle worker periodic timer verifies that a worker has been idle
on the pool for longer than the `:worker_idle_timeout` pool configuration (in milliseconds).
This callback must return one of the following values:
* `{:ok, worker_state}`: Updates worker state.
* `{:remove, user_reason}`: The pool will proceed to the standard worker termination
defined in `terminate_worker/3`.
* `{:stop, user_reason}`: The entire pool process will be terminated, and `terminate_worker/3`
will be called for every worker on the pool.
This callback is optional.
## Max idle pings
The `:max_idle_pings` pool option is useful to prevent sequential termination of a large number
of workers. However, it is important to keep in mind the following behaviours whenever
utilizing it.
* If you are not terminating workers with `c:handle_ping/2`, you may end up pinging only
the same workers over and over again because each cycle will ping only the first
`:max_idle_pings` workers.
* If you are terminating workers with `c:handle_ping/2`, the last worker may be terminated
after up to `worker_idle_timeout + worker_idle_timeout * ceil(number_of_workers/max_idle_pings)`,
instead of `2 * worker_idle_timeout` milliseconds of idle time.
For instance consider a pool with 10 workers and a ping of 1 second.
Given a negligible worker termination time and a worst-case scenario where all the workers
go idle right after a verification cycle is started, then without `max_idle_pings` the
last worker will be terminated in the next cycle (2 seconds), whereas with a
`max_idle_pings` of 2 the last worker will be terminated only in the 5th cycle (6 seconds).
## Disclaimers
* On lazy pools, if no worker is currently on the pool the callback will never be called.
Therefore you can not rely on this callback to terminate empty lazy pools.
* On not lazy pools, if you return `{:remove, user_reason}` you may end up
terminating and initializing workers at the same time every idle verification cycle.
* On large pools, if many resources go idle at the same cycle, you may end up terminating
a large number of workers sequentially, which could lead to the pool being unable to
fulfill requests. See `:max_idle_pings` option to prevent this.
"""
@doc callback: :worker
@callback handle_ping(
worker_state,
pool_state
) ::
{:ok, worker_state} | {:remove, user_reason()} | {:stop, user_reason()}
@doc """
Handle pool termination.
The `reason` argmument is the same given to GenServer's terminate/2 callback.
It is not necessary to terminate workers here because the
`terminate_worker/3` callback has already been invoked.
This should be used only for clean up extra resources that can not be
handled by `terminate_worker/3` callback.
This callback is optional.
"""
@doc callback: :pool
@callback terminate_pool(
reason :: :DOWN | :timeout | :throw | :error | :exit | user_reason,
pool_state
) :: :ok
@doc """
Handle cancelled checkout requests.
This callback is executed when a checkout request is cancelled unexpectedly.
The context argument may be `:queued` or `:checked_out`:
* `:queued` means the cancellation happened before resource checkout. This may happen
when the pool is starving under load and can not serve resources.
* `:checked_out` means the cancellation happened after resource checkout. This may happen
when the function given to `checkout!/4` raises.
This callback is optional.
"""
@doc callback: :pool
@callback handle_cancelled(
context :: :queued | :checked_out,
pool_state
) :: :ok
@doc """
Handle pings due to pool inactivity.
Runs when the idle pool timer detects no activity for a duration exceeding :pool_idle_timeout (in milliseconds).
A pool is idle if no checkouts or checkins occur within this period.
This callback is optional.
"""
@doc callback: :pool
@callback handle_ping(pool_state) :: {:ok, pool_state} | {:remove, user_reason(), pool_state}
@optional_callbacks init_pool: 1,
handle_checkin: 4,
handle_info: 2,
handle_enqueue: 2,
handle_update: 3,
handle_ping: 2,
handle_ping: 1,
terminate_worker: 3,
terminate_pool: 2,
handle_cancelled: 2
@doc """
Defines a pool to be started under the supervision tree.
It accepts the same options as `start_link/1` with the
addition or `:restart` and `:shutdown` that control the
"Child Specification".
## Examples
NimblePool.child_spec(worker: {__MODULE__, :some_arg}, restart: :temporary)
"""
@spec child_spec(keyword) :: Supervisor.child_spec()
def child_spec(opts) when is_list(opts) do
{worker, _} = Keyword.fetch!(opts, :worker)
{restart, opts} = Keyword.pop(opts, :restart, :permanent)
{shutdown, opts} = Keyword.pop(opts, :shutdown, 5_000)
%{
id: worker,
start: {__MODULE__, :start_link, [opts]},
shutdown: shutdown,
restart: restart
}
end
@doc """
Starts a pool.
## Options
* `:worker` - a `{worker_mod, worker_init_arg}` tuple with the worker
module that implements the `NimblePool` behaviour and the worker
initial argument. This argument is **required**.
* `:pool_size` - how many workers in the pool. Defaults to `10`.
* `:lazy` - When `true`, workers are started lazily, only when necessary.
Defaults to `false`.
* `:worker_idle_timeout` - Timeout in milliseconds to tag a worker as idle.
If not nil, starts a periodic timer on the same frequency that will ping
all idle workers using `handle_ping/2` optional callback .
Defaults to no timeout.
* `:max_idle_pings` - Defines a limit to the number of workers that can be pinged
for each cycle of the `handle_ping/2` optional callback.
Defaults to no limit. See `handle_ping/2` for more details.
* `:pool_idle_timeout` - Timeout in milliseconds to consider the pool idle.
If set, starts a periodic timer at this interval to ping the pool via the
optional `handle_ping/1` callback if there's no activity. Defaults to no timeout.
"""
@spec start_link(keyword) :: GenServer.on_start()
def start_link(opts) when is_list(opts) do
{{worker, arg}, opts} =
Keyword.pop_lazy(opts, :worker, fn ->
raise ArgumentError, "missing required :worker option"
end)
{pool_size, opts} = Keyword.pop(opts, :pool_size, 10)
{lazy, opts} = Keyword.pop(opts, :lazy, false)
{worker_idle_timeout, opts} = Keyword.pop(opts, :worker_idle_timeout, nil)
{max_idle_pings, opts} = Keyword.pop(opts, :max_idle_pings, -1)
{pool_idle_timeout, opts} = Keyword.pop(opts, :pool_idle_timeout, nil)
unless is_atom(worker) do
raise ArgumentError, "worker must be an atom, got: #{inspect(worker)}"
end
unless is_integer(pool_size) and pool_size > 0 do
raise ArgumentError, "pool_size must be a positive integer, got: #{inspect(pool_size)}"
end
GenServer.start_link(
__MODULE__,
{worker, arg, pool_size, lazy, worker_idle_timeout, max_idle_pings, pool_idle_timeout},
opts
)
end
@doc """
Stops the given `pool`.
The pool exits with the given `reason`. The pool has `timeout` milliseconds
to terminate, otherwise it will be brutally terminated.
## Examples
NimblePool.stop(pool)
#=> :ok
"""
@spec stop(pool, reason :: term, timeout) :: :ok
def stop(pool, reason \\ :normal, timeout \\ :infinity) do
GenServer.stop(pool, reason, timeout)
end
@doc """
Checks out a worker from the pool.
It expects a command, which will be passed to the `c:handle_checkout/4`
callback. The `c:handle_checkout/4` callback will return a client state,
which is given to the `function`.
The `function` receives two arguments, the request
(`{pid(), reference()}`) and the `client_state`.
The function must return a two-element tuple, where the first element is the
return value for `checkout!/4`, and the second element is the updated `client_state`,
which will be given as the first argument to `c:handle_checkin/4`.
`checkout!/4` also has an optional `timeout` value. This value will be applied
to the checkout operation itself. The "check in" operation happens asynchronously.
"""
@spec checkout!(pool, command :: term, function, timeout) :: result
when function: (from, client_state -> {result, client_state}), result: var
def checkout!(pool, command, function, timeout \\ 5_000) when is_function(function, 2) do
# Re-implementation of gen.erl call to avoid multiple monitors.
pid = GenServer.whereis(pool)
unless pid do
exit!(:noproc, :checkout, [pool])
end
ref = Process.monitor(pid)
send_call(pid, ref, {:checkout, command, deadline(timeout)})
receive do
{^ref, {:skipped, exception}} ->
raise exception
{^ref, client_state} ->
Process.demonitor(ref, [:flush])
try do
function.({pid, ref}, client_state)
catch
kind, reason ->
send(pid, {__MODULE__, :cancel, ref, kind})
:erlang.raise(kind, reason, __STACKTRACE__)
else
{result, client_state} ->
send(pid, {__MODULE__, :checkin, ref, client_state})
result
end
{:DOWN, ^ref, _, _, :noconnection} ->
exit!({:nodedown, get_node(pid)}, :checkout, [pool])
{:DOWN, ^ref, _, _, reason} ->
exit!(reason, :checkout, [pool])
after
timeout ->
send(pid, {__MODULE__, :cancel, ref, :timeout})
Process.demonitor(ref, [:flush])
exit!(:timeout, :checkout, [pool])
end
end
@doc """
Sends an **update** instruction to the pool about the checked out worker.
This must be called inside the `checkout!/4` callback function with
the `from` value given to `c:handle_checkout/4`.
This is useful to update the pool's state before effectively
checking the state in, which is handy when transferring
resources requires two steps.
"""
@spec update(from, command :: term) :: :ok
def update({pid, ref} = _from, command) do
send(pid, {__MODULE__, :update, ref, command})
:ok
end
defp deadline(timeout) when is_integer(timeout) do
System.monotonic_time() + System.convert_time_unit(timeout, :millisecond, :native)
end
defp deadline(:infinity), do: :infinity
defp get_node({_, node}), do: node
defp get_node(pid) when is_pid(pid), do: node(pid)
defp send_call(pid, ref, message) do
# Auto-connect is asynchronous. But we still use :noconnect to make sure
# we send on the monitored connection, and not trigger a new auto-connect.
Process.send(pid, {:"$gen_call", {self(), ref}, message}, [:noconnect])
end
defp exit!(reason, fun, args) do
exit({reason, {__MODULE__, fun, args}})
end
## Callbacks
@impl true
def init({worker, arg, pool_size, lazy, worker_idle_timeout, max_idle_pings, pool_idle_timeout}) do
Process.flag(:trap_exit, true)
case Code.ensure_loaded(worker) do
{:module, _} ->
:ok
{:error, reason} ->
raise ArgumentError, "failed to load worker module #{inspect(worker)}: #{inspect(reason)}"
end
lazy = if lazy, do: pool_size, else: nil
if worker_idle_timeout do
if function_exported?(worker, :handle_ping, 2) do
Process.send_after(self(), :check_idle, worker_idle_timeout)
else
IO.warn(
":worker_idle_timeout was given but the worker does not export a handle_ping/2 callback"
)
end
end
if pool_idle_timeout do
if function_exported?(worker, :handle_ping, 1) do
Process.send_after(self(), :check_idle_pool, pool_idle_timeout)
else
IO.warn(
":pool_idle_timeout was given but the worker does not export a handle_ping/1 callback"
)
end
end
with {:ok, pool_state} <- do_init_pool(worker, arg) do
{pool_state, resources, async} =
if is_nil(lazy) do
Enum.reduce(1..pool_size, {pool_state, :queue.new(), %{}}, fn
_, {pool_state, resources, async} ->
init_worker(worker, pool_state, resources, async, worker_idle_timeout)
end)
else
{pool_state, :queue.new(), %{}}
end
state = %{
worker: worker,
queue: :queue.new(),
requests: %{},
monitors: %{},
resources: resources,
async: async,
state: pool_state,
lazy: lazy,
worker_idle_timeout: worker_idle_timeout,
max_idle_pings: max_idle_pings,
pool_idle_timeout: pool_idle_timeout,
last_activity_ts: System.monotonic_time(:millisecond)
}
{:ok, state}
end
end
@impl true
def handle_call({:checkout, command, deadline}, {pid, ref} = from, state) do
%{requests: requests, monitors: monitors, worker: worker, state: pool_state} = state
mon_ref = Process.monitor(pid)
requests = Map.put(requests, ref, {pid, mon_ref, :command, command, deadline})
monitors = Map.put(monitors, mon_ref, ref)
state = %{state | requests: requests, monitors: monitors}
case handle_enqueue(worker, command, pool_state) do
{:ok, command, pool_state} ->
{:noreply,
maybe_checkout(command, mon_ref, deadline, from, %{
state
| state: pool_state,
last_activity_ts: System.monotonic_time(:millisecond)
})}
{:skip, exception, pool_state} ->
state = remove_request(%{state | state: pool_state}, ref, mon_ref)
{:reply, {:skipped, exception},
%{state | last_activity_ts: System.monotonic_time(:millisecond)}}
end
end
@impl true
def handle_info({__MODULE__, :update, ref, command}, state) do
%{requests: requests, state: pool_state, worker: worker} = state
case requests do
%{^ref => {pid, mon_ref, :state, worker_state}} ->
{:ok, worker_state, pool_state} = worker.handle_update(command, worker_state, pool_state)
requests = Map.put(requests, ref, {pid, mon_ref, :state, worker_state})
{:noreply, %{state | requests: requests, state: pool_state}}
%{} ->
exit(:unexpected_precheckin)
end
end
@impl true
def handle_info({__MODULE__, :checkin, ref, worker_client_state}, state) do
%{
requests: requests,
resources: resources,
worker: worker,
state: pool_state,
worker_idle_timeout: worker_idle_timeout
} = state
case requests do
%{^ref => {pid, mon_ref, :state, worker_server_state}} ->
checkin =
if function_exported?(worker, :handle_checkin, 4) do
args = [worker_client_state, {pid, ref}, worker_server_state, pool_state]
apply_worker_callback(pool_state, worker, :handle_checkin, args)
else
{:ok, worker_server_state, pool_state}
end
{resources, state} =
case checkin do
{:ok, worker_server_state, pool_state} ->
{:queue.in({worker_server_state, get_metadata(worker_idle_timeout)}, resources),
%{state | state: pool_state}}
{:remove, reason, pool_state} ->
{resources,
remove_worker(reason, worker_server_state, %{state | state: pool_state})}
end
state = remove_request(state, ref, mon_ref)
{:noreply,
maybe_checkout(%{
state
| resources: resources,
last_activity_ts: System.monotonic_time(:millisecond)
})}
%{} ->
exit(:unexpected_checkin)
end
end
@impl true
def handle_info({__MODULE__, :cancel, ref, reason}, state) do
cancel_request_ref(ref, reason, state)
end
@impl true
def handle_info({__MODULE__, :init_worker}, state) do
%{
async: async,
resources: resources,
worker: worker,
state: pool_state,
worker_idle_timeout: worker_idle_timeout
} = state
{pool_state, resources, async} =
init_worker(worker, pool_state, resources, async, worker_idle_timeout)
{:noreply, maybe_checkout(%{state | async: async, resources: resources, state: pool_state})}
end
@impl true
def handle_info({:DOWN, ref, _, _, _} = down, state) do
%{monitors: monitors, async: async} = state
case monitors do
%{^ref => request_ref} ->
cancel_request_ref(request_ref, :DOWN, state)
%{} ->
case async do
%{^ref => _} -> remove_async_ref(ref, state)
%{} -> maybe_handle_info(down, state)
end
end
end
@impl true
def handle_info({:EXIT, pid, _reason} = exit, state) do
%{async: async} = state
case async do
%{^pid => _} -> {:noreply, %{state | async: Map.delete(async, pid)}}
%{} -> maybe_handle_info(exit, state)
end
end
@impl true
def handle_info({ref, worker_state} = reply, state) when is_reference(ref) do
%{async: async, resources: resources, worker_idle_timeout: worker_idle_timeout} = state
case async do
%{^ref => _} ->
Process.demonitor(ref, [:flush])
resources = :queue.in({worker_state, get_metadata(worker_idle_timeout)}, resources)
async = Map.delete(async, ref)
state = %{state | async: async, resources: resources}
{:noreply, maybe_checkout(state)}
%{} ->
maybe_handle_info(reply, state)
end
end
@impl true
def handle_info(
:check_idle,
%{resources: resources, worker_idle_timeout: worker_idle_timeout} = state
) do
case check_idle_resources(resources, state) do
{:ok, new_resources, new_state} ->
Process.send_after(self(), :check_idle, worker_idle_timeout)
{:noreply, %{new_state | resources: new_resources}}
{:stop, reason, state} ->
{:stop, {:shutdown, reason}, state}
end
end
def handle_info(:check_idle_pool, state) do
%{
last_activity_ts: last_ts,
pool_idle_timeout: timeout
} = state
Process.send_after(self(), :check_idle_pool, timeout)
diff = System.monotonic_time(:millisecond) - last_ts
if diff > timeout do
case do_check_idle_pool(state) do
{:ok, new_state} -> {:noreply, new_state}
{:stop, reason, new_state} -> {:stop, {:shutdown, reason}, new_state}
end
else
{:noreply, state}
end
end
@impl true
def handle_info(msg, state) do
maybe_handle_info(msg, state)
end
@impl true
def terminate(reason, %{worker: worker, resources: resources} = state) do
for {worker_server_state, _} <- :queue.to_list(resources) do
maybe_terminate_worker(reason, worker_server_state, state)
end
if function_exported?(worker, :terminate_pool, 2) do
worker.terminate_pool(reason, state)
end
:ok
end
defp do_init_pool(worker, arg) do
if function_exported?(worker, :init_pool, 1) do
worker.init_pool(arg)
else
{:ok, arg}
end
end
defp remove_async_ref(ref, state) do
%{
async: async,
resources: resources,
worker: worker,
state: pool_state,
worker_idle_timeout: worker_idle_timeout
} = state
# If an async worker failed to start, we try to start another one
# immediately, even if the pool is lazy, as we assume there is an
# immediate need for this resource.
{pool_state, resources, async} =
init_worker(worker, pool_state, resources, Map.delete(async, ref), worker_idle_timeout)
{:noreply, %{state | resources: resources, async: async, state: pool_state}}
end
defp cancel_request_ref(
ref,
reason,
%{requests: requests, worker: worker, state: pool_state} = state
) do
case requests do
# Exited or timed out before we could serve it
%{^ref => {_, mon_ref, :command, _command, _deadline}} ->
if function_exported?(worker, :handle_cancelled, 2) do
args = [:queued, pool_state]
apply_worker_callback(worker, :handle_cancelled, args)
end
{:noreply, remove_request(state, ref, mon_ref)}
# Exited or errored during client processing
%{^ref => {_, mon_ref, :state, worker_server_state}} ->
if function_exported?(worker, :handle_cancelled, 2) do
args = [:checked_out, pool_state]
apply_worker_callback(worker, :handle_cancelled, args)
end
state = remove_request(state, ref, mon_ref)
{:noreply, remove_worker(reason, worker_server_state, state)}
# The client timed out, sent us a message, and we dropped the deadlined request
%{} ->
if function_exported?(worker, :handle_cancelled, 2) do
args = [:queued, pool_state]
apply_worker_callback(worker, :handle_cancelled, args)
end
{:noreply, state}
end
end
defp maybe_handle_info(msg, state) do
%{resources: resources, worker: worker, worker_idle_timeout: worker_idle_timeout} = state
if function_exported?(worker, :handle_info, 2) do
{resources, state} =
Enum.reduce(:queue.to_list(resources), {:queue.new(), state}, fn
{worker_server_state, _}, {resources, state} ->
case apply_worker_callback(worker, :handle_info, [msg, worker_server_state]) do
{:ok, worker_server_state} ->
{:queue.in({worker_server_state, get_metadata(worker_idle_timeout)}, resources),
state}
{:remove, reason} ->
{resources, remove_worker(reason, worker_server_state, state)}
end
end)
{:noreply, %{state | resources: resources}}
else
{:noreply, state}
end
end
defp maybe_checkout(%{queue: queue, requests: requests} = state) do
case :queue.out(queue) do
{{:value, {pid, ref}}, queue} ->
case requests do
# The request still exists, so we are good to go
%{^ref => {^pid, mon_ref, :command, command, deadline}} ->
maybe_checkout(command, mon_ref, deadline, {pid, ref}, %{state | queue: queue})
# It should never happen
%{^ref => _} ->
exit(:unexpected_checkout)
# The request is no longer active, do nothing
%{} ->
maybe_checkout(%{state | queue: queue})
end
{:empty, _queue} ->
state
end
end
defp maybe_checkout(command, mon_ref, deadline, {pid, ref} = from, state) do
if past_deadline?(deadline) do
state = remove_request(state, ref, mon_ref)
maybe_checkout(state)
else
%{resources: resources, requests: requests, worker: worker, queue: queue, state: pool_state} =
state = init_worker_if_lazy_and_empty(state)
case :queue.out(resources) do
{{:value, {worker_server_state, _}}, resources} ->
args = [command, from, worker_server_state, pool_state]
case apply_worker_callback(pool_state, worker, :handle_checkout, args) do
{:ok, worker_client_state, worker_server_state, pool_state} ->
GenServer.reply({pid, ref}, worker_client_state)
requests = Map.put(requests, ref, {pid, mon_ref, :state, worker_server_state})
%{
state
| resources: resources,
requests: requests,
state: pool_state,
last_activity_ts: System.monotonic_time(:millisecond)
}
{:remove, reason, pool_state} ->
state = remove_worker(reason, worker_server_state, %{state | state: pool_state})
maybe_checkout(command, mon_ref, deadline, from, %{state | resources: resources})
{:skip, exception, pool_state} ->
GenServer.reply({pid, ref}, {:skipped, exception})
remove_request(%{state | state: pool_state}, ref, mon_ref)
other ->
raise """
unexpected return from #{inspect(worker)}.handle_checkout/4.
Expected: {:ok, client_state, server_state, pool_state} | {:remove, reason, pool_state} | {:skip, Exception.t(), pool_state}
Got: #{inspect(other)}
"""
end
{:empty, _} ->
%{state | queue: :queue.in(from, queue)}
end
end
end
defp init_worker_if_lazy_and_empty(%{lazy: nil} = state), do: state
defp init_worker_if_lazy_and_empty(
%{lazy: lazy, resources: resources, worker_idle_timeout: worker_idle_timeout} = state
) do
if lazy > 0 and :queue.is_empty(resources) do
%{async: async, worker: worker, state: pool_state} = state
{pool_state, resources, async} =
init_worker(worker, pool_state, resources, async, worker_idle_timeout)
%{state | async: async, resources: resources, state: pool_state, lazy: lazy - 1}
else
state
end
end
defp past_deadline?(deadline) when is_integer(deadline) do
System.monotonic_time() >= deadline
end
defp past_deadline?(:infinity), do: false
defp remove_worker(reason, worker_server_state, state) do
state = maybe_terminate_worker(reason, worker_server_state, state)
if lazy = state.lazy do
%{state | lazy: lazy + 1}
else
schedule_init()
state
end
end
defp do_check_idle_pool(state) do
%{
worker: worker,
state: pool_state
} = state