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
use std::collections::{HashMap, VecDeque};
use std::net::Shutdown;
use std::time::{Duration, Instant};
use std::{fmt, io, mem, time};

use actix_inner::actors::resolver::{Connect as ResolveConnect, Resolver, ResolverError};
use actix_inner::{
    fut, Actor, ActorFuture, ActorResponse, AsyncContext, Context,
    ContextFutureSpawner, Handler, Message, Recipient, StreamHandler, Supervised,
    SystemService, WrapFuture,
};

use futures::sync::{mpsc, oneshot};
use futures::{Async, Future, Poll};
use http::{Error as HttpError, HttpTryFrom, Uri};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_timer::Delay;

#[cfg(any(feature = "alpn", feature = "ssl"))]
use {
    openssl::ssl::{Error as SslError, SslConnector, SslMethod},
    tokio_openssl::SslConnectorExt,
};

#[cfg(all(
    feature = "tls",
    not(any(feature = "alpn", feature = "ssl", feature = "rust-tls"))
))]
use {
    native_tls::{Error as SslError, TlsConnector as NativeTlsConnector},
    tokio_tls::TlsConnector as SslConnector,
};

#[cfg(all(
    feature = "rust-tls",
    not(any(feature = "alpn", feature = "tls", feature = "ssl"))
))]
use {
    rustls::ClientConfig, std::io::Error as SslError, std::sync::Arc,
    tokio_rustls::TlsConnector as SslConnector, webpki::DNSNameRef, webpki_roots,
};

#[cfg(not(any(
    feature = "alpn",
    feature = "ssl",
    feature = "tls",
    feature = "rust-tls"
)))]
type SslConnector = ();

use server::IoStream;
use {HAS_OPENSSL, HAS_RUSTLS, HAS_TLS};

/// Client connector usage stats
#[derive(Default, Message)]
pub struct ClientConnectorStats {
    /// Number of waited-on connections
    pub waits: usize,
    /// Size of the wait queue
    pub wait_queue: usize,
    /// Number of reused connections
    pub reused: usize,
    /// Number of opened connections
    pub opened: usize,
    /// Number of closed connections
    pub closed: usize,
    /// Number of connections with errors
    pub errors: usize,
    /// Number of connection timeouts
    pub timeouts: usize,
}

#[derive(Debug)]
/// `Connect` type represents a message that can be sent to
/// `ClientConnector` with a connection request.
pub struct Connect {
    pub(crate) uri: Uri,
    pub(crate) wait_timeout: Duration,
    pub(crate) conn_timeout: Duration,
}

impl Connect {
    /// Create `Connect` message for specified `Uri`
    pub fn new<U>(uri: U) -> Result<Connect, HttpError>
    where
        Uri: HttpTryFrom<U>,
    {
        Ok(Connect {
            uri: Uri::try_from(uri).map_err(|e| e.into())?,
            wait_timeout: Duration::from_secs(5),
            conn_timeout: Duration::from_secs(1),
        })
    }

    /// Connection timeout, i.e. max time to connect to remote host.
    /// Set to 1 second by default.
    pub fn conn_timeout(mut self, timeout: Duration) -> Self {
        self.conn_timeout = timeout;
        self
    }

    /// If connection pool limits are enabled, wait time indicates
    /// max time to wait for a connection to become available.
    /// Set to 5 seconds by default.
    pub fn wait_timeout(mut self, timeout: Duration) -> Self {
        self.wait_timeout = timeout;
        self
    }
}

impl Message for Connect {
    type Result = Result<Connection, ClientConnectorError>;
}

/// Pause connection process for `ClientConnector`
///
/// All connect requests enter wait state during connector pause.
pub struct Pause {
    time: Option<Duration>,
}

impl Pause {
    /// Create message with pause duration parameter
    pub fn new(time: Duration) -> Pause {
        Pause { time: Some(time) }
    }
}

impl Default for Pause {
    fn default() -> Pause {
        Pause { time: None }
    }
}

impl Message for Pause {
    type Result = ();
}

/// Resume connection process for `ClientConnector`
#[derive(Message)]
pub struct Resume;

/// A set of errors that can occur while connecting to an HTTP host
#[derive(Fail, Debug)]
pub enum ClientConnectorError {
    /// Invalid URL
    #[fail(display = "Invalid URL")]
    InvalidUrl,

    /// SSL feature is not enabled
    #[fail(display = "SSL is not supported")]
    SslIsNotSupported,

    /// SSL error
    #[cfg(any(
        feature = "tls",
        feature = "alpn",
        feature = "ssl",
        feature = "rust-tls",
    ))]
    #[fail(display = "{}", _0)]
    SslError(#[cause] SslError),

    /// Resolver error
    #[fail(display = "{}", _0)]
    Resolver(#[cause] ResolverError),

    /// Connection took too long
    #[fail(display = "Timeout while establishing connection")]
    Timeout,

    /// Connector has been disconnected
    #[fail(display = "Internal error: connector has been disconnected")]
    Disconnected,

    /// Connection IO error
    #[fail(display = "{}", _0)]
    IoError(#[cause] io::Error),
}

impl From<ResolverError> for ClientConnectorError {
    fn from(err: ResolverError) -> ClientConnectorError {
        match err {
            ResolverError::Timeout => ClientConnectorError::Timeout,
            _ => ClientConnectorError::Resolver(err),
        }
    }
}

struct Waiter {
    tx: oneshot::Sender<Result<Connection, ClientConnectorError>>,
    wait: Instant,
    conn_timeout: Duration,
}

enum Paused {
    No,
    Yes,
    Timeout(Instant, Delay),
}

impl Paused {
    fn is_paused(&self) -> bool {
        match *self {
            Paused::No => false,
            _ => true,
        }
    }
}

/// `ClientConnector` type is responsible for transport layer of a
/// client connection.
pub struct ClientConnector {
    #[allow(dead_code)]
    connector: SslConnector,

    stats: ClientConnectorStats,
    subscriber: Option<Recipient<ClientConnectorStats>>,

    acq_tx: mpsc::UnboundedSender<AcquiredConnOperation>,
    acq_rx: Option<mpsc::UnboundedReceiver<AcquiredConnOperation>>,

    resolver: Option<Recipient<ResolveConnect>>,
    conn_lifetime: Duration,
    conn_keep_alive: Duration,
    limit: usize,
    limit_per_host: usize,
    acquired: usize,
    acquired_per_host: HashMap<Key, usize>,
    available: HashMap<Key, VecDeque<Conn>>,
    to_close: Vec<Connection>,
    waiters: Option<HashMap<Key, VecDeque<Waiter>>>,
    wait_timeout: Option<(Instant, Delay)>,
    paused: Paused,
}

impl Actor for ClientConnector {
    type Context = Context<ClientConnector>;

    fn started(&mut self, ctx: &mut Self::Context) {
        if self.resolver.is_none() {
            self.resolver = Some(Resolver::from_registry().recipient())
        }
        self.collect_periodic(ctx);
        ctx.add_stream(self.acq_rx.take().unwrap());
        ctx.spawn(Maintenance);
    }
}

impl Supervised for ClientConnector {}

impl SystemService for ClientConnector {}

impl Default for ClientConnector {
    fn default() -> ClientConnector {
        let connector = {
            #[cfg(all(any(feature = "alpn", feature = "ssl")))]
            {
                SslConnector::builder(SslMethod::tls()).unwrap().build()
            }

            #[cfg(all(
                feature = "tls",
                not(any(feature = "alpn", feature = "ssl", feature = "rust-tls"))
            ))]
            {
                NativeTlsConnector::builder().build().unwrap().into()
            }

            #[cfg(all(
                feature = "rust-tls",
                not(any(feature = "alpn", feature = "tls", feature = "ssl"))
            ))]
            {
                let mut config = ClientConfig::new();
                config
                    .root_store
                    .add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
                SslConnector::from(Arc::new(config))
            }

            #[cfg_attr(rustfmt, rustfmt_skip)]
            #[cfg(not(any(
                feature = "alpn", feature = "ssl", feature = "tls", feature = "rust-tls")))]
            {
                ()
            }
        };

        #[cfg_attr(feature = "cargo-clippy", allow(let_unit_value))]
        ClientConnector::with_connector_impl(connector)
    }
}

impl ClientConnector {
    #[cfg(any(feature = "alpn", feature = "ssl"))]
    /// Create `ClientConnector` actor with custom `SslConnector` instance.
    ///
    /// By default `ClientConnector` uses very a simple SSL configuration.
    /// With `with_connector` method it is possible to use a custom
    /// `SslConnector` object.
    ///
    /// ```rust,ignore
    /// # #![cfg(feature="alpn")]
    /// # extern crate actix_web;
    /// # extern crate futures;
    /// # use futures::{future, Future};
    /// # use std::io::Write;
    /// # use actix_web::actix::Actor;
    /// extern crate openssl;
    /// use actix_web::{actix, client::ClientConnector, client::Connect};
    ///
    /// use openssl::ssl::{SslConnector, SslMethod};
    ///
    /// fn main() {
    ///     actix::run(|| {
    ///         // Start `ClientConnector` with custom `SslConnector`
    ///         let ssl_conn = SslConnector::builder(SslMethod::tls()).unwrap().build();
    ///         let conn = ClientConnector::with_connector(ssl_conn).start();
    ///
    ///         conn.send(
    ///             Connect::new("https://www.rust-lang.org").unwrap()) // <- connect to host
    ///                 .map_err(|_| ())
    ///                 .and_then(|res| {
    ///                     if let Ok(mut stream) = res {
    ///                         stream.write_all(b"GET / HTTP/1.0\r\n\r\n").unwrap();
    ///                     }
    /// #                   actix::System::current().stop();
    ///                     Ok(())
    ///                 })
    ///     });
    /// }
    /// ```
    pub fn with_connector(connector: SslConnector) -> ClientConnector {
        // keep level of indirection for docstrings matching featureflags
        Self::with_connector_impl(connector)
    }

    #[cfg(all(
        feature = "rust-tls",
        not(any(feature = "alpn", feature = "ssl", feature = "tls"))
    ))]
    /// Create `ClientConnector` actor with custom `SslConnector` instance.
    ///
    /// By default `ClientConnector` uses very a simple SSL configuration.
    /// With `with_connector` method it is possible to use a custom
    /// `SslConnector` object.
    ///
    /// ```rust
    /// # #![cfg(feature = "rust-tls")]
    /// # extern crate actix_web;
    /// # extern crate futures;
    /// # use futures::{future, Future};
    /// # use std::io::Write;
    /// # use actix_web::actix::Actor;
    /// extern crate rustls;
    /// extern crate webpki_roots;
    /// use actix_web::{actix, client::ClientConnector, client::Connect};
    ///
    /// use rustls::ClientConfig;
    /// use std::sync::Arc;
    ///
    /// fn main() {
    ///     actix::run(|| {
    ///         // Start `ClientConnector` with custom `ClientConfig`
    ///         let mut config = ClientConfig::new();
    ///         config
    ///             .root_store
    ///             .add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
    ///         let conn = ClientConnector::with_connector(config).start();
    ///
    ///         conn.send(
    ///             Connect::new("https://www.rust-lang.org").unwrap()) // <- connect to host
    ///                 .map_err(|_| ())
    ///                 .and_then(|res| {
    ///                     if let Ok(mut stream) = res {
    ///                         stream.write_all(b"GET / HTTP/1.0\r\n\r\n").unwrap();
    ///                     }
    /// #                   actix::System::current().stop();
    ///                     Ok(())
    ///                 })
    ///     });
    /// }
    /// ```
    pub fn with_connector(connector: ClientConfig) -> ClientConnector {
        // keep level of indirection for docstrings matching featureflags
        Self::with_connector_impl(SslConnector::from(Arc::new(connector)))
    }

    #[cfg(all(
        feature = "tls",
        not(any(feature = "ssl", feature = "alpn", feature = "rust-tls"))
    ))]
    /// Create `ClientConnector` actor with custom `SslConnector` instance.
    ///
    /// By default `ClientConnector` uses very a simple SSL configuration.
    /// With `with_connector` method it is possible to use a custom
    /// `SslConnector` object.
    ///
    /// ```rust
    /// # #![cfg(feature = "tls")]
    /// # extern crate actix_web;
    /// # extern crate futures;
    /// # use futures::{future, Future};
    /// # use std::io::Write;
    /// # use actix_web::actix::Actor;
    /// extern crate native_tls;
    /// extern crate webpki_roots;
    /// use native_tls::TlsConnector;
    /// use actix_web::{actix, client::ClientConnector, client::Connect};
    ///
    /// fn main() {
    ///     actix::run(|| {
    ///         let connector = TlsConnector::new().unwrap();
    ///         let conn = ClientConnector::with_connector(connector.into()).start();
    ///
    ///         conn.send(
    ///             Connect::new("https://www.rust-lang.org").unwrap()) // <- connect to host
    ///                 .map_err(|_| ())
    ///                 .and_then(|res| {
    ///                     if let Ok(mut stream) = res {
    ///                         stream.write_all(b"GET / HTTP/1.0\r\n\r\n").unwrap();
    ///                     }
    /// #                   actix::System::current().stop();
    ///                     Ok(())
    ///                 })
    ///     });
    /// }
    /// ```
    pub fn with_connector(connector: SslConnector) -> ClientConnector {
        // keep level of indirection for docstrings matching featureflags
        Self::with_connector_impl(connector)
    }

    #[inline]
    fn with_connector_impl(connector: SslConnector) -> ClientConnector {
        let (tx, rx) = mpsc::unbounded();

        ClientConnector {
            connector,
            stats: ClientConnectorStats::default(),
            subscriber: None,
            acq_tx: tx,
            acq_rx: Some(rx),
            resolver: None,
            conn_lifetime: Duration::from_secs(75),
            conn_keep_alive: Duration::from_secs(15),
            limit: 100,
            limit_per_host: 0,
            acquired: 0,
            acquired_per_host: HashMap::new(),
            available: HashMap::new(),
            to_close: Vec::new(),
            waiters: Some(HashMap::new()),
            wait_timeout: None,
            paused: Paused::No,
        }
    }

    /// Set total number of simultaneous connections.
    ///
    /// If limit is 0, the connector has no limit.
    /// The default limit size is 100.
    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = limit;
        self
    }

    /// Set total number of simultaneous connections to the same endpoint.
    ///
    /// Endpoints are the same if they have equal (host, port, ssl) triplets.
    /// If limit is 0, the connector has no limit. The default limit size is 0.
    pub fn limit_per_host(mut self, limit: usize) -> Self {
        self.limit_per_host = limit;
        self
    }

    /// Set keep-alive period for opened connection.
    ///
    /// Keep-alive period is the period between connection usage. If
    /// the delay between repeated usages of the same connection
    /// exceeds this period, the connection is closed.
    /// Default keep-alive period is 15 seconds.
    pub fn conn_keep_alive(mut self, dur: Duration) -> Self {
        self.conn_keep_alive = dur;
        self
    }

    /// Set max lifetime period for connection.
    ///
    /// Connection lifetime is max lifetime of any opened connection
    /// until it is closed regardless of keep-alive period.
    /// Default lifetime period is 75 seconds.
    pub fn conn_lifetime(mut self, dur: Duration) -> Self {
        self.conn_lifetime = dur;
        self
    }

    /// Subscribe for connector stats. Only one subscriber is supported.
    pub fn stats(mut self, subs: Recipient<ClientConnectorStats>) -> Self {
        self.subscriber = Some(subs);
        self
    }

    /// Use custom resolver actor
    ///
    /// By default actix's Resolver is used.
    pub fn resolver<A: Into<Recipient<ResolveConnect>>>(mut self, addr: A) -> Self {
        self.resolver = Some(addr.into());
        self
    }

    fn acquire(&mut self, key: &Key) -> Acquire {
        // check limits
        if self.limit > 0 {
            if self.acquired >= self.limit {
                return Acquire::NotAvailable;
            }
            if self.limit_per_host > 0 {
                if let Some(per_host) = self.acquired_per_host.get(key) {
                    if *per_host >= self.limit_per_host {
                        return Acquire::NotAvailable;
                    }
                }
            }
        } else if self.limit_per_host > 0 {
            if let Some(per_host) = self.acquired_per_host.get(key) {
                if *per_host >= self.limit_per_host {
                    return Acquire::NotAvailable;
                }
            }
        }

        self.reserve(key);

        // check if open connection is available
        // cleanup stale connections at the same time
        if let Some(ref mut connections) = self.available.get_mut(key) {
            let now = Instant::now();
            while let Some(conn) = connections.pop_back() {
                // check if it still usable
                if (now - conn.0) > self.conn_keep_alive
                    || (now - conn.1.ts) > self.conn_lifetime
                {
                    self.stats.closed += 1;
                    self.to_close.push(conn.1);
                } else {
                    let mut conn = conn.1;
                    let mut buf = [0; 2];
                    match conn.stream().read(&mut buf) {
                        Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => (),
                        Ok(n) if n > 0 => {
                            self.stats.closed += 1;
                            self.to_close.push(conn);
                            continue;
                        }
                        Ok(_) | Err(_) => continue,
                    }
                    return Acquire::Acquired(conn);
                }
            }
        }
        Acquire::Available
    }

    fn reserve(&mut self, key: &Key) {
        self.acquired += 1;
        let per_host = if let Some(per_host) = self.acquired_per_host.get(key) {
            *per_host
        } else {
            0
        };
        self.acquired_per_host.insert(key.clone(), per_host + 1);
    }

    fn release_key(&mut self, key: &Key) {
        if self.acquired > 0 {
            self.acquired -= 1;
        }
        let per_host = if let Some(per_host) = self.acquired_per_host.get(key) {
            *per_host
        } else {
            return;
        };
        if per_host > 1 {
            self.acquired_per_host.insert(key.clone(), per_host - 1);
        } else {
            self.acquired_per_host.remove(key);
        }
    }

    fn collect_periodic(&mut self, ctx: &mut Context<Self>) {
        // check connections for shutdown
        let mut idx = 0;
        while idx < self.to_close.len() {
            match AsyncWrite::shutdown(&mut self.to_close[idx]) {
                Ok(Async::NotReady) => idx += 1,
                _ => {
                    self.to_close.swap_remove(idx);
                }
            }
        }

        // re-schedule next collect period
        ctx.run_later(Duration::from_secs(1), |act, ctx| act.collect_periodic(ctx));

        // send stats
        let mut stats = mem::replace(&mut self.stats, ClientConnectorStats::default());
        if let Some(ref mut subscr) = self.subscriber {
            if let Some(ref waiters) = self.waiters {
                for w in waiters.values() {
                    stats.wait_queue += w.len();
                }
            }
            let _ = subscr.do_send(stats);
        }
    }

    // TODO: waiters should be sorted by deadline. maybe timewheel?
    fn collect_waiters(&mut self) {
        let now = Instant::now();
        let mut next = None;

        for waiters in self.waiters.as_mut().unwrap().values_mut() {
            let mut idx = 0;
            while idx < waiters.len() {
                let wait = waiters[idx].wait;
                if wait <= now {
                    self.stats.timeouts += 1;
                    let waiter = waiters.swap_remove_back(idx).unwrap();
                    let _ = waiter.tx.send(Err(ClientConnectorError::Timeout));
                } else {
                    if let Some(n) = next {
                        if wait < n {
                            next = Some(wait);
                        }
                    } else {
                        next = Some(wait);
                    }
                    idx += 1;
                }
            }
        }

        if next.is_some() {
            self.install_wait_timeout(next.unwrap());
        }
    }

    fn install_wait_timeout(&mut self, time: Instant) {
        if let Some(ref mut wait) = self.wait_timeout {
            if wait.0 < time {
                return;
            }
        }

        let mut timeout = Delay::new(time);
        let _ = timeout.poll();
        self.wait_timeout = Some((time, timeout));
    }

    fn wait_for(
        &mut self, key: Key, wait: Duration, conn_timeout: Duration,
    ) -> oneshot::Receiver<Result<Connection, ClientConnectorError>> {
        // connection is not available, wait
        let (tx, rx) = oneshot::channel();

        let wait = Instant::now() + wait;
        self.install_wait_timeout(wait);

        let waiter = Waiter {
            tx,
            wait,
            conn_timeout,
        };
        self.waiters
            .as_mut()
            .unwrap()
            .entry(key)
            .or_insert_with(VecDeque::new)
            .push_back(waiter);
        rx
    }

    fn check_availibility(&mut self, ctx: &mut Context<ClientConnector>) {
        // check waiters
        let mut act_waiters = self.waiters.take().unwrap();

        for (key, ref mut waiters) in &mut act_waiters {
            while let Some(waiter) = waiters.pop_front() {
                if waiter.tx.is_canceled() {
                    continue;
                }

                match self.acquire(key) {
                    Acquire::Acquired(mut conn) => {
                        // use existing connection
                        self.stats.reused += 1;
                        conn.pool =
                            Some(AcquiredConn(key.clone(), Some(self.acq_tx.clone())));
                        let _ = waiter.tx.send(Ok(conn));
                    }
                    Acquire::NotAvailable => {
                        waiters.push_front(waiter);
                        break;
                    }
                    Acquire::Available => {
                        // create new connection
                        self.connect_waiter(&key, waiter, ctx);
                    }
                }
            }
        }

        self.waiters = Some(act_waiters);
    }

    fn connect_waiter(&mut self, key: &Key, waiter: Waiter, ctx: &mut Context<Self>) {
        let key = key.clone();
        let conn = AcquiredConn(key.clone(), Some(self.acq_tx.clone()));

        let key2 = key.clone();
        fut::WrapFuture::<ClientConnector>::actfuture(
            self.resolver.as_ref().unwrap().send(
                ResolveConnect::host_and_port(&conn.0.host, conn.0.port)
                    .timeout(waiter.conn_timeout),
            ),
        ).map_err(move |_, act, _| {
            act.release_key(&key2);
            ()
        }).and_then(move |res, act, _| {
            #[cfg(any(feature = "alpn", feature = "ssl"))]
            match res {
                Err(err) => {
                    let _ = waiter.tx.send(Err(err.into()));
                    fut::Either::B(fut::err(()))
                }
                Ok(stream) => {
                    act.stats.opened += 1;
                    if conn.0.ssl {
                        fut::Either::A(
                            act.connector
                                .connect_async(&key.host, stream)
                                .into_actor(act)
                                .then(move |res, _, _| {
                                    match res {
                                        Err(e) => {
                                            let _ = waiter.tx.send(Err(
                                                ClientConnectorError::SslError(e),
                                            ));
                                        }
                                        Ok(stream) => {
                                            let _ = waiter.tx.send(Ok(Connection::new(
                                                conn.0.clone(),
                                                Some(conn),
                                                Box::new(stream),
                                            )));
                                        }
                                    }
                                    fut::ok(())
                                }),
                        )
                    } else {
                        let _ = waiter.tx.send(Ok(Connection::new(
                            conn.0.clone(),
                            Some(conn),
                            Box::new(stream),
                        )));
                        fut::Either::B(fut::ok(()))
                    }
                }
            }

            #[cfg(all(feature = "tls", not(any(feature = "alpn", feature = "ssl"))))]
            match res {
                Err(err) => {
                    let _ = waiter.tx.send(Err(err.into()));
                    fut::Either::B(fut::err(()))
                }
                Ok(stream) => {
                    act.stats.opened += 1;
                    if conn.0.ssl {
                        fut::Either::A(
                            act.connector
                                .connect(&conn.0.host, stream)
                                .into_actor(act)
                                .then(move |res, _, _| {
                                    match res {
                                        Err(e) => {
                                            let _ = waiter.tx.send(Err(
                                                ClientConnectorError::SslError(e),
                                            ));
                                        }
                                        Ok(stream) => {
                                            let _ = waiter.tx.send(Ok(Connection::new(
                                                conn.0.clone(),
                                                Some(conn),
                                                Box::new(stream),
                                            )));
                                        }
                                    }
                                    fut::ok(())
                                }),
                        )
                    } else {
                        let _ = waiter.tx.send(Ok(Connection::new(
                            conn.0.clone(),
                            Some(conn),
                            Box::new(stream),
                        )));
                        fut::Either::B(fut::ok(()))
                    }
                }
            }

            #[cfg(all(
                feature = "rust-tls",
                not(any(feature = "alpn", feature = "ssl", feature = "tls"))
            ))]
            match res {
                Err(err) => {
                    let _ = waiter.tx.send(Err(err.into()));
                    fut::Either::B(fut::err(()))
                }
                Ok(stream) => {
                    act.stats.opened += 1;
                    if conn.0.ssl {
                        let host = DNSNameRef::try_from_ascii_str(&key.host).unwrap();
                        fut::Either::A(
                            act.connector
                                .connect(host, stream)
                                .into_actor(act)
                                .then(move |res, _, _| {
                                    match res {
                                        Err(e) => {
                                            let _ = waiter.tx.send(Err(
                                                ClientConnectorError::SslError(e),
                                            ));
                                        }
                                        Ok(stream) => {
                                            let _ = waiter.tx.send(Ok(Connection::new(
                                                conn.0.clone(),
                                                Some(conn),
                                                Box::new(stream),
                                            )));
                                        }
                                    }
                                    fut::ok(())
                                }),
                        )
                    } else {
                        let _ = waiter.tx.send(Ok(Connection::new(
                            conn.0.clone(),
                            Some(conn),
                            Box::new(stream),
                        )));
                        fut::Either::B(fut::ok(()))
                    }
                }
            }

            #[cfg(not(any(
                feature = "alpn",
                feature = "ssl",
                feature = "tls",
                feature = "rust-tls"
            )))]
            match res {
                Err(err) => {
                    let _ = waiter.tx.send(Err(err.into()));
                    fut::err(())
                }
                Ok(stream) => {
                    act.stats.opened += 1;
                    if conn.0.ssl {
                        let _ =
                            waiter.tx.send(Err(ClientConnectorError::SslIsNotSupported));
                    } else {
                        let _ = waiter.tx.send(Ok(Connection::new(
                            conn.0.clone(),
                            Some(conn),
                            Box::new(stream),
                        )));
                    };
                    fut::ok(())
                }
            }
        }).spawn(ctx);
    }
}

impl Handler<Pause> for ClientConnector {
    type Result = ();

    fn handle(&mut self, msg: Pause, _: &mut Self::Context) {
        if let Some(time) = msg.time {
            let when = Instant::now() + time;
            let mut timeout = Delay::new(when);
            let _ = timeout.poll();
            self.paused = Paused::Timeout(when, timeout);
        } else {
            self.paused = Paused::Yes;
        }
    }
}

impl Handler<Resume> for ClientConnector {
    type Result = ();

    fn handle(&mut self, _: Resume, _: &mut Self::Context) {
        self.paused = Paused::No;
    }
}

impl Handler<Connect> for ClientConnector {
    type Result = ActorResponse<ClientConnector, Connection, ClientConnectorError>;

    fn handle(&mut self, msg: Connect, ctx: &mut Self::Context) -> Self::Result {
        let uri = &msg.uri;
        let wait_timeout = msg.wait_timeout;
        let conn_timeout = msg.conn_timeout;

        // host name is required
        if uri.host().is_none() {
            return ActorResponse::reply(Err(ClientConnectorError::InvalidUrl));
        }

        // supported protocols
        let proto = match uri.scheme_part() {
            Some(scheme) => match Protocol::from(scheme.as_str()) {
                Some(proto) => proto,
                None => {
                    return ActorResponse::reply(Err(ClientConnectorError::InvalidUrl))
                }
            },
            None => return ActorResponse::reply(Err(ClientConnectorError::InvalidUrl)),
        };

        // check ssl availability
        if proto.is_secure() && !HAS_OPENSSL && !HAS_TLS && !HAS_RUSTLS {
            return ActorResponse::reply(Err(ClientConnectorError::SslIsNotSupported));
        }

        let host = uri.host().unwrap().to_owned();
        let port = uri.port_part().map(|port| port.as_u16()).unwrap_or_else(|| proto.port());
        let key = Key {
            host,
            port,
            ssl: proto.is_secure(),
        };

        // check pause state
        if self.paused.is_paused() {
            let rx = self.wait_for(key.clone(), wait_timeout, conn_timeout);
            self.stats.waits += 1;
            return ActorResponse::async(
                rx.map_err(|_| ClientConnectorError::Disconnected)
                    .into_actor(self)
                    .and_then(move |res, act, ctx| match res {
                        Ok(conn) => fut::ok(conn),
                        Err(err) => {
                            match err {
                                ClientConnectorError::Timeout => (),
                                _ => {
                                    act.release_key(&key);
                                }
                            }
                            act.stats.errors += 1;
                            act.check_availibility(ctx);
                            fut::err(err)
                        }
                    }),
            );
        }

        // do not re-use websockets connection
        if !proto.is_http() {
            let (tx, rx) = oneshot::channel();
            let wait = Instant::now() + wait_timeout;
            let waiter = Waiter {
                tx,
                wait,
                conn_timeout,
            };
            self.connect_waiter(&key, waiter, ctx);

            return ActorResponse::async(
                rx.map_err(|_| ClientConnectorError::Disconnected)
                    .into_actor(self)
                    .and_then(move |res, act, ctx| match res {
                        Ok(conn) => fut::ok(conn),
                        Err(err) => {
                            act.stats.errors += 1;
                            act.release_key(&key);
                            act.check_availibility(ctx);
                            fut::err(err)
                        }
                    }),
            );
        }

        // acquire connection
        match self.acquire(&key) {
            Acquire::Acquired(mut conn) => {
                // use existing connection
                conn.pool = Some(AcquiredConn(key, Some(self.acq_tx.clone())));
                self.stats.reused += 1;
                ActorResponse::async(fut::ok(conn))
            }
            Acquire::NotAvailable => {
                // connection is not available, wait
                let rx = self.wait_for(key.clone(), wait_timeout, conn_timeout);
                self.stats.waits += 1;

                ActorResponse::async(
                    rx.map_err(|_| ClientConnectorError::Disconnected)
                        .into_actor(self)
                        .and_then(move |res, act, ctx| match res {
                            Ok(conn) => fut::ok(conn),
                            Err(err) => {
                                match err {
                                    ClientConnectorError::Timeout => (),
                                    _ => {
                                        act.release_key(&key);
                                    }
                                }
                                act.stats.errors += 1;
                                act.check_availibility(ctx);
                                fut::err(err)
                            }
                        }),
                )
            }
            Acquire::Available => {
                let (tx, rx) = oneshot::channel();
                let wait = Instant::now() + wait_timeout;
                let waiter = Waiter {
                    tx,
                    wait,
                    conn_timeout,
                };
                self.connect_waiter(&key, waiter, ctx);

                ActorResponse::async(
                    rx.map_err(|_| ClientConnectorError::Disconnected)
                        .into_actor(self)
                        .and_then(move |res, act, ctx| match res {
                            Ok(conn) => fut::ok(conn),
                            Err(err) => {
                                act.stats.errors += 1;
                                act.release_key(&key);
                                act.check_availibility(ctx);
                                fut::err(err)
                            }
                        }),
                )
            }
        }
    }
}

impl StreamHandler<AcquiredConnOperation, ()> for ClientConnector {
    fn handle(&mut self, msg: AcquiredConnOperation, ctx: &mut Context<Self>) {
        match msg {
            AcquiredConnOperation::Close(conn) => {
                self.release_key(&conn.key);
                self.to_close.push(conn);
                self.stats.closed += 1;
            }
            AcquiredConnOperation::Release(conn) => {
                self.release_key(&conn.key);
                if (Instant::now() - conn.ts) < self.conn_lifetime {
                    self.available
                        .entry(conn.key.clone())
                        .or_insert_with(VecDeque::new)
                        .push_back(Conn(Instant::now(), conn));
                } else {
                    self.to_close.push(conn);
                    self.stats.closed += 1;
                }
            }
            AcquiredConnOperation::ReleaseKey(key) => {
                // closed
                self.stats.closed += 1;
                self.release_key(&key);
            }
        }

        self.check_availibility(ctx);
    }
}

struct Maintenance;

impl fut::ActorFuture for Maintenance {
    type Item = ();
    type Error = ();
    type Actor = ClientConnector;

    fn poll(
        &mut self, act: &mut ClientConnector, ctx: &mut Context<ClientConnector>,
    ) -> Poll<Self::Item, Self::Error> {
        // check pause duration
        if let Paused::Timeout(inst, _) = act.paused {
            if inst <= Instant::now() {
                act.paused = Paused::No;
            }
        }

        // collect wait timers
        act.collect_waiters();

        // check waiters
        act.check_availibility(ctx);

        Ok(Async::NotReady)
    }
}

#[derive(PartialEq, Hash, Debug, Clone, Copy)]
enum Protocol {
    Http,
    Https,
    Ws,
    Wss,
}

impl Protocol {
    fn from(s: &str) -> Option<Protocol> {
        match s {
            "http" => Some(Protocol::Http),
            "https" => Some(Protocol::Https),
            "ws" => Some(Protocol::Ws),
            "wss" => Some(Protocol::Wss),
            _ => None,
        }
    }

    fn is_http(self) -> bool {
        match self {
            Protocol::Https | Protocol::Http => true,
            _ => false,
        }
    }

    fn is_secure(self) -> bool {
        match self {
            Protocol::Https | Protocol::Wss => true,
            _ => false,
        }
    }

    fn port(self) -> u16 {
        match self {
            Protocol::Http | Protocol::Ws => 80,
            Protocol::Https | Protocol::Wss => 443,
        }
    }
}

#[derive(Hash, Eq, PartialEq, Clone, Debug)]
struct Key {
    host: String,
    port: u16,
    ssl: bool,
}

impl Key {
    fn empty() -> Key {
        Key {
            host: String::new(),
            port: 0,
            ssl: false,
        }
    }
}

#[derive(Debug)]
struct Conn(Instant, Connection);

enum Acquire {
    Acquired(Connection),
    Available,
    NotAvailable,
}

enum AcquiredConnOperation {
    Close(Connection),
    Release(Connection),
    ReleaseKey(Key),
}

struct AcquiredConn(Key, Option<mpsc::UnboundedSender<AcquiredConnOperation>>);

impl AcquiredConn {
    fn close(&mut self, conn: Connection) {
        if let Some(tx) = self.1.take() {
            let _ = tx.unbounded_send(AcquiredConnOperation::Close(conn));
        }
    }
    fn release(&mut self, conn: Connection) {
        if let Some(tx) = self.1.take() {
            let _ = tx.unbounded_send(AcquiredConnOperation::Release(conn));
        }
    }
}

impl Drop for AcquiredConn {
    fn drop(&mut self) {
        if let Some(tx) = self.1.take() {
            let _ = tx.unbounded_send(AcquiredConnOperation::ReleaseKey(self.0.clone()));
        }
    }
}

/// HTTP client connection
pub struct Connection {
    key: Key,
    stream: Box<IoStream + Send>,
    pool: Option<AcquiredConn>,
    ts: Instant,
}

impl fmt::Debug for Connection {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Connection {}:{}", self.key.host, self.key.port)
    }
}

impl Connection {
    fn new(key: Key, pool: Option<AcquiredConn>, stream: Box<IoStream + Send>) -> Self {
        Connection {
            key,
            stream,
            pool,
            ts: Instant::now(),
        }
    }

    /// Raw IO stream
    pub fn stream(&mut self) -> &mut IoStream {
        &mut *self.stream
    }

    /// Create a new connection from an IO Stream
    ///
    /// The stream can be a `UnixStream` if the Unix-only "uds" feature is enabled.
    ///
    /// See also `ClientRequestBuilder::with_connection()`.
    pub fn from_stream<T: IoStream + Send>(io: T) -> Connection {
        Connection::new(Key::empty(), None, Box::new(io))
    }

    /// Close connection
    pub fn close(mut self) {
        if let Some(mut pool) = self.pool.take() {
            pool.close(self)
        }
    }

    /// Release this connection to the connection pool
    pub fn release(mut self) {
        if let Some(mut pool) = self.pool.take() {
            pool.release(self)
        }
    }
}

impl IoStream for Connection {
    fn shutdown(&mut self, how: Shutdown) -> io::Result<()> {
        IoStream::shutdown(&mut *self.stream, how)
    }

    #[inline]
    fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()> {
        IoStream::set_nodelay(&mut *self.stream, nodelay)
    }

    #[inline]
    fn set_linger(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
        IoStream::set_linger(&mut *self.stream, dur)
    }

    #[inline]
    fn set_keepalive(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
        IoStream::set_keepalive(&mut *self.stream, dur)
    }
}

impl io::Read for Connection {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.stream.read(buf)
    }
}

impl AsyncRead for Connection {}

impl io::Write for Connection {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.stream.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.stream.flush()
    }
}

impl AsyncWrite for Connection {
    fn shutdown(&mut self) -> Poll<(), io::Error> {
        self.stream.shutdown()
    }
}

#[cfg(feature = "tls")]
use tokio_tls::TlsStream;

#[cfg(feature = "tls")]
/// This is temp solution untile actix-net migration
impl<Io: IoStream> IoStream for TlsStream<Io> {
    #[inline]
    fn shutdown(&mut self, _how: Shutdown) -> io::Result<()> {
        let _ = self.get_mut().shutdown();
        Ok(())
    }

    #[inline]
    fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()> {
        self.get_mut().get_mut().set_nodelay(nodelay)
    }

    #[inline]
    fn set_linger(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
        self.get_mut().get_mut().set_linger(dur)
    }

    #[inline]
    fn set_keepalive(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
        self.get_mut().get_mut().set_keepalive(dur)
    }
}