-
Notifications
You must be signed in to change notification settings - Fork 339
/
poller_type_http.go
135 lines (120 loc) · 4.34 KB
/
poller_type_http.go
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
package poller
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/apache/trafficcontrol/lib/go-log"
"github.com/apache/trafficcontrol/traffic_monitor/config"
)
const PollerTypeHTTP = "http"
func init() {
AddPollerType(PollerTypeHTTP, httpGlobalInit, httpInit, httpPoll)
}
func httpGlobalInit(cfg config.Config, appData config.StaticAppData) interface{} {
sharedClient := &http.Client{
Transport: &http.Transport{},
Timeout: cfg.HTTPTimeout,
}
return &HTTPPollGlobalCtx{
UserAgent: appData.UserAgent,
Client: sharedClient,
FormatAccept: cfg.HTTPPollingFormat,
}
}
func httpInit(cfg PollerConfig, globalCtxI interface{}) interface{} {
gctx := (globalCtxI).(*HTTPPollGlobalCtx)
if cfg.Timeout != 0 || cfg.NoKeepAlive { // if the timeout isn't explicitly set, use the template value.
clientCopy := *gctx.Client
gctx.Client = &clientCopy // copy the client, so it's reused by pollers who DO use the default timeout/keepalive
if cfg.Timeout != 0 {
gctx.Client.Timeout = cfg.Timeout
}
if cfg.NoKeepAlive {
transportI := http.DefaultTransport
transport, ok := transportI.(*http.Transport)
if !ok {
log.Errorf("failed to set NoKeepAlive for poller ID '%s': http.DefaultTransport expected type *http.Transport actual %T\n", cfg.PollerID, transportI)
} else {
transport.DisableKeepAlives = cfg.NoKeepAlive
gctx.Client.Transport = transport
log.Infof("Setting transport.DisableKeepAlives %t for %s\n", transport.DisableKeepAlives, cfg.PollerID)
}
}
}
return &HTTPPollCtx{
Client: gctx.Client,
UserAgent: gctx.UserAgent,
NoKeepAlive: cfg.NoKeepAlive,
PollerID: cfg.PollerID,
FormatAccept: gctx.FormatAccept,
}
}
type HTTPPollGlobalCtx struct {
Client *http.Client
UserAgent string
FormatAccept string
}
type HTTPPollCtx struct {
Client *http.Client
UserAgent string
NoKeepAlive bool
PollerID string
HTTPHeader http.Header
FormatAccept string
}
func httpPoll(ctxI interface{}, url string, host string, pollID uint64) ([]byte, time.Time, time.Duration, error) {
ctx := (ctxI).(*HTTPPollCtx)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, time.Now(), 0, errors.New("creating HTTP request: " + err.Error())
}
req.Header.Set("User-Agent", ctx.UserAgent)
if !ctx.NoKeepAlive {
req.Header.Set("Connection", "keep-alive")
}
req.Header.Set("Accept", ctx.FormatAccept)
req.Host = host
startReq := time.Now()
resp, err := ctx.Client.Do(req)
if err != nil {
reqEnd := time.Now()
reqTime := reqEnd.Sub(startReq) // note this is the time to transfer the entire body, not just the roundtrip
return nil, reqEnd, reqTime, fmt.Errorf("id %v url %v fetch error: %v", ctx.PollerID, url, err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
reqEnd := time.Now()
reqTime := reqEnd.Sub(startReq) // note this is the time to transfer the entire body, not just the roundtrip
return nil, reqEnd, reqTime, fmt.Errorf("id %v url %v fetch error: bad HTTP status: %v", ctx.PollerID, url, resp.StatusCode)
}
bts, err := ioutil.ReadAll(resp.Body)
if err != nil {
reqEnd := time.Now()
reqTime := reqEnd.Sub(startReq) // note this is the time to transfer the entire body, not just the roundtrip
return nil, reqEnd, reqTime, fmt.Errorf("id %v url %v fetch error: reading body: %v", ctx.PollerID, url, err)
}
reqEnd := time.Now()
reqTime := reqEnd.Sub(startReq) // note this is the time to transfer the entire body, not just the roundtrip
ctx.HTTPHeader = resp.Header.Clone()
return bts, reqEnd, reqTime, nil
}