forked from benoitc/hackney
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackney_pool.erl
More file actions
1395 lines (1251 loc) · 55 KB
/
Copy pathhackney_pool.erl
File metadata and controls
1395 lines (1251 loc) · 55 KB
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
%%% -*- erlang -*-
%%%
%%% This file is part of hackney released under the Apache 2 license.
%%% See the NOTICE for more information.
%%%
%%% Copyright (c) 2009, Erlang Training and Consulting Ltd.
%%% Copyright (c) 2012-2024, Benoît Chesneau <benoitc@e-engura.org>
%% @doc Pool of connection processes.
%%
%% This module manages hackney_conn processes in a pool. Instead of storing
%% raw sockets, it tracks connection process pids. Idle timeout is handled
%% by the connection processes themselves (gen_statem state_timeout).
%%
-module(hackney_pool).
-behaviour(gen_server).
%% PUBLIC API
-export([start/0,
checkout/4,
checkout_ssl/4,
checkin/2]).
%% HTTP/2 connection pooling
-export([checkout_h2/4,
register_h2/5,
unregister_h2/2,
unregister_h2_all/0]).
%% HTTP/3 connection pooling
-export([checkout_h3/4,
register_h3/5,
unregister_h3/2,
get_h3_session/4,
store_h3_session/5,
delete_h3_session/4]).
-export([
get_stats/1,
start_pool/2,
stop_pool/1,
find_pool/1,
notify/2
]).
-export([count/1, count/2,
max_connections/1,
set_max_connections/2,
timeout/1,
set_timeout/2,
prewarm/3,
prewarm/4,
host_stats/3,
child_spec/2]).
-export([start_link/2]).
%% For internal use
-export([to_pool_name/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2]).
-include("hackney.hrl").
-include_lib("hackney_internal.hrl").
-record(state, {
name,
max_connections,
keepalive_timeout,
prewarm_count,
%% Available connection processes: #{Key => [Pid]}
available = #{},
%% In-use connections: #{Pid => Key}
in_use = #{},
%% Pid to monitor ref mapping: #{Pid => MonitorRef}
pid_monitors = #{},
%% Hosts that have been activated (prewarm triggered): sets:set(Key)
activated_hosts = sets:new(),
%% HTTP/2 connections: #{Key => Pid} - one multiplexed connection per host
%% These connections are shared across callers (not checked out exclusively)
h2_connections = #{},
%% HTTP/3 connections: #{Key => Pid} - one multiplexed QUIC connection per host
%% These connections are shared across callers (not checked out exclusively)
h3_connections = #{},
%% HTTP/3 0-RTT/resumption session tickets: #{Key => Ticket} keyed by
%% h3_connection_key/4. Replayed on the next connect to resume.
h3_sessions = #{}
}).
-define(DEFAULT_MAX_CONNECTIONS, 50).
-define(DEFAULT_KEEPALIVE_TIMEOUT, 2000). % 2 seconds max idle
-define(DEFAULT_PREWARM_COUNT, 4). % Connections to maintain per host
-define(STOP_CONN_TIMEOUT, 100). % Max wait for a conn to stop
-define(PREWARM_CONNECT_TIMEOUT, 5000). % Dial budget for a prewarm conn
%% Every question the pool asks a conn about its own health is answered from
%% the conn's state, so a healthy conn answers at once. A conn that does not
%% is wedged, and waiting on it from inside the pool gen_server blocks every
%% caller of the pool, not just the one that asked: treat slow as unusable.
-define(PROBE_TIMEOUT, 250).
start() ->
%% Create ETS table to store pool pid by name
_ = ets:new(?MODULE, [set, public, named_table, {read_concurrency, true}]),
ok.
%% @doc Checkout a connection process from the pool.
%% Returns {ok, PoolInfo, Pid} where Pid is a hackney_conn process.
-spec checkout(Host :: string(), Port :: non_neg_integer(),
Transport :: module(), Options :: list()) ->
{ok, term(), pid()} | {error, term()}.
checkout(Host, Port, Transport, Options) ->
Requester = self(),
try
do_checkout(Requester, Host, Port, Transport, Options)
catch
exit:{timeout, _} ->
?report_trace("pool: checkout timeout", []),
{error, checkout_timeout};
_:Error ->
?report_trace("pool: checkout failure", [{error, Error}]),
{error, checkout_failure}
end.
do_checkout(Requester, Host, Port, Transport, Opts) ->
ConnectTimeout = proplists:get_value(connect_timeout, Opts, 8000),
CheckoutTimeout = proplists:get_value(checkout_timeout, Opts, ConnectTimeout),
PoolName = proplists:get_value(pool, Opts, default),
Pool = find_pool(PoolName, Opts),
Key = connection_key(Host, Port, Transport),
case gen_server:call(Pool, {checkout, Key, Requester, Opts}, CheckoutTimeout) of
{ok, Pid} ->
%% Return pool info for later checkin
PoolInfo = {PoolName, Key, Pool, Transport},
{ok, PoolInfo, Pid};
{error, _} = Error ->
Error;
{'EXIT', {timeout, _}} ->
{error, checkout_timeout}
end.
%% @doc Checkout a connection for an HTTPS request with SSL pooling enabled.
%% Reuses a pooled upgraded HTTPS/1.1 connection when the hash of its TLS
%% options (`tls_key' in Options) matches exactly, returning `ready'.
%% Otherwise a TCP connection is handed out as `needs_upgrade' and the
%% caller performs the TLS upgrade.
-spec checkout_ssl(Host :: string(), Port :: non_neg_integer(),
Transport :: module(), Options :: list()) ->
{ok, term(), pid(), ready | needs_upgrade} | {error, term()}.
checkout_ssl(Host, Port, Transport, Options) ->
Requester = self(),
try
do_checkout_ssl(Requester, Host, Port, Transport, Options)
catch
exit:{timeout, _} ->
?report_trace("pool: checkout_ssl timeout", []),
{error, checkout_timeout};
_:Error ->
?report_trace("pool: checkout_ssl failure", [{error, Error}]),
{error, checkout_failure}
end.
do_checkout_ssl(Requester, Host, Port, Transport, Opts) ->
ConnectTimeout = proplists:get_value(connect_timeout, Opts, 8000),
CheckoutTimeout = proplists:get_value(checkout_timeout, Opts, ConnectTimeout),
PoolName = proplists:get_value(pool, Opts, default),
Pool = find_pool(PoolName, Opts),
TlsKey = proplists:get_value(tls_key, Opts, default),
SslKey = connection_key(Host, Port, Transport, TlsKey),
case gen_server:call(Pool, {checkout_ssl, SslKey, Requester, Opts}, CheckoutTimeout) of
{ok, Pid, ConnState} ->
PoolInfo = {PoolName, SslKey, Pool, Transport},
{ok, PoolInfo, Pid, ConnState};
{error, _} = Error ->
Error;
{'EXIT', {timeout, _}} ->
{error, checkout_timeout}
end.
%% @doc Return a connection process to the pool.
-spec checkin(PoolInfo :: term(), Pid :: pid()) -> ok.
checkin({_PoolName, _Key, Pool, _Transport}, Pid) ->
gen_server:cast(Pool, {checkin, nil, Pid}),
ok.
%%====================================================================
%% HTTP/2 Connection Pooling
%%====================================================================
%% @doc Get an existing HTTP/2 connection for a host/port, or 'none' if not available.
%% HTTP/2 connections are shared (multiplexed) across callers.
-spec checkout_h2(Host :: string(), Port :: non_neg_integer(),
Transport :: module(), Options :: list()) ->
{ok, pid()} | none.
checkout_h2(Host, Port, Transport, Options) ->
PoolName = proplists:get_value(pool, Options, default),
ConnectTimeout = proplists:get_value(connect_timeout, Options, 8000),
Pool = find_pool(PoolName, Options),
Key = h2_connection_key(Host, Port, Transport, Options),
try
gen_server:call(Pool, {checkout_h2, Key}, ConnectTimeout)
catch
exit:{timeout, _} -> none;
_:_ -> none
end.
%% @doc Register an HTTP/2 connection in the pool for sharing.
%% Called after ALPN negotiation confirms HTTP/2.
-spec register_h2(Host :: string(), Port :: non_neg_integer(),
Transport :: module(), Pid :: pid(), Options :: list()) -> ok.
register_h2(Host, Port, Transport, Pid, Options) ->
PoolName = proplists:get_value(pool, Options, default),
Pool = find_pool(PoolName, Options),
Key = h2_connection_key(Host, Port, Transport, Options),
gen_server:cast(Pool, {register_h2, Key, Pid}),
ok.
%% @doc Remove an HTTP/2 connection from the pool (e.g., on GOAWAY).
-spec unregister_h2(Pid :: pid(), Options :: list()) -> ok.
unregister_h2(Pid, Options) ->
PoolName = proplists:get_value(pool, Options, default),
Pool = find_pool(PoolName, Options),
gen_server:cast(Pool, {unregister_h2, Pid}),
ok.
%% @doc Remove all HTTP/2 connections from the default pool.
%% Used for testing to ensure clean state between tests.
-spec unregister_h2_all() -> ok.
unregister_h2_all() ->
Pool = find_pool(default, []),
gen_server:call(Pool, unregister_h2_all).
%%====================================================================
%% HTTP/3 Connection Pooling
%%====================================================================
%% @doc Get an existing HTTP/3 connection for a host/port, or 'none' if not available.
%% HTTP/3 connections are shared (multiplexed) across callers via QUIC streams.
-spec checkout_h3(Host :: string(), Port :: non_neg_integer(),
Transport :: module(), Options :: list()) ->
{ok, pid()} | none.
checkout_h3(Host, Port, Transport, Options) ->
PoolName = proplists:get_value(pool, Options, default),
ConnectTimeout = proplists:get_value(connect_timeout, Options, 8000),
Pool = find_pool(PoolName, Options),
Key = h3_connection_key(Host, Port, Transport, Options),
try
gen_server:call(Pool, {checkout_h3, Key}, ConnectTimeout)
catch
exit:{timeout, _} -> none;
_:_ -> none
end.
%% @doc Register an HTTP/3 connection in the pool for sharing.
%% Called after QUIC connection is established with HTTP/3.
-spec register_h3(Host :: string(), Port :: non_neg_integer(),
Transport :: module(), Pid :: pid(), Options :: list()) -> ok.
register_h3(Host, Port, Transport, Pid, Options) ->
PoolName = proplists:get_value(pool, Options, default),
Pool = find_pool(PoolName, Options),
Key = h3_connection_key(Host, Port, Transport, Options),
gen_server:cast(Pool, {register_h3, Key, Pid}),
ok.
%% @doc Remove an HTTP/3 connection from the pool (e.g., on connection close).
-spec unregister_h3(Pid :: pid(), Options :: list()) -> ok.
unregister_h3(Pid, Options) ->
PoolName = proplists:get_value(pool, Options, default),
Pool = find_pool(PoolName, Options),
gen_server:cast(Pool, {unregister_h3, Pid}),
ok.
%% @doc Look up a cached HTTP/3 0-RTT/resumption session ticket for a host/port.
-spec get_h3_session(Host :: string(), Port :: non_neg_integer(),
Transport :: module(), Options :: list()) ->
{ok, term()} | none.
get_h3_session(Host, Port, Transport, Options) ->
PoolName = proplists:get_value(pool, Options, default),
Pool = find_pool(PoolName, Options),
Key = h3_connection_key(Host, Port, Transport, Options),
try
gen_server:call(Pool, {get_h3_session, Key})
catch
_:_ -> none
end.
%% @doc Cache an HTTP/3 session ticket for a host/port for later resumption.
-spec store_h3_session(Host :: string(), Port :: non_neg_integer(),
Transport :: module(), Ticket :: term(),
Options :: list()) -> ok.
store_h3_session(Host, Port, Transport, Ticket, Options) ->
PoolName = proplists:get_value(pool, Options, default),
Pool = find_pool(PoolName, Options),
Key = h3_connection_key(Host, Port, Transport, Options),
gen_server:cast(Pool, {store_h3_session, Key, Ticket}),
ok.
%% @doc Invalidate a cached HTTP/3 session ticket (e.g. after 0-RTT rejection).
-spec delete_h3_session(Host :: string(), Port :: non_neg_integer(),
Transport :: module(), Options :: list()) -> ok.
delete_h3_session(Host, Port, Transport, Options) ->
PoolName = proplists:get_value(pool, Options, default),
Pool = find_pool(PoolName, Options),
Key = h3_connection_key(Host, Port, Transport, Options),
gen_server:cast(Pool, {delete_h3_session, Key}),
ok.
get_stats(Pool) ->
gen_server:call(find_pool(Pool), stats).
%% @doc start a pool
start_pool(Name, Options) ->
case find_pool(Name, Options) of
Pid when is_pid(Pid) ->
ok;
Error ->
Error
end.
%% @doc stop a pool
stop_pool(Name) ->
case find_pool(Name) of
undefined ->
ok;
_Pid ->
case supervisor:terminate_child(hackney_sup, Name) of
ok ->
_ = supervisor:delete_child(hackney_sup, Name),
ets:delete(hackney_pool, Name),
ok;
Error ->
Error
end
end.
notify(Pool, Msg) ->
case find_pool(Pool) of
undefined -> ok;
Pid -> Pid ! Msg
end.
%% @doc return a child spec suitable for embedding your pool in the supervisor
child_spec(Name, Options0) ->
Options = [{name, Name} | Options0],
{Name, {hackney_pool, start_link, [Name, Options]},
permanent, 10000, worker, [hackney_pool]}.
%% @doc get the number of connections in the pool
count(Name) ->
case find_pool(Name) of
undefined -> 0;
Pid -> gen_server:call(Pid, count)
end.
%% @doc get the number of connections in the pool for a key. A legacy
%% `{Host, Port, Transport}' tuple aggregates over all TLS buckets of the
%% triple; a `{Host, Port, Transport, TlsKey}' tuple counts one bucket.
count(Name, Key) ->
case find_pool(Name) of
undefined -> 0;
Pid -> gen_server:call(Pid, {count, Key})
end.
%% @doc get max pool size
max_connections(Name) ->
case find_pool(Name) of
undefined -> 0;
Pid -> gen_server:call(Pid, max_connections)
end.
%% @doc change the pool size
set_max_connections(Name, NewSize) ->
gen_server:cast(find_pool(Name), {set_maxconn, NewSize}).
%% @doc get timeout
timeout(Name) ->
case find_pool(Name) of
undefined -> 0;
Pid -> gen_server:call(Pid, timeout)
end.
%% @doc change the connection timeout
set_timeout(Name, NewTimeout) ->
gen_server:cast(find_pool(Name), {set_timeout, NewTimeout}).
%% @doc Prewarm connections to a host (default count from pool settings)
%% Starts the pool if it doesn't exist.
-spec prewarm(atom(), string() | binary(), inet:port_number()) -> ok.
prewarm(PoolName, Host, Port) ->
Pool = find_pool(PoolName, []),
gen_server:cast(Pool, {prewarm, Host, Port}).
%% @doc Prewarm a specific number of connections to a host
%% Starts the pool if it doesn't exist.
-spec prewarm(atom(), string() | binary(), inet:port_number(), non_neg_integer()) -> ok.
prewarm(PoolName, Host, Port, Count) ->
Pool = find_pool(PoolName, []),
gen_server:cast(Pool, {prewarm, Host, Port, Count}).
%% @doc Get per-host connection statistics.
%% Returns a proplist with:
%% - active: number of active requests (from load_regulation)
%% - in_use: connections checked out from pool
%% - free: connections available in pool
-spec host_stats(atom(), string() | binary(), inet:port_number()) ->
[{atom(), non_neg_integer()}].
host_stats(PoolName, Host, Port) ->
Active = hackney_load_regulation:current(Host, Port),
case find_pool(PoolName) of
undefined ->
[{active, Active}, {in_use, 0}, {free, 0}];
Pool ->
{InUse, Free} = gen_server:call(Pool, {host_stats, Host, Port}),
[{active, Active}, {in_use, InUse}, {free, Free}]
end.
to_pool_name(Name) when is_atom(Name) ->
list_to_atom("hackney_pool_" ++ atom_to_list(Name));
to_pool_name(Name) when is_list(Name) ->
list_to_atom("hackney_pool_" ++ Name);
to_pool_name(Name) when is_binary(Name) ->
to_pool_name(binary_to_list(Name)).
%% @private
do_start_pool(Name, Options) ->
Spec = child_spec(Name, Options),
case supervisor:start_child(hackney_sup, Spec) of
{ok, Pid} ->
Pid;
{error, {already_started, _}} ->
find_pool(Name, Options)
end.
find_pool(Name) ->
case ets:lookup(?MODULE, Name) of
[] ->
undefined;
[{_, Pid}] ->
Pid
end.
find_pool(Name, Options) ->
case ets:lookup(?MODULE, Name) of
[] ->
do_start_pool(Name, Options);
[{_, Pid}] ->
Pid
end.
start_link(Name, Options0) ->
Options = hackney_util:maybe_apply_defaults([max_connections, timeout],
Options0),
gen_server:start_link(?MODULE, [Name, Options], []).
%%====================================================================
%% gen_server callbacks
%%====================================================================
init([Name, Options]) ->
%% Trap exits so a supervisor shutdown (stop_pool -> terminate_child) runs
%% terminate/2 instead of killing the pool outright. terminate/2 releases the
%% load_regulation slots of in_use connections; without trapping exits it
%% would be skipped and those per-host slots would leak.
process_flag(trap_exit, true),
MaxConn = case proplists:get_value(pool_size, Options) of
undefined ->
proplists:get_value(max_connections, Options, ?DEFAULT_MAX_CONNECTIONS);
Size ->
Size
end,
%% keepalive_timeout: max idle time for pooled connections (capped at 2s)
%% Also accept 'timeout' for backward compatibility
RawTimeout = case proplists:get_value(keepalive_timeout, Options) of
undefined ->
proplists:get_value(timeout, Options, ?DEFAULT_KEEPALIVE_TIMEOUT);
KT ->
KT
end,
KeepaliveTimeout = min(RawTimeout, ?DEFAULT_KEEPALIVE_TIMEOUT),
%% prewarm_count: number of TCP connections to maintain per host
%% Check pool options first, then app env, then default
PrewarmCount = case proplists:get_value(prewarm_count, Options) of
undefined ->
hackney_app:get_app_env(prewarm_count, ?DEFAULT_PREWARM_COUNT);
PC ->
PC
end,
%% register the module
ets:insert(?MODULE, {Name, self()}),
{ok, #state{name=Name, max_connections=MaxConn,
keepalive_timeout=KeepaliveTimeout, prewarm_count=PrewarmCount}}.
handle_call(stats, _From, State) ->
{reply, handle_stats(State), State};
handle_call(count, _From, #state{available=Available, in_use=InUse}=State) ->
AvailCount = maps:fold(fun(_, Pids, Acc) -> Acc + length(Pids) end, 0, Available),
{reply, AvailCount + maps:size(InUse), State};
handle_call(timeout, _From, #state{keepalive_timeout=Timeout}=State) ->
{reply, Timeout, State};
handle_call(max_connections, _From, #state{max_connections=MaxConn}=State) ->
{reply, MaxConn, State};
handle_call({count, {Host, Port, Transport}}, _From, #state{available=Available}=State) ->
%% Legacy 3-tuple key: aggregate over all TLS buckets for the triple
Count = maps:fold(
fun({H, P, T, _K}, Pids, Acc) when H =:= Host, P =:= Port, T =:= Transport ->
Acc + length(Pids);
(_, _, Acc) -> Acc
end, 0, Available),
{reply, Count, State};
handle_call({count, Key}, _From, #state{available=Available}=State) ->
Count = case maps:find(Key, Available) of
{ok, Pids} -> length(Pids);
error -> 0
end,
{reply, Count, State};
handle_call({host_stats, Host, Port}, _From, #state{available=Available, in_use=InUse}=State) ->
%% Count in_use and free for this host (any transport)
HostLower = string:lowercase(Host),
InUseCount = maps:fold(
fun(_Pid, {H, P, _T, _K}, Acc) when H =:= HostLower, P =:= Port -> Acc + 1;
(_, _, Acc) -> Acc
end, 0, InUse),
FreeCount = maps:fold(
fun({H, P, _T, _K}, Pids, Acc) when H =:= HostLower, P =:= Port -> Acc + length(Pids);
(_, _, Acc) -> Acc
end, 0, Available),
{reply, {InUseCount, FreeCount}, State};
handle_call({checkout, Key, Requester, Opts}, _From, State) ->
#state{name=PoolName, max_connections=MaxConn,
available=Available, in_use=InUse} = State,
TotalInUse = maps:size(InUse),
?report_trace("pool: checkout request", [{pool, PoolName}, {key, Key},
{total_in_use, TotalInUse}, {max_conn, MaxConn}]),
case find_available(Key, Available) of
{ok, Pid, Available2} ->
%% Found an available connection - update owner to new requester
?report_debug("pool: reusing connection", [{pool, PoolName}, {pid, Pid}]),
case set_owner(Pid, Requester) of
ok ->
InUse2 = maps:put(Pid, Key, InUse),
{reply, {ok, Pid}, State#state{available=Available2, in_use=InUse2}};
{error, _} ->
%% #850: the connection closed between is_ready and
%% set_owner (server-side close race). It is already out of
%% Available2; drop it and start a fresh connection rather
%% than crashing the pool on a bad match.
case start_connection(Key, Requester, Opts, State#state{available=Available2}) of
{ok, Pid2, State2} ->
InUse2 = maps:put(Pid2, Key, State2#state.in_use),
{reply, {ok, Pid2}, State2#state{in_use=InUse2}};
{error, Reason} ->
{reply, {error, Reason}, State#state{available=Available2}}
end
end;
none ->
%% No pooled connection available. Per-host concurrency is already
%% capped by hackney_load_regulation, so start a connection even
%% when in_use has reached max_connections: it is an overflow
%% connection, closed at checkin rather than pooled (see do_checkin).
%% max_connections bounds the warm/idle pool, not the number of
%% concurrent connections.
?report_trace("pool: starting new connection",
[{pool, PoolName}, {overflow, TotalInUse >= MaxConn}]),
case start_connection(Key, Requester, Opts, State) of
{ok, Pid, State2} ->
InUse2 = maps:put(Pid, Key, State2#state.in_use),
{reply, {ok, Pid}, State2#state{in_use=InUse2}};
{error, Reason} ->
{reply, {error, Reason}, State}
end
end;
handle_call({checkout_ssl, SslKey, Requester, Opts}, _From, State) ->
#state{name=PoolName, available=Available, in_use=InUse} = State,
?report_trace("pool: checkout_ssl request", [{pool, PoolName}, {key, SslKey},
{total_in_use, maps:size(InUse)}]),
case find_available_ssl(SslKey, Available) of
{ok, Pid, Available2} ->
%% Found a pooled SSL connection with the same TLS options hash
?report_debug("pool: reusing ssl connection", [{pool, PoolName}, {pid, Pid}]),
case set_owner(Pid, Requester) of
ok ->
InUse2 = maps:put(Pid, SslKey, InUse),
{reply, {ok, Pid, ready},
State#state{available=Available2, in_use=InUse2}};
{error, _} ->
%% #850: the connection closed between is_ready and
%% set_owner. It is already out of Available2; fall back
%% to the TCP bucket with the dead conn dropped.
checkout_ssl_fallback(SslKey, Requester, Opts,
State#state{available=Available2})
end;
none ->
checkout_ssl_fallback(SslKey, Requester, Opts, State)
end;
handle_call({checkin_sync, Pid}, _From, State) ->
%% Synchronous checkin - caller waits for acknowledgement
%% Legacy format without SSL flag - need to query connection (may deadlock if called from connection)
State2 = do_checkin(Pid, State),
{reply, ok, State2};
handle_call({checkin_sync, Pid, ShouldClose}, _From, State) ->
%% Synchronous checkin with "should close" flag - avoids deadlock
%% ShouldClose is true if connection was SSL upgraded or is a proxy tunnel
State2 = do_checkin_with_close_flag(Pid, ShouldClose, State),
{reply, ok, State2};
handle_call({checkout_h2, Key}, _From, #state{h2_connections = H2Conns} = State) ->
%% HTTP/2 checkout - return existing connection if available.
%% Liveness check includes both process_alive and gen_statem state, so a
%% hackney_conn that already transitioned to `closed` (e.g. after an h2
%% GOAWAY) but has not yet been removed via 'DOWN' is not handed out.
case maps:get(Key, H2Conns, undefined) of
undefined ->
{reply, none, State};
Pid ->
case h2_conn_usable(Pid) of
true ->
{reply, {ok, Pid}, State};
false ->
H2Conns2 = maps:remove(Key, H2Conns),
{reply, none, State#state{h2_connections = H2Conns2}}
end
end;
handle_call({checkout_h3, Key}, _From, #state{h3_connections = H3Conns} = State) ->
%% HTTP/3 checkout - return existing connection if available
case maps:get(Key, H3Conns, undefined) of
undefined ->
{reply, none, State};
Pid ->
%% Verify connection is still alive
case erlang:is_process_alive(Pid) of
true ->
{reply, {ok, Pid}, State};
false ->
%% Connection died, remove from pool
H3Conns2 = maps:remove(Key, H3Conns),
{reply, none, State#state{h3_connections = H3Conns2}}
end
end;
handle_call({get_h3_session, Key}, _From, #state{h3_sessions = Sessions} = State) ->
case maps:get(Key, Sessions, undefined) of
undefined -> {reply, none, State};
Ticket -> {reply, {ok, Ticket}, State}
end;
handle_call(unregister_h2_all, _From, State) ->
%% Clear all HTTP/2 connections (for testing)
{reply, ok, State#state{h2_connections = #{}}}.
handle_cast({checkin, _PoolInfo, Pid}, State) ->
State2 = do_checkin(Pid, State),
{noreply, State2};
handle_cast({set_maxconn, MaxConn}, State) ->
{noreply, State#state{max_connections=MaxConn}};
handle_cast({set_timeout, NewTimeout}, State) ->
%% Cap at 2 seconds
Capped = min(NewTimeout, ?DEFAULT_KEEPALIVE_TIMEOUT),
{noreply, State#state{keepalive_timeout=Capped}};
handle_cast({prewarm, Host, Port}, #state{prewarm_count=Count}=State) ->
State2 = do_prewarm(Host, Port, Count, State),
{noreply, State2};
handle_cast({prewarm, Host, Port, Count}, State) ->
State2 = do_prewarm(Host, Port, Count, State),
{noreply, State2};
handle_cast({prewarm_checkin, Pid, Key}, State) ->
%% Add a prewarmed connection to the pool
#state{available=Available, pid_monitors=PidMonitors} = State,
%% Set owner to pool
hackney_conn:set_owner_async(Pid, self()),
%% Monitor the connection
MonRef = erlang:monitor(process, Pid),
PidMonitors2 = maps:put(Pid, MonRef, PidMonitors),
%% Add to available
Available2 = maps:update_with(Key, fun(Pids) -> [Pid | Pids] end, [Pid], Available),
{noreply, State#state{available=Available2, pid_monitors=PidMonitors2}};
handle_cast({register_h2, Key, Pid}, State) ->
%% Register an HTTP/2 connection for sharing
#state{h2_connections = H2Conns, pid_monitors = PidMonitors} = State,
%% Monitor the connection if not already monitored
PidMonitors2 = case maps:is_key(Pid, PidMonitors) of
true -> PidMonitors;
false ->
MonRef = erlang:monitor(process, Pid),
maps:put(Pid, MonRef, PidMonitors)
end,
%% Store HTTP/2 connection
H2Conns2 = maps:put(Key, Pid, H2Conns),
{noreply, State#state{h2_connections = H2Conns2, pid_monitors = PidMonitors2}};
handle_cast({unregister_h2, Pid}, State) ->
%% Remove an HTTP/2 connection from the pool
State2 = do_unregister_h2(Pid, State),
{noreply, State2};
handle_cast({register_h3, Key, Pid}, State) ->
%% Register an HTTP/3 connection for sharing
#state{h3_connections = H3Conns, pid_monitors = PidMonitors} = State,
%% Monitor the connection if not already monitored
PidMonitors2 = case maps:is_key(Pid, PidMonitors) of
true -> PidMonitors;
false ->
MonRef = erlang:monitor(process, Pid),
maps:put(Pid, MonRef, PidMonitors)
end,
%% Store HTTP/3 connection
H3Conns2 = maps:put(Key, Pid, H3Conns),
{noreply, State#state{h3_connections = H3Conns2, pid_monitors = PidMonitors2}};
handle_cast({unregister_h3, Pid}, State) ->
%% Remove an HTTP/3 connection from the pool
State2 = do_unregister_h3(Pid, State),
{noreply, State2};
handle_cast({store_h3_session, Key, Ticket}, #state{h3_sessions = Sessions} = State) ->
{noreply, State#state{h3_sessions = maps:put(Key, Ticket, Sessions)}};
handle_cast({delete_h3_session, Key}, #state{h3_sessions = Sessions} = State) ->
{noreply, State#state{h3_sessions = maps:remove(Key, Sessions)}};
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info({'DOWN', _MonRef, process, Pid, Reason}, State) ->
%% Connection process died, remove from available, in_use, h2_connections, and h3_connections
#state{name=PoolName, available=Available, in_use=InUse, pid_monitors=PidMonitors,
h2_connections=H2Conns, h3_connections=H3Conns} = State,
?report_trace("pool: connection DOWN", [{pool, PoolName}, {pid, Pid}, {reason, Reason},
{was_in_use, maps:is_key(Pid, InUse)}]),
%% Remove from available pools
Available2 = maps:fold(
fun(Key, Pids, Acc) ->
case lists:delete(Pid, Pids) of
[] -> maps:remove(Key, Acc);
Pids2 -> maps:put(Key, Pids2, Acc)
end
end,
Available,
Available
),
%% Remove from in_use and release load_regulation slot if it was checked out
InUse2 = case maps:take(Pid, InUse) of
{Key, NewInUse} ->
%% Connection died while in use - release the load_regulation slot
{Host, Port} = key_host_port(Key),
hackney_load_regulation:release(Host, Port),
NewInUse;
error ->
InUse
end,
%% Remove from HTTP/2 connections if present
H2Conns2 = maps:fold(
fun(Key, ConnPid, Acc) ->
case ConnPid of
Pid -> maps:remove(Key, Acc);
_ -> Acc
end
end,
H2Conns,
H2Conns
),
%% Remove from HTTP/3 connections if present
H3Conns2 = maps:fold(
fun(Key, ConnPid, Acc) when is_pid(ConnPid) ->
case ConnPid of
Pid -> maps:remove(Key, Acc);
_ -> Acc
end;
(_Key, _Val, Acc) -> Acc
end,
H3Conns,
H3Conns
),
PidMonitors2 = maps:remove(Pid, PidMonitors),
{noreply, State#state{available=Available2, in_use=InUse2, pid_monitors=PidMonitors2,
h2_connections=H2Conns2, h3_connections=H3Conns2}};
handle_info(_Info, State) ->
{noreply, State}.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
terminate(_Reason, #state{available=Available, in_use=InUse,
h2_connections=H2Conns, h3_connections=H3Conns,
pid_monitors=PidMonitors}) ->
%% Stop all available connections
maps:foreach(
fun(_Key, Pids) ->
lists:foreach(fun(Pid) ->
stop_conn(Pid)
end, Pids)
end,
Available
),
%% Release the load_regulation slot of every checked-out connection and stop
%% it. Without this, stopping a pool while requests are in flight orphans the
%% in_use conns: the pool's DOWN handler (which would release) is gone, so the
%% global per-host slots leak and that host's concurrency cap is starved
%% node-wide. Each Key carries the conn's host/port.
maps:foreach(
fun(Pid, Key) ->
{Host, Port} = key_host_port(Key),
hackney_load_regulation:release(Host, Port),
stop_conn(Pid)
end,
InUse
),
%% Stop all HTTP/2 connections
maps:foreach(
fun(_Key, Pid) ->
stop_conn(Pid)
end,
H2Conns
),
%% Stop all HTTP/3 connections
maps:foreach(
fun(_Key, Pid) ->
stop_conn(Pid)
end,
H3Conns
),
%% Demonitor all
maps:foreach(fun(_Pid, MonRef) -> erlang:demonitor(MonRef, [flush]) end, PidMonitors),
ok.
%%====================================================================
%% Internal functions
%%====================================================================
%% @private Key for pooled connections. The 4th element buckets pooled SSL
%% connections by the hash of their effective TLS options (ssl_pooling);
%% plain TCP connections all use the `default' bucket.
connection_key(Host, Port, Transport) ->
connection_key(Host, Port, Transport, default).
connection_key(Host0, Port, Transport, TlsKey) ->
Host = string:lowercase(Host0),
{Host, Port, Transport, TlsKey}.
%% @private Host and port of a pool connection key.
key_host_port({Host, Port, _Transport, _TlsKey}) ->
{Host, Port}.
%% @private Key for shared HTTP/2 connections. Includes the hash of the
%% effective TLS options (tls_key) so requests with different ssl_options
%% never share a connection. Callers that pass no tls_key all land in the
%% `default' bucket, preserving the previous behavior.
h2_connection_key(Host0, Port, Transport, Options) ->
Host = string:lowercase(Host0),
{Host, Port, Transport, proplists:get_value(tls_key, Options, default)}.
%% @private Key for shared HTTP/3 connections and cached 0-RTT session
%% tickets. Includes the hash of the QUIC trust projection (h3_tls_key) so
%% requests with differing trust configs never share a connection, and a
%% ticket obtained under one trust config is never resumed under another.
%% Callers that pass no h3_tls_key all land in the `default' bucket,
%% preserving the previous behavior.
h3_connection_key(Host0, Port, Transport, Options) ->
Host = string:lowercase(Host0),
{Host, Port, Transport, proplists:get_value(h3_tls_key, Options, default)}.
%% @private Stop a connection, tolerating an already-dead process. Bounded on
%% purpose: this runs inside the pool gen_server, and a conn wedged in a
%% transport call (the dial that just outlived its timeout, typically) would
%% otherwise hold every caller of the pool for as long as the transport takes
%% to return. Past the deadline the conn is killed.
stop_conn(Pid) ->
try hackney_conn:stop(Pid, ?STOP_CONN_TIMEOUT) catch _:_ -> ok end.
%% @private Find a reusable idle connection for `Key', discarding any that are
%% no longer keepalive-ready. Only a conn that is_ready reports `{ok, connected}'
%% is handed out; a closed conn is stopped and dropped (never reanimated). Fresh
%% dialing for an empty bucket is the caller's `none' branch, off the pool's hot
%% path. The SSL alias exists only to mark intent at the SSL checkout site.
find_available(Key, Available) ->
case maps:find(Key, Available) of
{ok, [Pid | Rest]} ->
Available2 = case Rest of
[] -> maps:remove(Key, Available);
_ -> maps:put(Key, Rest, Available)
end,
%% Verify connection is still alive and usable
case is_process_alive(Pid) of
true ->
%% is_ready checks both state and socket health in one call.
%% The connection can die between is_process_alive/1 above
%% and this gen_statem call (flaky network); the resulting
%% noproc exit must not crash the pool, so skip and move on.
try hackney_conn:is_ready(Pid, ?PROBE_TIMEOUT) of
{ok, connected} ->
{ok, Pid, Available2};
_ ->
%% Closed or unusable: discard it rather than redial
%% from inside the pool. Reanimating a closed pid would
%% break the "only keepalive conns are reused" invariant
%% and a redial here would block the pool on connect.
stop_conn(Pid),
find_available(Key, Available2)
catch
_:_ -> find_available(Key, Available2)
end;
false ->
find_available(Key, Available2)
end;
{ok, []} ->
none;
error ->
none
end.
%% @private SSL-bucket variant. Now identical to find_available/2 (closed conns
%% are always dropped, never redialed); kept as a named alias to mark intent at
%% the SSL checkout site.
find_available_ssl(Key, Available) ->
find_available(Key, Available).
%% @private SSL checkout miss: reuse or dial a TCP connection for the caller
%% to upgrade. It is recorded in in_use under the SSL key so the checkin
%% decision can tell it apart from plain TCP checkouts.
checkout_ssl_fallback(SslKey, Requester, Opts, State) ->
#state{name=PoolName, max_connections=MaxConn,
available=Available, in_use=InUse} = State,
{Host, Port} = key_host_port(SslKey),
TcpKey = connection_key(Host, Port, hackney_tcp),
TotalInUse = maps:size(InUse),
case find_available(TcpKey, Available) of
{ok, Pid, Available2} ->
case set_owner(Pid, Requester) of
ok ->
InUse2 = maps:put(Pid, SslKey, InUse),
{reply, {ok, Pid, needs_upgrade},
State#state{available=Available2, in_use=InUse2}};
{error, _} ->
%% #850 race again: drop the dead conn and dial fresh
start_ssl_checkout_conn(SslKey, Requester, Opts,
State#state{available=Available2})
end;
none ->
%% No pooled TCP connection. Per-host concurrency is already capped
%% by hackney_load_regulation, so start one even at max_connections:
%% it is an overflow connection, closed at checkin (see do_checkin)
%% rather than pooled. Mirrors the plain checkout path.
?report_trace("pool: starting new connection",
[{pool, PoolName}, {overflow, TotalInUse >= MaxConn}]),
start_ssl_checkout_conn(SslKey, Requester, Opts, State)
end.
%% @private Dial a fresh TCP connection for an SSL checkout. The caller
%% upgrades it, so the dialed transport is TCP even though the in_use key
%% carries hackney_ssl.
start_ssl_checkout_conn(SslKey, Requester, Opts, State) ->
{Host, Port} = key_host_port(SslKey),
case start_connection(Host, Port, hackney_tcp, Requester, Opts, State) of
{ok, Pid, State2} ->
InUse2 = maps:put(Pid, SslKey, State2#state.in_use),
{reply, {ok, Pid, needs_upgrade}, State2#state{in_use=InUse2}};
{error, Reason} ->
{reply, {error, Reason}, State}
end.
start_connection(Key, Owner, Opts, State) ->
%% Plain checkouts dial the key's transport; SSL-bucket checkouts dial