aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/neh/http2.cpp
blob: 766a344013bbdbf4aa4e54263f322116d3e09353 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
#include "http2.h"

#include "conn_cache.h"
#include "details.h"
#include "factory.h"
#include "http_common.h"
#include "smart_ptr.h"
#include "utils.h"

#include <library/cpp/http/push_parser/http_parser.h>
#include <library/cpp/http/misc/httpcodes.h>
#include <library/cpp/http/misc/parsed_request.h>
#include <library/cpp/neh/asio/executor.h>

#include <util/generic/singleton.h>
#include <util/generic/vector.h>
#include <util/network/iovec.h>
#include <util/stream/output.h>
#include <util/stream/zlib.h>
#include <util/system/condvar.h>
#include <util/system/mutex.h>
#include <util/system/spinlock.h>
#include <util/system/yassert.h>
#include <util/thread/factory.h>
#include <util/thread/singleton.h>
#include <util/system/sanitizers.h>
#include <util/system/thread.h>

#include <atomic>

#if defined(_unix_)
#include <sys/ioctl.h>
#endif

#if defined(_linux_)
#undef SIOCGSTAMP
#undef SIOCGSTAMPNS
#include <linux/sockios.h>
#define FIONWRITE SIOCOUTQ
#endif

//#define DEBUG_HTTP2

#ifdef DEBUG_HTTP2
#define DBGOUT(args) Cout << args << Endl;
#else
#define DBGOUT(args)
#endif

using namespace NDns;
using namespace NAsio;
using namespace NNeh;
using namespace NNeh::NHttp;
using namespace NNeh::NHttp2;
using namespace std::placeholders;

//
// has complex keep-alive references between entities in multi-thread enviroment,
// this create risks for races/memory leak, etc..
// so connecting/disconnecting entities must be doing carefully
//
// handler <=-> request <==> connection(socket) <= handlers, stored in io_service
//                           ^
//                           +== connections_cache
// '=>' -- shared/intrusive ptr
// '->' -- weak_ptr
//

static TDuration FixTimeoutForSanitizer(const TDuration timeout) {
    ui64 multiplier = 1;
    if (NSan::ASanIsOn()) {
        // https://github.com/google/sanitizers/wiki/AddressSanitizer
        multiplier = 4;
    } else if (NSan::MSanIsOn()) {
        // via https://github.com/google/sanitizers/wiki/MemorySanitizer
        multiplier = 3;
    } else if (NSan::TSanIsOn()) {
        // via https://clang.llvm.org/docs/ThreadSanitizer.html
        multiplier = 15;
    }

    return TDuration::FromValue(timeout.GetValue() * multiplier);
}

TDuration THttp2Options::ConnectTimeout = FixTimeoutForSanitizer(TDuration::MilliSeconds(1000));
TDuration THttp2Options::InputDeadline = TDuration::Max();
TDuration THttp2Options::OutputDeadline = TDuration::Max();
TDuration THttp2Options::SymptomSlowConnect = FixTimeoutForSanitizer(TDuration::MilliSeconds(10));
size_t THttp2Options::InputBufferSize = 16 * 1024;
bool THttp2Options::KeepInputBufferForCachedConnections = false;
size_t THttp2Options::AsioThreads = 4;
size_t THttp2Options::AsioServerThreads = 4;
bool THttp2Options::EnsureSendingCompleteByAck = false;
int THttp2Options::Backlog = 100;
TDuration THttp2Options::ServerInputDeadline = FixTimeoutForSanitizer(TDuration::MilliSeconds(500));
TDuration THttp2Options::ServerOutputDeadline = TDuration::Max();
TDuration THttp2Options::ServerInputDeadlineKeepAliveMax = FixTimeoutForSanitizer(TDuration::Seconds(120));
TDuration THttp2Options::ServerInputDeadlineKeepAliveMin = FixTimeoutForSanitizer(TDuration::Seconds(10));
bool THttp2Options::ServerUseDirectWrite = false;
bool THttp2Options::UseResponseAsErrorMessage = false;
bool THttp2Options::FullHeadersAsErrorMessage = false;
bool THttp2Options::ErrorDetailsAsResponseBody = false;
bool THttp2Options::RedirectionNotError = false;
bool THttp2Options::AnyResponseIsNotError = false;
bool THttp2Options::TcpKeepAlive = false;
i32 THttp2Options::LimitRequestsPerConnection = -1;
bool THttp2Options::QuickAck = false;
bool THttp2Options::UseAsyncSendRequest = false;
bool THttp2Options::RespectHostInHttpServerNetworkAddress = false;

bool THttp2Options::Set(TStringBuf name, TStringBuf value) {
#define HTTP2_TRY_SET(optType, optName)       \
    if (name == TStringBuf(#optName)) {      \
        optName = FromString<optType>(value); \
    }

    HTTP2_TRY_SET(TDuration, ConnectTimeout)
    else HTTP2_TRY_SET(TDuration, InputDeadline)
    else HTTP2_TRY_SET(TDuration, OutputDeadline)
    else HTTP2_TRY_SET(TDuration, SymptomSlowConnect) else HTTP2_TRY_SET(size_t, InputBufferSize) else HTTP2_TRY_SET(bool, KeepInputBufferForCachedConnections) else HTTP2_TRY_SET(size_t, AsioThreads) else HTTP2_TRY_SET(size_t, AsioServerThreads) else HTTP2_TRY_SET(bool, EnsureSendingCompleteByAck) else HTTP2_TRY_SET(int, Backlog) else HTTP2_TRY_SET(TDuration, ServerInputDeadline) else HTTP2_TRY_SET(TDuration, ServerOutputDeadline) else HTTP2_TRY_SET(TDuration, ServerInputDeadlineKeepAliveMax) else HTTP2_TRY_SET(TDuration, ServerInputDeadlineKeepAliveMin) else HTTP2_TRY_SET(bool, ServerUseDirectWrite) else HTTP2_TRY_SET(bool, UseResponseAsErrorMessage) else HTTP2_TRY_SET(bool, FullHeadersAsErrorMessage) else HTTP2_TRY_SET(bool, ErrorDetailsAsResponseBody) else HTTP2_TRY_SET(bool, RedirectionNotError) else HTTP2_TRY_SET(bool, AnyResponseIsNotError) else HTTP2_TRY_SET(bool, TcpKeepAlive) else HTTP2_TRY_SET(i32, LimitRequestsPerConnection) else HTTP2_TRY_SET(bool, QuickAck)
    else HTTP2_TRY_SET(bool, UseAsyncSendRequest) else {
        return false;
    }
    return true;
}

namespace NNeh {
    const NDns::TResolvedHost* Resolve(const TStringBuf host, ui16 port, NHttp::EResolverType resolverType);
}

namespace {
//#define DEBUG_STAT

#ifdef DEBUG_STAT
    struct TDebugStat {
        static std::atomic<size_t> ConnTotal;
        static std::atomic<size_t> ConnActive;
        static std::atomic<size_t> ConnCached;
        static std::atomic<size_t> ConnDestroyed;
        static std::atomic<size_t> ConnFailed;
        static std::atomic<size_t> ConnConnCanceled;
        static std::atomic<size_t> ConnSlow;
        static std::atomic<size_t> Conn2Success;
        static std::atomic<size_t> ConnPurgedInCache;
        static std::atomic<size_t> ConnDestroyedInCache;
        static std::atomic<size_t> RequestTotal;
        static std::atomic<size_t> RequestSuccessed;
        static std::atomic<size_t> RequestFailed;
        static void Print() {
            Cout << "ct=" << ConnTotal.load(std::memory_order_acquire)
                 << " ca=" << ConnActive.load(std::memory_order_acquire)
                 << " cch=" << ConnCached.load(std::memory_order_acquire)
                 << " cd=" << ConnDestroyed.load(std::memory_order_acquire)
                 << " cf=" << ConnFailed.load(std::memory_order_acquire)
                 << " ccc=" << ConnConnCanceled.load(std::memory_order_acquire)
                 << " csl=" << ConnSlow.load(std::memory_order_acquire)
                 << " c2s=" << Conn2Success.load(std::memory_order_acquire)
                 << " cpc=" << ConnPurgedInCache.load(std::memory_order_acquire)
                 << " cdc=" << ConnDestroyedInCache.load(std::memory_order_acquire)
                 << " rt=" << RequestTotal.load(std::memory_order_acquire)
                 << " rs=" << RequestSuccessed.load(std::memory_order_acquire)
                 << " rf=" << RequestFailed.load(std::memory_order_acquire)
                 << Endl;
        }
    };
    std::atomic<size_t> TDebugStat::ConnTotal = 0;
    std::atomic<size_t> TDebugStat::ConnActive = 0;
    std::atomic<size_t> TDebugStat::ConnCached = 0;
    std::atomic<size_t> TDebugStat::ConnDestroyed = 0;
    std::atomic<size_t> TDebugStat::ConnFailed = 0;
    std::atomic<size_t> TDebugStat::ConnConnCanceled = 0;
    std::atomic<size_t> TDebugStat::ConnSlow = 0;
    std::atomic<size_t> TDebugStat::Conn2Success = 0;
    std::atomic<size_t> TDebugStat::ConnPurgedInCache = 0;
    std::atomic<size_t> TDebugStat::ConnDestroyedInCache = 0;
    std::atomic<size_t> TDebugStat::RequestTotal = 0;
    std::atomic<size_t> TDebugStat::RequestSuccessed = 0;
    std::atomic<size_t> TDebugStat::RequestFailed = 0;
#endif

    inline void PrepareSocket(SOCKET s, const TRequestSettings& requestSettings = TRequestSettings()) {
        if (requestSettings.NoDelay) {
            SetNoDelay(s, true);
        }
    }

    bool Compress(TData& data, const TString& compressionScheme) {
        if (compressionScheme == "gzip" && data.size() > 23) {  // there is no string less than 24 bytes long that might be compressed with gzip
            try {
                TData gzipped(data.size());
                TMemoryOutput out(gzipped.data(), gzipped.size());
                TZLibCompress c(&out, ZLib::GZip);
                c.Write(data.data(), data.size());
                c.Finish();
                gzipped.resize(out.Buf() - gzipped.data());
                data.swap(gzipped);
                return true;
            } catch (yexception&) {
                // gzipped data occupies more space than original data
            }
        }
        return false;
    }

    class THttpRequestBuffers: public NAsio::TTcpSocket::IBuffers {
    public:
        THttpRequestBuffers(TRequestData::TPtr rd)
            : Req_(rd)
            , Parts_(Req_->Parts())
            , IOvec_(Parts_.data(), Parts_.size())
        {
        }

        TContIOVector* GetIOvec() override {
            return &IOvec_;
        }

    private:
        TRequestData::TPtr Req_;
        TVector<IOutputStream::TPart> Parts_;
        TContIOVector IOvec_;
    };

    struct TRequestGet1: public TRequestGet {
        static inline TStringBuf Name() noexcept {
            return TStringBuf("http");
        }
    };

    struct TRequestPost1: public TRequestPost {
        static inline TStringBuf Name() noexcept {
            return TStringBuf("post");
        }
    };

    struct TRequestFull1: public TRequestFull {
        static inline TStringBuf Name() noexcept {
            return TStringBuf("full");
        }
    };

    struct TRequestGet2: public TRequestGet {
        static inline TStringBuf Name() noexcept {
            return TStringBuf("http2");
        }
    };

    struct TRequestPost2: public TRequestPost {
        static inline TStringBuf Name() noexcept {
            return TStringBuf("post2");
        }
    };

    struct TRequestFull2: public TRequestFull {
        static inline TStringBuf Name() noexcept {
            return TStringBuf("full2");
        }
    };

    struct TRequestUnixSocketGet: public TRequestGet {
        static inline TStringBuf Name() noexcept {
            return TStringBuf("http+unix");
        }

        static TRequestSettings RequestSettings() {
            return TRequestSettings()
                .SetNoDelay(false)
                .SetResolverType(EResolverType::EUNIXSOCKET);
        }
    };

    struct TRequestUnixSocketPost: public TRequestPost {
        static inline TStringBuf Name() noexcept {
            return TStringBuf("post+unix");
        }

        static TRequestSettings RequestSettings() {
            return TRequestSettings()
                .SetNoDelay(false)
                .SetResolverType(EResolverType::EUNIXSOCKET);
        }
    };

    struct TRequestUnixSocketFull: public TRequestFull {
        static inline TStringBuf Name() noexcept {
            return TStringBuf("full+unix");
        }

        static TRequestSettings RequestSettings() {
            return TRequestSettings()
                .SetNoDelay(false)
                .SetResolverType(EResolverType::EUNIXSOCKET);
        }
    };

    typedef TAutoPtr<THttpRequestBuffers> THttpRequestBuffersPtr;

    class THttpRequest;
    typedef TSharedPtrB<THttpRequest> THttpRequestRef;

    class THttpConn;
    typedef TIntrusivePtr<THttpConn> THttpConnRef;

    typedef std::function<TRequestData::TPtr(const TMessage&, const TParsedLocation&)> TRequestBuilder;

    class THttpRequest {
    public:
        class THandle: public TSimpleHandle {
        public:
            THandle(IOnRecv* f, const TMessage& msg, TStatCollector* s) noexcept
                : TSimpleHandle(f, msg, s)
            {
            }

            bool MessageSendedCompletely() const noexcept override {
                if (TSimpleHandle::MessageSendedCompletely()) {
                    return true;
                }

                THttpRequestRef req(GetRequest());
                if (!!req && req->RequestSendedCompletely()) {
                    const_cast<THandle*>(this)->SetSendComplete();
                }

                return TSimpleHandle::MessageSendedCompletely();
            }

            void Cancel() noexcept override {
                if (TSimpleHandle::Canceled()) {
                    return;
                }

                THttpRequestRef req(GetRequest());
                if (!!req) {
                    TSimpleHandle::Cancel();
                    req->Cancel();
                }
            }

            void NotifyError(TErrorRef error, const THttpParser* rsp = nullptr) {
#ifdef DEBUG_STAT
                ++TDebugStat::RequestFailed;
#endif
                if (rsp) {
                    TSimpleHandle::NotifyError(error, rsp->DecodedContent(), rsp->FirstLine(), rsp->Headers());
                } else {
                    TSimpleHandle::NotifyError(error);
                }

                ReleaseRequest();
            }

            //not thread safe!
            void SetRequest(const TWeakPtrB<THttpRequest>& r) noexcept {
                Req_ = r;
            }

        private:
            THttpRequestRef GetRequest() const noexcept {
                TGuard<TSpinLock> g(SP_);
                return Req_;
            }

            void ReleaseRequest() noexcept {
                TWeakPtrB<THttpRequest> tmp;
                TGuard<TSpinLock> g(SP_);
                tmp.Swap(Req_);
            }

            mutable TSpinLock SP_;
            TWeakPtrB<THttpRequest> Req_;
        };

        typedef TIntrusivePtr<THandle> THandleRef;

        static void Run(THandleRef& h, const TMessage& msg, TRequestBuilder f, const TRequestSettings& s) {
            THttpRequestRef req(new THttpRequest(h, msg, f, s));
            req->WeakThis_ = req;
            h->SetRequest(req->WeakThis_);
            req->Run(req);
        }

        ~THttpRequest() {
            DBGOUT("~THttpRequest()");
        }

    private:
        THttpRequest(THandleRef& h, TMessage msg, TRequestBuilder f, const TRequestSettings& s)
            : Hndl_(h)
            , RequestBuilder_(f)
            , RequestSettings_(s)
            , Msg_(std::move(msg))
            , Loc_(Msg_.Addr)
            , Addr_(Resolve(Loc_.Host, Loc_.GetPort(), RequestSettings_.ResolverType))
            , AddrIter_(Addr_->Addr.Begin())
            , Canceled_(false)
            , RequestSendedCompletely_(false)
        {
        }

        void Run(THttpRequestRef& req);

    public:
        THttpRequestBuffersPtr BuildRequest() {
            return new THttpRequestBuffers(RequestBuilder_(Msg_, Loc_));
        }

        TRequestSettings RequestSettings() {
            return RequestSettings_;
        }

        //can create a spare socket in an attempt to decrease connecting time
        void OnDetectSlowConnecting();

        //remove extra connection on success connec
        void OnConnect(THttpConn* c);

        //have some response input
        void OnBeginRead() noexcept {
            RequestSendedCompletely_ = true;
        }

        void OnResponse(TAutoPtr<THttpParser>& rsp);

        void OnConnectFailed(THttpConn* c, const TErrorCode& ec);
        void OnSystemError(THttpConn* c, const TErrorCode& ec);
        void OnError(THttpConn* c, const TString& errorText);

        bool RequestSendedCompletely() noexcept;

        void Cancel() noexcept;

    private:
        void NotifyResponse(const TString& resp, const TString& firstLine, const THttpHeaders& headers) {
            THandleRef h(ReleaseHandler());
            if (!!h) {
                h->NotifyResponse(resp, firstLine, headers);
            }
        }

        void NotifyError(
            const TString& errorText,
            TError::TType errorType = TError::UnknownType,
            i32 errorCode = 0, i32 systemErrorCode = 0) {
            NotifyError(new TError(errorText, errorType, errorCode, systemErrorCode));
        }

        void NotifyError(TErrorRef error, const THttpParser* rsp = nullptr) {
            THandleRef h(ReleaseHandler());
            if (!!h) {
                h->NotifyError(error, rsp);
            }
        }

        void Finalize(THttpConn* skipConn = nullptr) noexcept;

        inline THandleRef ReleaseHandler() noexcept {
            THandleRef h;
            {
                TGuard<TSpinLock> g(SL_);
                h.Swap(Hndl_);
            }
            return h;
        }

        inline THttpConnRef GetConn() noexcept {
            TGuard<TSpinLock> g(SL_);
            return Conn_;
        }

        inline THttpConnRef ReleaseConn() noexcept {
            THttpConnRef c;
            {
                TGuard<TSpinLock> g(SL_);
                c.Swap(Conn_);
            }
            return c;
        }

        inline THttpConnRef ReleaseConn2() noexcept {
            THttpConnRef c;
            {
                TGuard<TSpinLock> g(SL_);
                c.Swap(Conn2_);
            }
            return c;
        }

        TSpinLock SL_; //guaranted calling notify() only once (prevent race between asio thread and current)
        THandleRef Hndl_;
        TRequestBuilder RequestBuilder_;
        TRequestSettings RequestSettings_;
        const TMessage Msg_;
        const TParsedLocation Loc_;
        const TResolvedHost* Addr_;
        TNetworkAddress::TIterator AddrIter_;
        THttpConnRef Conn_;
        THttpConnRef Conn2_; //concurrent connection used, if detected slow connecting on first connection
        TWeakPtrB<THttpRequest> WeakThis_;
        TAtomicBool Canceled_;
        TAtomicBool RequestSendedCompletely_;
    };

    TAtomicCounter* HttpOutConnCounter();

    class THttpConn: public TThrRefBase {
    public:
        static THttpConnRef Create(TIOService& srv);

        ~THttpConn() override {
            DBGOUT("~THttpConn()");
            Req_.Reset();
            HttpOutConnCounter()->Dec();
#ifdef DEBUG_STAT
            ++TDebugStat::ConnDestroyed;
#endif
        }

        void StartRequest(THttpRequestRef req, const TEndpoint& ep, size_t addrId, TDuration slowConn) {
            {
                //thread safe linking connection->request
                TGuard<TSpinLock> g(SL_);
                Req_ = req;
            }
            AddrId_ = addrId;
            try {
                TDuration connectDeadline(THttp2Options::ConnectTimeout);
                if (THttp2Options::ConnectTimeout > slowConn) {
                    //use append non fatal connect deadline, so on first timedout
                    //report about slow connecting to THttpRequest, and continue wait ConnectDeadline_ period
                    connectDeadline = slowConn;
                    ConnectDeadline_ = THttp2Options::ConnectTimeout - slowConn;
                }
                DBGOUT("AsyncConnect to " << ep.IpToString());
                AS_.AsyncConnect(ep, std::bind(&THttpConn::OnConnect, THttpConnRef(this), _1, _2), connectDeadline);
            } catch (...) {
                ReleaseRequest();
                throw;
            }
        }

        //start next request on keep-alive connection
        bool StartNextRequest(THttpRequestRef& req) {
            if (Finalized_) {
                return false;
            }

            {
                //thread safe linking connection->request
                TGuard<TSpinLock> g(SL_);
                Req_ = req;
            }

            RequestWritten_ = false;
            BeginReadResponse_ = false;

            try {
                TErrorCode ec;
                SendRequest(req->BuildRequest(), ec); //throw std::bad_alloc
                if (ec.Value() == ECANCELED) {
                    OnCancel();
                } else if (ec) {
                    OnError(ec);
                }
            } catch (...) {
                OnError(CurrentExceptionMessage());
                throw;
            }
            return true;
        }

        //connection received from cache must be validated before using
        //(process removing closed conection from cache consume some time)
        inline bool IsValid() const noexcept {
            return !Finalized_;
        }

        void SetCached(bool v) noexcept {
            Cached_ = v;
        }

        void Close() noexcept {
            try {
                Cancel();
            } catch (...) {
            }
        }

        void DetachRequest() noexcept {
            ReleaseRequest();
        }

        void Cancel() { //throw std::bad_alloc
            if (!Canceled_) {
                Canceled_ = true;
                Finalized_ = true;
                OnCancel();
                AS_.AsyncCancel();
            }
        }

        void OnCancel() {
            THttpRequestRef r(ReleaseRequest());
            if (!!r) {
                static const TString reqCanceled("request canceled");
                r->OnError(this, reqCanceled);
            }
        }

        bool RequestSendedCompletely() const noexcept {
            DBGOUT("RequestSendedCompletely()");
            if (!Connected_ || !RequestWritten_) {
                return false;
            }
            if (BeginReadResponse_) {
                return true;
            }
#if defined(FIONWRITE)
            if (THttp2Options::EnsureSendingCompleteByAck) {
                int nbytes = Max<int>();
                int err = ioctl(AS_.Native(), FIONWRITE, &nbytes);
                return err ? false : nbytes == 0;
            }
#endif
            return true;
        }

        TIOService& GetIOService() const noexcept {
            return AS_.GetIOService();
        }

    private:
        THttpConn(TIOService& srv)
            : AddrId_(0)
            , AS_(srv)
            , BuffSize_(THttp2Options::InputBufferSize)
            , Connected_(false)
            , Cached_(false)
            , Canceled_(false)
            , Finalized_(false)
            , InAsyncRead_(false)
            , RequestWritten_(false)
            , BeginReadResponse_(false)
        {
            HttpOutConnCounter()->Inc();
        }

        //can be called only from asio
        void OnConnect(const TErrorCode& ec, IHandlingContext& ctx) {
            DBGOUT("THttpConn::OnConnect: " << ec.Value());
            if (Y_UNLIKELY(ec)) {
                if (ec.Value() == ETIMEDOUT && ConnectDeadline_.GetValue()) {
                    //detect slow connecting (yet not reached final timeout)
                    DBGOUT("OnConnectTimingCheck");
                    THttpRequestRef req(GetRequest());
                    if (!req) {
                        return; //cancel from client thread can ahead us
                    }
                    TDuration newDeadline(ConnectDeadline_);
                    ConnectDeadline_ = TDuration::Zero(); //next timeout is final

                    req->OnDetectSlowConnecting();
                    //continue wait connect
                    ctx.ContinueUseHandler(newDeadline);

                    return;
                }
#ifdef DEBUG_STAT
                if (ec.Value() != ECANCELED) {
                    ++TDebugStat::ConnFailed;
                } else {
                    ++TDebugStat::ConnConnCanceled;
                }
#endif
                if (ec.Value() == EIO) {
                    //try get more detail error info
                    char buf[1];
                    TErrorCode errConnect;
                    AS_.ReadSome(buf, 1, errConnect);
                    OnConnectFailed(errConnect.Value() ? errConnect : ec);
                } else if (ec.Value() == ECANCELED) {
                    // not try connecting to next host ip addr, simple fail
                    OnError(ec);
                } else {
                    OnConnectFailed(ec);
                }
            } else {
                Connected_ = true;

                THttpRequestRef req(GetRequest());
                if (!req || Canceled_) {
                    return;
                }

                try {
                    PrepareSocket(AS_.Native(), req->RequestSettings());
                    if (THttp2Options::TcpKeepAlive) {
                        SetKeepAlive(AS_.Native(), true);
                    }
                } catch (TSystemError& err) {
                    TErrorCode ec2(err.Status());
                    OnError(ec2);
                    return;
                }

                req->OnConnect(this);

                THttpRequestBuffersPtr ptr(req->BuildRequest());
                PrepareParser();

                TErrorCode ec3;
                SendRequest(ptr, ec3);
                if (ec3) {
                    OnError(ec3);
                }
            }
        }

        void PrepareParser() {
            Prs_ = new THttpParser();
            Prs_->Prepare();
        }

        void SendRequest(const THttpRequestBuffersPtr& bfs, TErrorCode& ec) { //throw std::bad_alloc
            if (!THttp2Options::UseAsyncSendRequest) {
                size_t amount = AS_.WriteSome(*bfs->GetIOvec(), ec);

                if (ec && ec.Value() != EAGAIN && ec.Value() != EWOULDBLOCK && ec.Value() != EINPROGRESS) {
                    return;
                }
                ec.Assign(0);

                bfs->GetIOvec()->Proceed(amount);

                if (bfs->GetIOvec()->Complete()) {
                    RequestWritten_ = true;
                    StartRead();
                } else {
                    SendRequestAsync(bfs);
                }
            } else {
                SendRequestAsync(bfs);
            }
        }

        void SendRequestAsync(const THttpRequestBuffersPtr& bfs) {
            NAsio::TTcpSocket::TSendedData sd(bfs.Release());
            AS_.AsyncWrite(sd, std::bind(&THttpConn::OnWrite, THttpConnRef(this), _1, _2, _3), THttp2Options::OutputDeadline);
        }

        void OnWrite(const TErrorCode& err, size_t amount, IHandlingContext& ctx) {
            Y_UNUSED(amount);
            Y_UNUSED(ctx);
            if (err) {
                OnError(err);
            } else {
                DBGOUT("OnWrite()");
                RequestWritten_ = true;
                StartRead();
            }
        }

        inline void StartRead() {
            if (!InAsyncRead_ && !Canceled_) {
                InAsyncRead_ = true;
                AS_.AsyncPollRead(std::bind(&THttpConn::OnCanRead, THttpConnRef(this), _1, _2), THttp2Options::InputDeadline);
            }
        }

        //can be called only from asio
        void OnReadSome(const TErrorCode& err, size_t bytes, IHandlingContext& ctx) {
            if (Y_UNLIKELY(err)) {
                OnError(err);
                return;
            }
            if (!BeginReadResponse_) {
                //used in MessageSendedCompletely()
                BeginReadResponse_ = true;
                THttpRequestRef r(GetRequest());
                if (!!r) {
                    r->OnBeginRead();
                }
            }
            DBGOUT("receive:" << TStringBuf(Buff_.Get(), bytes));
            try {
                if (!Prs_) {
                    throw yexception() << TStringBuf("receive some data while not in request");
                }

#if defined(_linux_)
                if (THttp2Options::QuickAck) {
                    SetSockOpt(AS_.Native(), SOL_TCP, TCP_QUICKACK, (int)1);
                }
#endif

                DBGOUT("parse:");
                while (!Prs_->Parse(Buff_.Get(), bytes)) {
                    if (BuffSize_ == bytes) {
                        TErrorCode ec;
                        bytes = AS_.ReadSome(Buff_.Get(), BuffSize_, ec);

                        if (!ec) {
                            continue;
                        }

                        if (ec.Value() != EAGAIN && ec.Value() != EWOULDBLOCK) {
                            OnError(ec);

                            return;
                        }
                    }
                    //continue async. read from socket
                    ctx.ContinueUseHandler(THttp2Options::InputDeadline);

                    return;
                }

                //succesfully reach end of http response
                THttpRequestRef r(ReleaseRequest());
                if (!r) {
                    //lost race to req. canceling
                    DBGOUT("connection failed");
                    return;
                }

                DBGOUT("response:");
                bool keepALive = Prs_->IsKeepAlive();

                r->OnResponse(Prs_);

                if (!keepALive) {
                    return;
                }

                //continue use connection (keep-alive mode)
                PrepareParser();

                if (!THttp2Options::KeepInputBufferForCachedConnections) {
                    Buff_.Destroy();
                }
                //continue async. read from socket
                ctx.ContinueUseHandler(THttp2Options::InputDeadline);

                PutSelfToCache();
            } catch (...) {
                OnError(CurrentExceptionMessage());
            }
        }

        void PutSelfToCache();

        //method for reaction on input data for re-used keep-alive connection,
        //which free/release buffer when was placed in cache
        void OnCanRead(const TErrorCode& err, IHandlingContext& ctx) {
            if (Y_UNLIKELY(err)) {
                OnError(err);
            } else {
                if (!Buff_) {
                    Buff_.Reset(new char[BuffSize_]);
                }
                TErrorCode ec;
                OnReadSome(ec, AS_.ReadSome(Buff_.Get(), BuffSize_, ec), ctx);
            }
        }

        //unlink connection and request, thread-safe mark connection as non valid
        inline THttpRequestRef GetRequest() noexcept {
            TGuard<TSpinLock> g(SL_);
            return Req_;
        }

        inline THttpRequestRef ReleaseRequest() noexcept {
            THttpRequestRef r;
            {
                TGuard<TSpinLock> g(SL_);
                r.Swap(Req_);
            }
            return r;
        }

        void OnConnectFailed(const TErrorCode& ec);

        inline void OnError(const TErrorCode& ec) {
            OnError(ec.Text());
        }

        inline void OnError(const TString& errText);

        size_t AddrId_;
        NAsio::TTcpSocket AS_;
        TArrayHolder<char> Buff_; //input buffer
        const size_t BuffSize_;

        TAutoPtr<THttpParser> Prs_; //input data parser & parsed info storage

        TSpinLock SL_;
        THttpRequestRef Req_; //current request
        TDuration ConnectDeadline_;
        TAtomicBool Connected_;
        TAtomicBool Cached_;
        TAtomicBool Canceled_;
        TAtomicBool Finalized_;

        bool InAsyncRead_;
        TAtomicBool RequestWritten_;
        TAtomicBool BeginReadResponse_;
    };

    //conn limits monitoring, cache clean, contain used in http clients asio threads/executors
    class THttpConnManager: public IThreadFactory::IThreadAble {
    public:
        THttpConnManager()
            : TotalConn(0)
            , EP_(THttp2Options::AsioThreads)
            , InPurging_(0)
            , MaxConnId_(0)
            , Shutdown_(false)
        {
            T_ = SystemThreadFactory()->Run(this);
            Limits.SetSoft(40000);
            Limits.SetHard(50000);
        }

        ~THttpConnManager() override {
            {
                TGuard<TMutex> g(PurgeMutex_);

                Shutdown_ = true;
                CondPurge_.Signal();
            }

            EP_.SyncShutdown();

            T_->Join();
        }

        inline void SetLimits(size_t softLimit, size_t hardLimit) noexcept {
            Limits.SetSoft(softLimit);
            Limits.SetHard(hardLimit);
        }

        inline std::pair<size_t, size_t> GetLimits() const noexcept {
            return {Limits.Soft(), Limits.Hard()};
        }

        inline void CheckLimits() {
            if (ExceedSoftLimit()) {
                SuggestPurgeCache();

                if (ExceedHardLimit()) {
                    Y_ABORT("neh::http2 output connections limit reached");
                    //ythrow yexception() << "neh::http2 output connections limit reached";
                }
            }
        }

        inline bool Get(THttpConnRef& conn, size_t addrId) {
#ifdef DEBUG_STAT
            TDebugStat::ConnTotal.store(TotalConn.Val(), std::memory_order_release);
            TDebugStat::ConnActive.store(Active(), std::memory_order_release);
            TDebugStat::ConnCached.store(Cache_.Size(), std::memory_order_release);
#endif
            return Cache_.Get(conn, addrId);
        }

        inline void Put(THttpConnRef& conn, size_t addrId) {
            if (Y_LIKELY(!Shutdown_ && !ExceedHardLimit() && !CacheDisabled())) {
                if (Y_UNLIKELY(addrId > (size_t)AtomicGet(MaxConnId_))) {
                    AtomicSet(MaxConnId_, addrId);
                }
                Cache_.Put(conn, addrId);
            } else {
                conn->Close();
                conn.Drop();
            }
        }

        inline size_t OnConnError(size_t addrId) {
            return Cache_.Validate(addrId);
        }

        TIOService& GetIOService() {
            return EP_.GetExecutor().GetIOService();
        }

        bool CacheDisabled() const {
            return Limits.Soft() == 0;
        }

        bool IsShutdown() const noexcept {
            return Shutdown_;
        }

        TAtomicCounter TotalConn;

    private:
        inline size_t Total() const noexcept {
            return TotalConn.Val();
        }

        inline size_t Active() const noexcept {
            return TFdLimits::ExceedLimit(Total(), Cache_.Size());
        }

        inline size_t ExceedSoftLimit() const noexcept {
            return TFdLimits::ExceedLimit(Total(), Limits.Soft());
        }

        inline size_t ExceedHardLimit() const noexcept {
            return TFdLimits::ExceedLimit(Total(), Limits.Hard());
        }

        void SuggestPurgeCache() {
            if (AtomicTryLock(&InPurging_)) {
                //evaluate the usefulness of purging the cache
                //если в кеше мало соединений (< MaxConnId_/16 или 64), не чистим кеш
                if (Cache_.Size() > (Min((size_t)AtomicGet(MaxConnId_), (size_t)1024U) >> 4)) {
                    //по мере приближения к hardlimit нужда в чистке cache приближается к 100%
                    size_t closenessToHardLimit256 = ((Active() + 1) << 8) / (Limits.Delta() + 1);
                    //чем больше соединений в кеше, а не в работе, тем менее нужен кеш (можно его почистить)
                    size_t cacheUselessness256 = ((Cache_.Size() + 1) << 8) / (Active() + 1);

                    //итого, - пороги срабатывания:
                    //при достижении soft-limit, если соединения в кеше, а не в работе
                    //на полпути от soft-limit к hard-limit, если в кеше больше половины соединений
                    //при приближении к hardlimit пытаться почистить кеш почти постоянно
                    if ((closenessToHardLimit256 + cacheUselessness256) >= 256U) {
                        TGuard<TMutex> g(PurgeMutex_);

                        CondPurge_.Signal();
                        return; //memo: thread MUST unlock InPurging_ (see DoExecute())
                    }
                }
                AtomicUnlock(&InPurging_);
            }
        }

        void DoExecute() override {
            TThread::SetCurrentThreadName("NehHttpConnMngr");
            while (true) {
                {
                    TGuard<TMutex> g(PurgeMutex_);

                    if (Shutdown_)
                        return;

                    CondPurge_.WaitI(PurgeMutex_);
                }

                PurgeCache();

                AtomicUnlock(&InPurging_);
            }
        }

        void PurgeCache() noexcept {
            //try remove at least ExceedSoftLimit() oldest connections from cache
            //вычисляем долю кеша, которую нужно почистить (в 256 долях) (но не менее 1/32 кеша)
            size_t frac256 = Min(size_t(Max(size_t(256U / 32U), (ExceedSoftLimit() << 8) / (Cache_.Size() + 1))), (size_t)256U);

            size_t processed = 0;
            size_t maxConnId = AtomicGet(MaxConnId_);
            for (size_t i = 0; i <= maxConnId && !Shutdown_; ++i) {
                processed += Cache_.Purge(i, frac256);
                if (processed > 32) {
#ifdef DEBUG_STAT
                    TDebugStat::ConnPurgedInCache += processed;
#endif
                    processed = 0;
                    Sleep(TDuration::MilliSeconds(10)); //prevent big spike cpu/system usage
                }
            }
        }

        TFdLimits Limits;
        TExecutorsPool EP_;

        TConnCache<THttpConn> Cache_;
        TAtomic InPurging_;
        TAtomic MaxConnId_;

        TAutoPtr<IThreadFactory::IThread> T_;
        TCondVar CondPurge_;
        TMutex PurgeMutex_;
        TAtomicBool Shutdown_;
    };

    THttpConnManager* HttpConnManager() {
        return Singleton<THttpConnManager>();
    }

    TAtomicCounter* HttpOutConnCounter() {
        return &HttpConnManager()->TotalConn;
    }

    THttpConnRef THttpConn::Create(TIOService& srv) {
        if (HttpConnManager()->IsShutdown()) {
            throw yexception() << "can't create connection with shutdowned service";
        }

        return new THttpConn(srv);
    }

    void THttpConn::PutSelfToCache() {
        THttpConnRef c(this);
        HttpConnManager()->Put(c, AddrId_);
    }

    void THttpConn::OnConnectFailed(const TErrorCode& ec) {
        THttpRequestRef r(GetRequest());
        if (!!r) {
            r->OnConnectFailed(this, ec);
        }
        OnError(ec);
    }

    void THttpConn::OnError(const TString& errText) {
        Finalized_ = true;
        if (Connected_) {
            Connected_ = false;
            TErrorCode ec;
            AS_.Shutdown(NAsio::TTcpSocket::ShutdownBoth, ec);
        } else {
            if (AS_.IsOpen()) {
                AS_.AsyncCancel();
            }
        }
        THttpRequestRef r(ReleaseRequest());
        if (!!r) {
            r->OnError(this, errText);
        } else {
            if (Cached_) {
                size_t res = HttpConnManager()->OnConnError(AddrId_);
                Y_UNUSED(res);
#ifdef DEBUG_STAT
                TDebugStat::ConnDestroyedInCache += res;
#endif
            }
        }
    }

    void THttpRequest::Run(THttpRequestRef& req) {
#ifdef DEBUG_STAT
        if ((++TDebugStat::RequestTotal & 0xFFF) == 0) {
            TDebugStat::Print();
        }
#endif
        try {
            while (!Canceled_) {
                THttpConnRef conn;
                if (HttpConnManager()->Get(conn, Addr_->Id)) {
                    DBGOUT("Use connection from cache");
                    Conn_ = conn; //thread magic
                    if (!conn->StartNextRequest(req)) {
                        continue; //if use connection from cache, ignore write error and try another conn
                    }
                } else {
                    HttpConnManager()->CheckLimits(); //here throw exception if reach hard limit (or atexit() state)
                    Conn_ = THttpConn::Create(HttpConnManager()->GetIOService());
                    TEndpoint ep(new NAddr::TAddrInfo(&*AddrIter_));
                    Conn_->StartRequest(req, ep, Addr_->Id, THttp2Options::SymptomSlowConnect); // can throw
                }
                break;
            }
        } catch (...) {
            Conn_.Reset();
            throw;
        }
    }

    //it seems we have lost TCP SYN packet, create extra connection for decrease response time
    void THttpRequest::OnDetectSlowConnecting() {
#ifdef DEBUG_STAT
        ++TDebugStat::ConnSlow;
#endif
        //use some io_service (Run() thread-executor), from first conn. for more thread safety
        THttpConnRef conn = GetConn();

        if (!conn) {
            return;
        }

        THttpConnRef conn2;
        try {
            conn2 = THttpConn::Create(conn->GetIOService());
        } catch (...) {
            return; // cant create spare connection, simple continue use only main
        }

        {
            TGuard<TSpinLock> g(SL_);
            Conn2_ = conn2;
        }

        if (Y_UNLIKELY(Canceled_)) {
            ReleaseConn2();
        } else {
            //use connect timeout for disable detecting slow connecting on second conn.
            TEndpoint ep(new NAddr::TAddrInfo(&*Addr_->Addr.Begin()));
            try {
                conn2->StartRequest(WeakThis_, ep, Addr_->Id, THttp2Options::ConnectTimeout);
            } catch (...) {
                // ignore errors on spare connection
                ReleaseConn2();
            }
        }
    }

    void THttpRequest::OnConnect(THttpConn* c) {
        THttpConnRef extraConn;
        {
            TGuard<TSpinLock> g(SL_);
            if (Y_UNLIKELY(!!Conn2_)) {
                //has pair concurrent conn, 'should stay only one'
                if (Conn2_.Get() == c) {
#ifdef DEBUG_STAT
                    ++TDebugStat::Conn2Success;
#endif
                    Conn2_.Swap(Conn_);
                }
                extraConn.Swap(Conn2_);
            }
        }
        if (!!extraConn) {
            extraConn->DetachRequest(); //prevent call OnError()
            extraConn->Close();
        }
    }

    void THttpRequest::OnResponse(TAutoPtr<THttpParser>& rsp) {
        DBGOUT("THttpRequest::OnResponse()");
        ReleaseConn();
        if (Y_LIKELY(((rsp->RetCode() >= 200 && rsp->RetCode() < (!THttp2Options::RedirectionNotError ? 300 : 400)) || THttp2Options::AnyResponseIsNotError))) {
            NotifyResponse(rsp->DecodedContent(), rsp->FirstLine(), rsp->Headers());
        } else {
            TString message;

            if (THttp2Options::FullHeadersAsErrorMessage) {
                TStringStream err;
                err << rsp->FirstLine();

                THttpHeaders hdrs = rsp->Headers();
                for (auto h = hdrs.begin(); h < hdrs.end(); h++) {
                    err << h->ToString() << TStringBuf("\r\n");
                }

                message = err.Str();
            } else if (THttp2Options::UseResponseAsErrorMessage) {
                message = rsp->DecodedContent();
            } else {
                TStringStream err;
                err << TStringBuf("request failed(") << rsp->FirstLine() << TStringBuf(")");
                message = err.Str();
            }

            NotifyError(new TError(message, TError::ProtocolSpecific, rsp->RetCode()), rsp.Get());
        }
    }

    void THttpRequest::OnConnectFailed(THttpConn* c, const TErrorCode& ec) {
        DBGOUT("THttpRequest::OnConnectFailed()");
        //detach/discard failed conn, try connect to next ip addr (if can)
        THttpConnRef cc(GetConn());
        if (c != cc.Get() || AddrIter_ == Addr_->Addr.End() || ++AddrIter_ == Addr_->Addr.End() || Canceled_) {
            return OnSystemError(c, ec);
        }
        // can try next host addr
        c->DetachRequest();
        c->Close();
        THttpConnRef nextConn;
        try {
            nextConn = THttpConn::Create(HttpConnManager()->GetIOService());
        } catch (...) {
            OnSystemError(nullptr, ec);
            return;
        }
        {
            THttpConnRef nc = nextConn;
            TGuard<TSpinLock> g(SL_);
            Conn_.Swap(nc);
        }
        TEndpoint ep(new NAddr::TAddrInfo(&*AddrIter_));
        try {
            nextConn->StartRequest(WeakThis_, ep, Addr_->Id, THttp2Options::SymptomSlowConnect);
        } catch (...) {
            OnError(nullptr, CurrentExceptionMessage());
            return;
        }

        if (Canceled_) {
            OnError(nullptr, "canceled");
        }
    }

    void THttpRequest::OnSystemError(THttpConn* c, const TErrorCode& ec) {
        DBGOUT("THttpRequest::OnSystemError()");
        NotifyError(ec.Text(), TError::TType::UnknownType, 0, ec.Value());
        Finalize(c);
    }

    void THttpRequest::OnError(THttpConn* c, const TString& errorText) {
        DBGOUT("THttpRequest::OnError()");
        NotifyError(errorText);
        Finalize(c);
    }

    bool THttpRequest::RequestSendedCompletely() noexcept {
        if (RequestSendedCompletely_) {
            return true;
        }

        THttpConnRef c(GetConn());
        return !!c ? c->RequestSendedCompletely() : false;
    }

    void THttpRequest::Cancel() noexcept {
        if (!Canceled_) {
            Canceled_ = true;
            try {
                static const TString canceled("Canceled");
                NotifyError(canceled, TError::Cancelled);
                Finalize();
            } catch (...) {
            }
        }
    }

    inline void FinalizeConn(THttpConnRef& c, THttpConn* skipConn) noexcept {
        if (!!c && c.Get() != skipConn) {
            c->DetachRequest();
            c->Close();
        }
    }

    void THttpRequest::Finalize(THttpConn* skipConn) noexcept {
        THttpConnRef c1(ReleaseConn());
        FinalizeConn(c1, skipConn);
        THttpConnRef c2(ReleaseConn2());
        FinalizeConn(c2, skipConn);
    }

    /////////////////////////////////// server side ////////////////////////////////////

    TAtomicCounter* HttpInConnCounter() {
        return Singleton<TAtomicCounter>();
    }

    TFdLimits* HttpInConnLimits() {
        return Singleton<TFdLimits>();
    }

    class THttpServer: public IRequester {
        typedef TAutoPtr<TTcpAcceptor> TTcpAcceptorPtr;
        typedef TAtomicSharedPtr<TTcpSocket> TTcpSocketRef;
        class TConn;
        typedef TSharedPtrB<TConn> TConnRef;

        class TRequest: public IHttpRequest {
        public:
            TRequest(TWeakPtrB<TConn>& c, TAutoPtr<THttpParser>& p)
                : C_(c)
                , P_(p)
                , RemoteHost_(C_->RemoteHost())
                , CompressionScheme_(P_->GetBestCompressionScheme())
                , H_(TStringBuf(P_->FirstLine()))
            {
            }

            ~TRequest() override {
                if (!!C_) {
                    try {
                        C_->SendError(Id(), 503, "service unavailable (request ignored)", P_->HttpVersion(), {});
                    } catch (...) {
                        DBGOUT("~TRequest()::SendFail() exception");
                    }
                }
            }

            TAtomicBase Id() const {
                return Id_;
            }

        protected:
            TStringBuf Scheme() const override {
                return TStringBuf("http");
            }

            TString RemoteHost() const override {
                return RemoteHost_;
            }

            TStringBuf Service() const override {
                return TStringBuf(H_.Path).Skip(1);
            }

            const THttpHeaders& Headers() const override {
                return P_->Headers();
            }

            TStringBuf Method() const override {
                return H_.Method;
            }

            TStringBuf Body() const override {
                return P_->DecodedContent();
            }

            TStringBuf Cgi() const override {
                return H_.Cgi;
            }

            TStringBuf RequestId() const override {
                return TStringBuf();
            }

            bool Canceled() const override {
                if (!C_) {
                    return false;
                }
                return C_->IsCanceled();
            }

            void SendReply(TData& data) override {
                SendReply(data, TString(), HttpCodes::HTTP_OK);
            }

            void SendReply(TData& data, const TString& headers, int httpCode) override {
                if (!!C_) {
                    C_->Send(Id(), data, CompressionScheme_, P_->HttpVersion(), headers, httpCode);
                    C_.Reset();
                }
            }

            void SendError(TResponseError err, const THttpErrorDetails& details) override {
                static const unsigned errorToHttpCode[IRequest::MaxResponseError] =
                    {
                        400,
                        403,
                        404,
                        429,
                        500,
                        501,
                        502,
                        503,
                        509};

                if (!!C_) {
                    C_->SendError(Id(), errorToHttpCode[err], details.Details, P_->HttpVersion(), details.Headers);
                    C_.Reset();
                }
            }

            static TAtomicBase NextId() {
                static TAtomic idGenerator = 0;
                TAtomicBase id = 0;
                do {
                    id = AtomicIncrement(idGenerator);
                } while (!id);
                return id;
            }

            TSharedPtrB<TConn> C_;
            TAutoPtr<THttpParser> P_;
            TString RemoteHost_;
            TString CompressionScheme_;
            TParsedHttpFull H_;
            TAtomicBase Id_ = NextId();
        };

        class TRequestGet: public TRequest {
        public:
            TRequestGet(TWeakPtrB<TConn>& c, TAutoPtr<THttpParser> p)
                : TRequest(c, p)
            {
            }

            TStringBuf Data() const override {
                return H_.Cgi;
            }
        };

        class TRequestPost: public TRequest {
        public:
            TRequestPost(TWeakPtrB<TConn>& c, TAutoPtr<THttpParser> p)
                : TRequest(c, p)
            {
            }

            TStringBuf Data() const override {
                return P_->DecodedContent();
            }
        };

        class TConn {
        private:
            TConn(THttpServer& hs, const TTcpSocketRef& s)
                : HS_(hs)
                , AS_(s)
                , RemoteHost_(NNeh::PrintHostByRfc(*AS_->RemoteEndpoint().Addr()))
                , BuffSize_(THttp2Options::InputBufferSize)
                , Buff_(new char[BuffSize_])
                , Canceled_(false)
                , LeftRequestsToDisconnect_(hs.LimitRequestsPerConnection)
            {
                DBGOUT("THttpServer::TConn()");
                HS_.OnCreateConn();
            }

            inline TConnRef SelfRef() noexcept {
                return WeakThis_;
            }

        public:
            static void Create(THttpServer& hs, const TTcpSocketRef& s) {
                TSharedPtrB<TConn> conn(new TConn(hs, s));
                conn->WeakThis_ = conn;
                conn->ExpectNewRequest();
                conn->AS_->AsyncPollRead(std::bind(&TConn::OnCanRead, conn, _1, _2), THttp2Options::ServerInputDeadline);
            }

            ~TConn() {
                DBGOUT("~THttpServer::TConn(" << (!AS_ ? -666 : AS_->Native()) << ")");
                HS_.OnDestroyConn();
            }

        private:
            void ExpectNewRequest() {
                P_.Reset(new THttpParser(THttpParser::Request));
                P_->Prepare();
            }

            void OnCanRead(const TErrorCode& ec, IHandlingContext& ctx) {
                if (ec) {
                    OnError();
                } else {
                    TErrorCode ec2;
                    OnReadSome(ec2, AS_->ReadSome(Buff_.Get(), BuffSize_, ec2), ctx);
                }
            }

            void OnError() {
                DBGOUT("Srv OnError(" << (!AS_ ? -666 : AS_->Native()) << ")");
                Canceled_ = true;
                AS_->AsyncCancel();
            }

            void OnReadSome(const TErrorCode& ec, size_t amount, IHandlingContext& ctx) {
                if (ec || !amount) {
                    OnError();

                    return;
                }

                DBGOUT("ReadSome(" << (!AS_ ? -666 : AS_->Native()) << "): " << amount);
                try {
                    size_t buffPos = 0;
                    //DBGOUT("receive and parse: " << TStringBuf(Buff_.Get(), amount));
                    while (P_->Parse(Buff_.Get() + buffPos, amount - buffPos)) {
                        if (!P_->IsKeepAlive() || LeftRequestsToDisconnect_ == 1) {
                            SeenMessageWithoutKeepalive_ = true;
                        }

                        char rt = *P_->FirstLine().data();
                        const size_t extraDataSize = P_->GetExtraDataSize();
                        if (rt == 'P' || rt == 'p') {
                            OnRequest(new TRequestPost(WeakThis_, P_));
                        } else {
                            OnRequest(new TRequestGet(WeakThis_, P_));
                        }
                        if (extraDataSize) {
                            // has http pipelining
                            buffPos = amount - extraDataSize;
                            ExpectNewRequest();
                        } else {
                            ExpectNewRequest();
                            ctx.ContinueUseHandler(HS_.GetKeepAliveTimeout());
                            return;
                        }
                    }
                    ctx.ContinueUseHandler(THttp2Options::ServerInputDeadline);
                } catch (...) {
                    OnError();
                }
            }

            void OnRequest(TRequest* r) {
                DBGOUT("OnRequest()");
                if (AtomicGet(PrimaryResponse_)) {
                    // has pipelining
                    PipelineOrder_.Enqueue(r->Id());
                } else {
                    AtomicSet(PrimaryResponse_, r->Id());
                }
                HS_.OnRequest(r);
                OnRequestDone();
            }

            void OnRequestDone() {
                DBGOUT("OnRequestDone()");
                if (LeftRequestsToDisconnect_ > 0) {
                    --LeftRequestsToDisconnect_;
                }
            }

            static void PrintHttpVersion(IOutputStream& out, const THttpVersion& ver) {
                out << TStringBuf("HTTP/") << ver.Major << TStringBuf(".") << ver.Minor;
            }

            struct TResponseData : TThrRefBase {
                TResponseData(size_t reqId, TTcpSocket::TSendedData data)
                    : RequestId_(reqId)
                    , Data_(data)
                {
                }

                size_t RequestId_;
                TTcpSocket::TSendedData Data_;
            };
            typedef TIntrusivePtr<TResponseData> TResponseDataRef;

        public:
            //called non thread-safe (from outside thread)
            void Send(TAtomicBase requestId, TData& data, const TString& compressionScheme, const THttpVersion& ver, const TString& headers, int httpCode) {
                class THttpResponseFormatter {
                public:
                    THttpResponseFormatter(TData& theData, const TString& contentEncoding, const THttpVersion& theVer, const TString& theHeaders, int theHttpCode, bool closeConnection) {
                        Header.Reserve(128 + contentEncoding.size() + theHeaders.size());
                        PrintHttpVersion(Header, theVer);
                        Header << TStringBuf(" ") << theHttpCode << ' ' << HttpCodeStr(theHttpCode);
                        if (Compress(theData, contentEncoding)) {
                            Header << TStringBuf("\r\nContent-Encoding: ") << contentEncoding;
                        }
                        Header << TStringBuf("\r\nContent-Length: ") << theData.size();
                        if (closeConnection) {
                            Header << TStringBuf("\r\nConnection: close");
                        } else if (Y_LIKELY(theVer.Major > 1 || theVer.Minor > 0)) {
                            // since HTTP/1.1 Keep-Alive is default behaviour
                            Header << TStringBuf("\r\nConnection: Keep-Alive");
                        }
                        if (theHeaders) {
                            Header << theHeaders;
                        }
                        Header << TStringBuf("\r\n\r\n");

                        Body.swap(theData);

                        Parts[0].buf = Header.Data();
                        Parts[0].len = Header.Size();
                        Parts[1].buf = Body.data();
                        Parts[1].len = Body.size();
                    }

                    TStringStream Header;
                    TData Body;
                    IOutputStream::TPart Parts[2];
                };

                class TBuffers: public THttpResponseFormatter, public TTcpSocket::IBuffers {
                public:
                    TBuffers(TData& theData, const TString& contentEncoding, const THttpVersion& theVer, const TString& theHeaders, int theHttpCode, bool closeConnection)
                        : THttpResponseFormatter(theData, contentEncoding, theVer, theHeaders, theHttpCode, closeConnection)
                        , IOVec(Parts, 2)
                    {
                    }

                    TContIOVector* GetIOvec() override {
                        return &IOVec;
                    }

                    TContIOVector IOVec;
                };

                TTcpSocket::TSendedData sd(new TBuffers(data, compressionScheme, ver, headers, httpCode, SeenMessageWithoutKeepalive_));
                SendData(requestId, sd);
            }

            //called non thread-safe (from outside thread)
            void SendError(TAtomicBase requestId, unsigned httpCode, const TString& descr, const THttpVersion& ver, const TString& headers) {
                if (Canceled_) {
                    return;
                }

                class THttpErrorResponseFormatter {
                public:
                    THttpErrorResponseFormatter(unsigned theHttpCode, const TString& theDescr, const THttpVersion& theVer, bool closeConnection, const TString& headers) {
                        PrintHttpVersion(Answer, theVer);
                        Answer << TStringBuf(" ") << theHttpCode << TStringBuf(" ");
                        if (theDescr.size() && !THttp2Options::ErrorDetailsAsResponseBody) {
                            // Reason-Phrase  = *<TEXT, excluding CR, LF>
                            // replace bad chars to '.'
                            TString reasonPhrase = theDescr;
                            for (TString::iterator it = reasonPhrase.begin(); it != reasonPhrase.end(); ++it) {
                                char& ch = *it;
                                if (ch == ' ') {
                                    continue;
                                }
                                if (((ch & 31) == ch) || static_cast<unsigned>(ch) == 127 || (static_cast<unsigned>(ch) & 0x80)) {
                                    //CTLs || DEL(127) || non ascii
                                    // (ch <= 32) || (ch >= 127)
                                    ch = '.';
                                }
                            }
                            Answer << reasonPhrase;
                        } else {
                            Answer << HttpCodeStr(static_cast<int>(theHttpCode));
                        }

                        if (closeConnection) {
                            Answer << TStringBuf("\r\nConnection: close");
                        }

                        if (headers) {
                            Answer << "\r\n" << headers;
                        }

                        if (THttp2Options::ErrorDetailsAsResponseBody) {
                            Answer << TStringBuf("\r\nContent-Length:") << theDescr.size() << "\r\n\r\n" << theDescr;
                        } else {
                            Answer << "\r\n"
                                      "Content-Length:0\r\n\r\n"sv;
                        }

                        Parts[0].buf = Answer.Data();
                        Parts[0].len = Answer.Size();
                    }

                    TStringStream Answer;
                    IOutputStream::TPart Parts[1];
                };

                class TBuffers: public THttpErrorResponseFormatter, public TTcpSocket::IBuffers {
                public:
                    TBuffers(
                        unsigned theHttpCode,
                        const TString& theDescr,
                        const THttpVersion& theVer,
                        bool closeConnection,
                        const TString& headers
                    )
                        : THttpErrorResponseFormatter(theHttpCode, theDescr, theVer, closeConnection, headers)
                        , IOVec(Parts, 1)
                    {
                    }

                    TContIOVector* GetIOvec() override {
                        return &IOVec;
                    }

                    TContIOVector IOVec;
                };

                TTcpSocket::TSendedData sd(new TBuffers(httpCode, descr, ver, SeenMessageWithoutKeepalive_, headers));
                SendData(requestId, sd);
            }

            void ProcessPipeline() {
                // on successfull response to current (PrimaryResponse_) request
                TAtomicBase requestId;
                if (PipelineOrder_.Dequeue(&requestId)) {
                    TAtomicBase oldReqId;
                    do {
                        oldReqId = AtomicGet(PrimaryResponse_);
                        Y_ABORT_UNLESS(oldReqId, "race inside http pipelining");
                    } while (!AtomicCas(&PrimaryResponse_, requestId, oldReqId));

                    ProcessResponsesData();
                } else {
                    TAtomicBase oldReqId = AtomicGet(PrimaryResponse_);
                    if (oldReqId) {
                        while (!AtomicCas(&PrimaryResponse_, 0, oldReqId)) {
                            Y_ABORT_UNLESS(oldReqId == AtomicGet(PrimaryResponse_), "race inside http pipelining [2]");
                        }
                    }
                }
            }

            void ProcessResponsesData() {
                // process responses data queue, send response (if already have next PrimaryResponse_)
                TResponseDataRef rd;
                while (ResponsesDataQueue_.Dequeue(&rd)) {
                    ResponsesData_[rd->RequestId_] = rd;
                }
                TAtomicBase requestId = AtomicGet(PrimaryResponse_);
                if (requestId) {
                    THashMap<TAtomicBase, TResponseDataRef>::iterator it = ResponsesData_.find(requestId);
                    if (it != ResponsesData_.end()) {
                        // has next primary response
                        rd = it->second;
                        ResponsesData_.erase(it);
                        AS_->AsyncWrite(rd->Data_, std::bind(&TConn::OnSend, SelfRef(), _1, _2, _3), THttp2Options::ServerOutputDeadline);
                    }
                }
            }

        private:
            void SendData(TAtomicBase requestId, TTcpSocket::TSendedData sd) {
                TContIOVector& vec = *sd->GetIOvec();

                if (requestId != AtomicGet(PrimaryResponse_)) {
                    // already has another request for response first, so push this to queue
                    // + enqueue event for safe checking queue (at local/transport thread)
                    TResponseDataRef rdr = new TResponseData(requestId, sd);
                    ResponsesDataQueue_.Enqueue(rdr);
                    AS_->GetIOService().Post(std::bind(&TConn::ProcessResponsesData, SelfRef()));
                    return;
                }
                if (THttp2Options::ServerUseDirectWrite) {
                    vec.Proceed(AS_->WriteSome(vec));
                }
                if (!vec.Complete()) {
                    DBGOUT("AsyncWrite()");
                    AS_->AsyncWrite(sd, std::bind(&TConn::OnSend, SelfRef(), _1, _2, _3), THttp2Options::ServerOutputDeadline);
                } else {
                    // run ProcessPipeline at safe thread
                    AS_->GetIOService().Post(std::bind(&TConn::ProcessPipeline, SelfRef()));
                }
            }

            void OnSend(const TErrorCode& ec, size_t amount, IHandlingContext&) {
                Y_UNUSED(amount);
                if (ec) {
                    OnError();
                } else {
                    ProcessPipeline();
                }

                if (SeenMessageWithoutKeepalive_) {
                    TErrorCode shutdown_ec;
                    AS_->Shutdown(TTcpSocket::ShutdownBoth, shutdown_ec);
                }
            }

        public:
            bool IsCanceled() const noexcept {
                return Canceled_;
            }

            const TString& RemoteHost() const noexcept {
                return RemoteHost_;
            }

        private:
            TWeakPtrB<TConn> WeakThis_;
            THttpServer& HS_;
            TTcpSocketRef AS_;
            TString RemoteHost_;
            size_t BuffSize_;
            TArrayHolder<char> Buff_;
            TAutoPtr<THttpParser> P_;
            // pipeline supporting
            TAtomic PrimaryResponse_ = 0;
            TLockFreeQueue<TAtomicBase> PipelineOrder_;
            TLockFreeQueue<TResponseDataRef> ResponsesDataQueue_;
            THashMap<TAtomicBase, TResponseDataRef> ResponsesData_;

            TAtomicBool Canceled_;
            TAtomicBool SeenMessageWithoutKeepalive_ = false;

            i32 LeftRequestsToDisconnect_ = -1;
        };

        ///////////////////////////////////////////////////////////

    public:
        THttpServer(IOnRequest* cb, const TParsedLocation& loc)
            : E_(THttp2Options::AsioServerThreads)
            , CB_(cb)
            , LimitRequestsPerConnection(THttp2Options::LimitRequestsPerConnection)
        {
            TNetworkAddress addr = THttp2Options::RespectHostInHttpServerNetworkAddress ?
                                    TNetworkAddress(TString(loc.Host), loc.GetPort())
                                    : TNetworkAddress(loc.GetPort());

            for (TNetworkAddress::TIterator it = addr.Begin(); it != addr.End(); ++it) {
                TEndpoint ep(new NAddr::TAddrInfo(&*it));
                TTcpAcceptorPtr a(new TTcpAcceptor(AcceptExecutor_.GetIOService()));
                DBGOUT("bind:" << ep.IpToString() << ":" << ep.Port());
                a->Bind(ep);
                a->Listen(THttp2Options::Backlog);
                StartAccept(a.Get());
                A_.push_back(a);
            }
        }

        ~THttpServer() override {
            AcceptExecutor_.SyncShutdown(); //cancel operation for all current sockets (include acceptors)
            A_.clear();                     //stop listening
            E_.SyncShutdown();
        }

        void OnAccept(TTcpAcceptor* a, TAtomicSharedPtr<TTcpSocket> s, const TErrorCode& ec, IHandlingContext&) {
            if (Y_UNLIKELY(ec)) {
                if (ec.Value() == ECANCELED) {
                    return;
                } else if (ec.Value() == EMFILE || ec.Value() == ENFILE || ec.Value() == ENOMEM || ec.Value() == ENOBUFS) {
                    //reach some os limit, suspend accepting
                    TAtomicSharedPtr<TDeadlineTimer> dt(new TDeadlineTimer(a->GetIOService()));
                    dt->AsyncWaitExpireAt(TDuration::Seconds(30), std::bind(&THttpServer::OnTimeoutSuspendAccept, this, a, dt, _1, _2));
                    return;
                } else {
                    Cdbg << "acc: " << ec.Text() << Endl;
                }
            } else {
                if (static_cast<size_t>(HttpInConnCounter()->Val()) < HttpInConnLimits()->Hard()) {
                    try {
                        SetNonBlock(s->Native());
                        PrepareSocket(s->Native());
                        TConn::Create(*this, s);
                    } catch (TSystemError& err) {
                        TErrorCode ec2(err.Status());
                        Cdbg << "acc: " << ec2.Text() << Endl;
                    }
                } //else accepted socket will be closed
            }
            StartAccept(a); //continue accepting
        }

        void OnTimeoutSuspendAccept(TTcpAcceptor* a, TAtomicSharedPtr<TDeadlineTimer>, const TErrorCode& ec, IHandlingContext&) {
            if (!ec) {
                DBGOUT("resume acceptor")
                StartAccept(a);
            }
        }

        void OnRequest(IRequest* r) {
            try {
                CB_->OnRequest(r);
            } catch (...) {
                Cdbg << CurrentExceptionMessage() << Endl;
            }
        }

    protected:
        void OnCreateConn() noexcept {
            HttpInConnCounter()->Inc();
        }

        void OnDestroyConn() noexcept {
            HttpInConnCounter()->Dec();
        }

        TDuration GetKeepAliveTimeout() const noexcept {
            size_t cc = HttpInConnCounter()->Val();
            TFdLimits lim(*HttpInConnLimits());

            if (!TFdLimits::ExceedLimit(cc, lim.Soft())) {
                return THttp2Options::ServerInputDeadlineKeepAliveMax;
            }

            if (cc > lim.Hard()) {
                cc = lim.Hard();
            }
            TDuration::TValue softTuneRange = THttp2Options::ServerInputDeadlineKeepAliveMax.Seconds() - THttp2Options::ServerInputDeadlineKeepAliveMin.Seconds();

            return TDuration::Seconds((softTuneRange * (cc - lim.Soft())) / (lim.Hard() - lim.Soft() + 1)) + THttp2Options::ServerInputDeadlineKeepAliveMin;
        }

    private:
        void StartAccept(TTcpAcceptor* a) {
            TAtomicSharedPtr<TTcpSocket> s(new TTcpSocket(E_.Size() ? E_.GetExecutor().GetIOService() : AcceptExecutor_.GetIOService()));
            a->AsyncAccept(*s, std::bind(&THttpServer::OnAccept, this, a, s, _1, _2));
        }

        TIOServiceExecutor AcceptExecutor_;
        TVector<TTcpAcceptorPtr> A_;
        TExecutorsPool E_;
        IOnRequest* CB_;

    public:
        const i32 LimitRequestsPerConnection;
    };

    template <class T>
    class THttp2Protocol: public IProtocol {
    public:
        IRequesterRef CreateRequester(IOnRequest* cb, const TParsedLocation& loc) override {
            return new THttpServer(cb, loc);
        }

        THandleRef ScheduleRequest(const TMessage& msg, IOnRecv* fallback, TServiceStatRef& ss) override {
            THttpRequest::THandleRef ret(new THttpRequest::THandle(fallback, msg, !ss ? nullptr : new TStatCollector(ss)));
            try {
                THttpRequest::Run(ret, msg, &T::Build, T::RequestSettings());
            } catch (...) {
                ret->ResetOnRecv();
                throw;
            }
            return ret.Get();
        }

        TStringBuf Scheme() const noexcept override {
            return T::Name();
        }

        bool SetOption(TStringBuf name, TStringBuf value) override {
            return THttp2Options::Set(name, value);
        }
    };
}

namespace NNeh {
    IProtocol* Http1Protocol() {
        return Singleton<THttp2Protocol<TRequestGet1>>();
    }
    IProtocol* Post1Protocol() {
        return Singleton<THttp2Protocol<TRequestPost1>>();
    }
    IProtocol* Full1Protocol() {
        return Singleton<THttp2Protocol<TRequestFull1>>();
    }
    IProtocol* Http2Protocol() {
        return Singleton<THttp2Protocol<TRequestGet2>>();
    }
    IProtocol* Post2Protocol() {
        return Singleton<THttp2Protocol<TRequestPost2>>();
    }
    IProtocol* Full2Protocol() {
        return Singleton<THttp2Protocol<TRequestFull2>>();
    }
    IProtocol* UnixSocketGetProtocol() {
        return Singleton<THttp2Protocol<TRequestUnixSocketGet>>();
    }
    IProtocol* UnixSocketPostProtocol() {
        return Singleton<THttp2Protocol<TRequestUnixSocketPost>>();
    }
    IProtocol* UnixSocketFullProtocol() {
        return Singleton<THttp2Protocol<TRequestUnixSocketFull>>();
    }

    void SetHttp2OutputConnectionsLimits(size_t softLimit, size_t hardLimit) {
        HttpConnManager()->SetLimits(softLimit, hardLimit);
    }

    void SetHttp2InputConnectionsLimits(size_t softLimit, size_t hardLimit) {
        HttpInConnLimits()->SetSoft(softLimit);
        HttpInConnLimits()->SetHard(hardLimit);
    }

    TAtomicBase GetHttpOutputConnectionCount() {
        return HttpOutConnCounter()->Val();
    }

    std::pair<size_t, size_t> GetHttpOutputConnectionLimits() {
        return HttpConnManager()->GetLimits();
    }

    TAtomicBase GetHttpInputConnectionCount() {
        return HttpInConnCounter()->Val();
    }

    void SetHttp2InputConnectionsTimeouts(unsigned minSeconds, unsigned maxSeconds) {
        THttp2Options::ServerInputDeadlineKeepAliveMin = TDuration::Seconds(minSeconds);
        THttp2Options::ServerInputDeadlineKeepAliveMax = TDuration::Seconds(maxSeconds);
    }

    class TUnixSocketResolver {
    public:
        NDns::TResolvedHost* Resolve(const TString& path) {
            TString unixSocketPath = path;
            if (path.size() > 2 && path[0] == '[' && path[path.size() - 1] == ']') {
                unixSocketPath = path.substr(1, path.size() - 2);
            }

            if (auto resolvedUnixSocket = ResolvedUnixSockets_.FindPtr(unixSocketPath)) {
                return resolvedUnixSocket->Get();
            }

            TNetworkAddress na{TUnixSocketPath(unixSocketPath)};
            ResolvedUnixSockets_[unixSocketPath] = MakeHolder<NDns::TResolvedHost>(unixSocketPath, na);

            return ResolvedUnixSockets_[unixSocketPath].Get();
        }

    private:
        THashMap<TString, THolder<NDns::TResolvedHost>> ResolvedUnixSockets_;
    };

    TUnixSocketResolver* UnixSocketResolver() {
        return FastTlsSingleton<TUnixSocketResolver>();
    }

    const NDns::TResolvedHost* Resolve(const TStringBuf host, ui16 port, NHttp::EResolverType resolverType) {
        if (resolverType == EResolverType::EUNIXSOCKET) {
            return UnixSocketResolver()->Resolve(TString(host));
        }
        return NDns::CachedResolve(NDns::TResolveInfo(host, port));

    }
}