blob: ea16b4c0f8697265cb29fefe96c59a8e7ebbe604 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// Copyright 2015 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package transport
16
17import (
18 "net"
19 "net/http"
20 "time"
21)
22
23// NewTimeoutTransport returns a transport created using the given TLS info.
24// If read/write on the created connection blocks longer than its time limit,
25// it will return timeout error.
26// If read/write timeout is set, transport will not be able to reuse connection.
27func NewTimeoutTransport(info TLSInfo, dialtimeoutd, rdtimeoutd, wtimeoutd time.Duration) (*http.Transport, error) {
28 tr, err := NewTransport(info, dialtimeoutd)
29 if err != nil {
30 return nil, err
31 }
32
33 if rdtimeoutd != 0 || wtimeoutd != 0 {
34 // the timed out connection will timeout soon after it is idle.
35 // it should not be put back to http transport as an idle connection for future usage.
36 tr.MaxIdleConnsPerHost = -1
37 } else {
38 // allow more idle connections between peers to avoid unnecessary port allocation.
39 tr.MaxIdleConnsPerHost = 1024
40 }
41
42 tr.Dial = (&rwTimeoutDialer{
43 Dialer: net.Dialer{
44 Timeout: dialtimeoutd,
45 KeepAlive: 30 * time.Second,
46 },
47 rdtimeoutd: rdtimeoutd,
48 wtimeoutd: wtimeoutd,
49 }).Dial
50 return tr, nil
51}