本文整理汇总了Golang中net/http.Transport.DisableKeepAlives方法的典型用法代码示例。如果您正苦于以下问题:Golang Transport.DisableKeepAlives方法的具体用法?Golang Transport.DisableKeepAlives怎么用?Golang Transport.DisableKeepAlives使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http.Transport
的用法示例。
在下文中一共展示了Transport.DisableKeepAlives方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getClientConfig
func (d *Daemon) getClientConfig() (*clientConfig, error) {
var (
transport *http.Transport
scheme string
addr string
proto string
)
if d.UseDefaultTLSHost {
option := &tlsconfig.Options{
CAFile: "fixtures/https/ca.pem",
CertFile: "fixtures/https/client-cert.pem",
KeyFile: "fixtures/https/client-key.pem",
}
tlsConfig, err := tlsconfig.Client(*option)
if err != nil {
return nil, err
}
transport = &http.Transport{
TLSClientConfig: tlsConfig,
}
addr = fmt.Sprintf("%s:%d", opts.DefaultHTTPHost, opts.DefaultTLSHTTPPort)
scheme = "https"
proto = "tcp"
} else if d.UseDefaultHost {
addr = opts.DefaultUnixSocket
proto = "unix"
scheme = "http"
transport = &http.Transport{}
} else {
addr = d.sockPath()
proto = "unix"
scheme = "http"
transport = &http.Transport{}
}
if err := sockets.ConfigureTransport(transport, proto, addr); err != nil {
return nil, err
}
transport.DisableKeepAlives = true
return &clientConfig{
transport: transport,
scheme: scheme,
addr: addr,
}, nil
}
示例2: newHttpTransporter
func newHttpTransporter() (trans *httpTransporter) {
tr := new(http.Transport)
tr.DisableCompression = true
tr.DisableKeepAlives = false
tr.MaxIdleConnsPerHost = 4
jar := cookieJar
if DisableGlobalCookie {
jar, _ = cookiejar.New(nil)
}
client := new(http.Client)
client.Jar = jar
client.Transport = tr
trans = new(httpTransporter)
trans.Client = client
trans.Header = new(http.Header)
return
}
示例3: NewClient
// NewClient creates an http client for the specific host
func NewClient(host string) (*http.Client, error) {
// FIXME(vdemeester) 10*time.Second timeout of SockRequest… ?
proto, addr, _, err := dclient.ParseHost(host)
if err != nil {
return nil, err
}
transport := new(http.Transport)
if proto == "tcp" && os.Getenv("DOCKER_TLS_VERIFY") != "" {
// Setup the socket TLS configuration.
tlsConfig, err := getTLSConfig()
if err != nil {
return nil, err
}
transport = &http.Transport{TLSClientConfig: tlsConfig}
}
transport.DisableKeepAlives = true
err = sockets.ConfigureTransport(transport, proto, addr)
return &http.Client{
Transport: transport,
}, err
}
示例4: robot
// Goroutine to run a robot
func robot() {
var payload interface{}
transport := new(http.Transport)
transport.DisableKeepAlives = true
client := &http.Client{Transport: transport}
id := uuid.New()
// login
resp, err := client.PostForm(msgurl,
url.Values{"command": {"login"}, "id": {id}})
if err != nil {
log.Printf("http login error: %v", err)
return
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Printf("http login error: %v", err)
return
}
// extract payload
err = json.Unmarshal(body, &payload)
if err != nil {
log.Printf("json unmarshal error: %v", err)
log.Printf("json data: %s", string(body))
return
}
outerObj := payload.(map[string]interface{})
respType := outerObj["type"].(string)
// check for login errors
if respType == "status" {
status := outerObj["status"].(string)
if status == "error" {
message := outerObj["message"].(string)
log.Printf("chat server login error: %s", message)
return
}
}
// get additional user data
//publicId := outerObj["publicid"].(string)
//userName := outerObj["username"].(string)
// run the poller
go robotPoller(id)
msgCount := 0
// main loop
for {
// send chat
resp, err = client.PostForm(msgurl,
url.Values{
"command": {"broadcast"},
"id": {id},
"message": {fmt.Sprintf("message %d", msgCount)},
})
msgCount++
if err != nil {
log.Printf("http broadcast error: %v", err)
return
}
body, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
// check for broadcast errors
if respType == "status" {
status := outerObj["status"].(string)
if status == "error" {
message := outerObj["message"].(string)
log.Printf("chat server broadcast error: %s", message)
return
}
}
// sleep a bit
duration := rand.Int31n(int32(maxDelay-minDelay)) + int32(minDelay)
time.Sleep(time.Duration(duration) * time.Millisecond)
transport.CloseIdleConnections()
}
}
示例5: robotPoller
// Goroutine to run a long poll thread for a robot
func robotPoller(id string) {
var payload interface{}
transport := new(http.Transport)
transport.DisableKeepAlives = true
client := &http.Client{Transport: transport}
for {
// send in a poll request and wait for response
resp, err := client.PostForm(pollurl,
url.Values{"id": {id}})
if err != nil {
log.Printf("poller post error: %v", err)
return
}
//log.Printf("************** %v %v", resp, err)
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Printf("http poll request error: %v", err)
return
}
// extract payload
err = json.Unmarshal(body, &payload)
if err != nil {
log.Printf("json poll unmarshal error: %v", err)
log.Printf("json poll data: %s", string(body))
return
}
outerObj := payload.([]interface{})
for _, v := range outerObj {
message := v.(map[string]interface{})
respType := message["type"].(string)
// check for login errors
if respType == "status" {
status := message["status"].(string)
if status == "error" {
message := message["message"].(string)
log.Printf("chat server pollrequest error: %s", message)
return
}
}
// parse message
switch respType {
case "message":
//log.Printf("%s: message: %s: %s", id, message["username"].(string), message["message"].(string))
case "newuser":
//log.Printf("%s: new user: %s", id, message["username"].(string))
default:
log.Printf("%s: unknown type: %s", id, respType)
}
}
transport.CloseIdleConnections()
}
}