dousong5492 2016-04-11 05:08 采纳率: 100%
浏览 87

几个请求后,go http客户端失败

I am using golang's net/http package in my application. In my use case I am trying to make http calls several times continuously using my custom http client. I am facing following issues with this.

Issue 1: After several calls [ ~100 less than a minute], client fails to make calls.I mean the request is not hitting the server.

Here is my custom client code,

package serviceclient

import (
    "bytes"
    "io/ioutil"
    "net"
    "net/http"
    "time"
    "log"
)

const(
    HTTP_DIAL_TIME_OUT          = 5    //seconds
    HTTP_TLS_HANDSHAKE_TIME_OUT = 5
    HTTP_CLIENT_TIME_OUT        = 10
)

// RequestService requests the api through the service URL
func RequestService(methodType string, serviceURL string, data string, contentType string) string {
    log.Printf("Request string %s", data)
    dataBytes := bytes.NewBufferString(data)
    req, err := http.NewRequest(methodType, serviceURL, dataBytes)
    if err != nil {
        log.Printf("Client request creation issue")
    }
    req.Header.Set("Content-Type", contentType)

    client := getHTTPClient()
    res, err := client.Do(req)
    log.Printf("Response Object original : %s", res)
    if err != nil {
        log.Printf("Could not make the client request")
    }
    defer res.Body.Close()
    responseBytes, err := ioutil.ReadAll(res.Body)
    if err != nil {
        log.Printf("Response string conversion issue")
    }
    responseString := string(responseBytes[:])
    return responseString
}

func getHTTPClient() *http.Client {
    var netTransport = &http.Transport{
          Dial: (&net.Dialer{
            Timeout: HTTP_DIAL_TIME_OUT * time.Second,
          }).Dial,
          TLSHandshakeTimeout: HTTP_TLS_HANDSHAKE_TIME_OUT * time.Second,
        }
    var client = &http.Client{
          Timeout: time.Second * HTTP_CLIENT_TIME_OUT,
          Transport: netTransport,
        }
    return client
}

Issue 2: Also every time the client tries to make calls even after successful response with an empty request[Not with the original request]. I mean when I call the RequestService() function it makes 5 extra calls even after getting a successful response. But the request value is undefined for those calls.

Here is my console output[I printed the http request and few info] for one http call,

Got in appserver ops api  &{GET /api/services/appserverops/ HTTP/1.1 1 1 map[Content-Type:[application/json] Accept-Encoding:[gzip] User-Agent:[Go-http-client/1.1] Content-Length:[160]] 0xc820398180 160 [] false localhost:8081 map[] map[] <nil> map[] [::1]:57455 /api/services/appserverops/ <nil> <nil>}
appserveropslogic :: Getappserver() :: request Wg
appserveropslogic :: Getappserver() :: response &{{127 https://www.youtube.com/watch?v=0SMoqP9eCwM https://www.youtube.com/embed/0SMoqP9eCwM 1} {44 www.github.com/docker 1} 2016-04-09 05:38:32 {} 2016-04-09 05:38:32 [0xc82000c4c0 0xc8200d4da0 0xc82000c600] {1005   0 0    0    []} false}

Got in appserver ops api  &{GET /api/services/appserverops/ HTTP/1.1 1 1 map[User-Agent:[Go-http-client/1.1] Content-Length:[167] Content-Type:[application/json] Accept-Encoding:[gzip]] 0xc8203f4800 167 [] false localhost:8081 map[] map[] <nil> map[] [::1]:57455 /api/services/appserverops/ <nil> <nil>}
appserveropslogic :: Getappserver() :: request undefined
appserveropslogic :: Getappserver() :: response <nil>

Got in appserver ops api  &{GET /api/services/appserverops/ HTTP/1.1 1 1 map[User-Agent:[Go-http-client/1.1] Content-Length:[167] Content-Type:[application/json] Accept-Encoding:[gzip]] 0xc8203f4880 167 [] false localhost:8081 map[] map[] <nil> map[] [::1]:57455 /api/services/appserverops/ <nil> <nil>}
appserveropslogic :: Getappserver() :: request undefined
appserveropslogic :: Getappserver() :: response <nil>

Got in appserver ops api  &{GET /api/services/appserverops/ HTTP/1.1 1 1 map[User-Agent:[Go-http-client/1.1] Content-Length:[167] Content-Type:[application/json] Accept-Encoding:[gzip]] 0xc8203f49c0 167 [] false localhost:8081 map[] map[] <nil> map[] [::1]:57455 /api/services/appserverops/ <nil> <nil>}
appserveropslogic :: Getappserver() :: request undefined
appserveropslogic :: Getappserver() :: response <nil>

Got in appserver ops api  &{GET /api/services/appserverops/ HTTP/1.1 1 1 map[User-Agent:[Go-http-client/1.1] Content-Length:[167] Content-Type:[application/json] Accept-Encoding:[gzip]] 0xc8203f4a40 167 [] false localhost:8081 map[] map[] <nil> map[] [::1]:57455 /api/services/appserverops/ <nil> <nil>}
appserveropslogic :: Getappserver() :: request undefined
appserveropslogic :: Getappserver() :: response <nil>

Got in appserver ops api  &{GET /api/services/appserverops/ HTTP/1.1 1 1 map[User-Agent:[Go-http-client/1.1] Content-Length:[167] Content-Type:[application/json] Accept-Encoding:[gzip]] 0xc8203f4b00 167 [] false localhost:8081 map[] map[] <nil> map[] [::1]:57455 /api/services/appserverops/ <nil> <nil>}
appserveropslogic :: Getappserver() :: request undefined
appserveropslogic :: Getappserver() :: response <nil>

Got in appserver ops api  &{GET /api/services/appserverops/ HTTP/1.1 1 1 map[User-Agent:[Go-http-client/1.1] Content-Length:[167] Content-Type:[application/json] Accept-Encoding:[gzip]] 0xc8203f4c00 167 [] false localhost:8081 map[] map[] <nil> map[] [::1]:57455 /api/services/appserverops/ <nil> <nil>}
appserveropslogic :: Getappserver() :: request undefined
appserveropslogic :: Getappserver() :: response <nil>

Got in appserver ops api  &{GET /api/services/appserverops/ HTTP/1.1 1 1 map[User-Agent:[Go-http-client/1.1] Content-Length:[167] Content-Type:[application/json] Accept-Encoding:[gzip]] 0xc8203f4c80 167 [] false localhost:8081 map[] map[] <nil> map[] [::1]:57455 /api/services/appserverops/ <nil> <nil>}
appserveropslogic :: Getappserver() :: request undefined
appserveropslogic :: Getappserver() :: response <nil>

Am I doing anything wrong? Could someone help me with this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程