GoLang(cgp) http.Client 从 Linux 守护进程获取请求挂起
GoLang(cgp) http.Client get request hangs from Linux daemon process
我编写了一个简单的 GoLang 函数,它使用 http.Client 发出 HTTP GET 请求并打印响应字符串。然后,我使用以下命令将其导出为 .so 文件中的 C 函数。
go build -o libapp.so -buildmode=c-shared libapp.go
然后使用这个 .SO 和 .h,从我的 C 测试程序中调用导出的 Go 函数。下面是我用来编译和构建我的 C 测试程序的命令。
gcc -o goclient goclient.c ./libapp.so
这是 GoLang 中的 CheckConnection 函数
var file io.Writer
//export CheckConnection
func CheckConnection() {
path := "/opt/logs/gologs.txt"
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
file = f
log.SetOutput(file)
log.Print("---------Logging started---------")
log.Print("Hello from CheckConnection\r\n")
log.Print("CheckConnection creating http.Client: step1")
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
log.Print("CheckConnection creating http.Client: step2")
var myClient = &http.Client{
Timeout: time.Second * 10,
Transport: netTransport,
}
log.Print("CheckConnection creating http.Client: step3")
res,err := myClient.Get("https://www.google.com")
log.Print("CheckConnection creating http.Client: step4")
if err!= nil {
log.Print("Error ",err)
}
log.Print("CheckConnection creating http.Client: step5")
data,_ := ioutil.ReadAll(res.Body)
log.Print("CheckConnection creating http.Client: step6")
res.Body.Close()
log.Print("CheckConnection creating http.Client: step7")
log.Print("Resp code ",res.StatusCode)
log.Print("Data ",data)
defer log.Print("Returning from CheckConnection");
}
线程在下一行被阻塞,没有超时或报错 -
res,err := myClient.Get("https://www.google.com")
当从守护进程调用 CheckConnection() 函数时会发生这种情况。如果我从 C、C++ main() 进程调用这个函数,那么它就可以工作。我在 GitHub https://github.com/golang/go/issues/47077 上报告了同样的问题。它具有重现问题的示例代码 (Sample.zip)。
请参阅 GitHub 问题 https://github.com/golang/go/issues/47077 以获取示例可重现代码。
下面添加go env
GO111MODULE=""
GOARCH="386"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOINSECURE="golang.org,googlesource.com"
GOMODCACHE="/home/nilesh/golang/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/nilesh/golang"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_386"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
GO386="sse2"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/nilesh/SVN/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m32 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build718813889=/tmp/go-build -gno-record-gcc-switches"
默认情况下,DefaultClient 没有超时。您可以尝试使用自己的客户端设置超时吗?
var myClient = &http.Client{
Timeout: time.Second * 10,
}
在 linux 上的 C/C++ 程序中使用 cgp 共享库时,如果您从分叉进程调用这些函数,请确保您应该考虑以下几点。
不要静态 link 共享库,而是使用 dlopen、dlsym、dlclose 动态加载它。
确保在执行上述操作时您没有在 linking
期间提供共享库名称
这解决了我的问题,我可以从我的分叉进程进行网络调用。
我编写了一个简单的 GoLang 函数,它使用 http.Client 发出 HTTP GET 请求并打印响应字符串。然后,我使用以下命令将其导出为 .so 文件中的 C 函数。
go build -o libapp.so -buildmode=c-shared libapp.go
然后使用这个 .SO 和 .h,从我的 C 测试程序中调用导出的 Go 函数。下面是我用来编译和构建我的 C 测试程序的命令。
gcc -o goclient goclient.c ./libapp.so
这是 GoLang 中的 CheckConnection 函数
var file io.Writer
//export CheckConnection
func CheckConnection() {
path := "/opt/logs/gologs.txt"
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
file = f
log.SetOutput(file)
log.Print("---------Logging started---------")
log.Print("Hello from CheckConnection\r\n")
log.Print("CheckConnection creating http.Client: step1")
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
log.Print("CheckConnection creating http.Client: step2")
var myClient = &http.Client{
Timeout: time.Second * 10,
Transport: netTransport,
}
log.Print("CheckConnection creating http.Client: step3")
res,err := myClient.Get("https://www.google.com")
log.Print("CheckConnection creating http.Client: step4")
if err!= nil {
log.Print("Error ",err)
}
log.Print("CheckConnection creating http.Client: step5")
data,_ := ioutil.ReadAll(res.Body)
log.Print("CheckConnection creating http.Client: step6")
res.Body.Close()
log.Print("CheckConnection creating http.Client: step7")
log.Print("Resp code ",res.StatusCode)
log.Print("Data ",data)
defer log.Print("Returning from CheckConnection");
}
线程在下一行被阻塞,没有超时或报错 -
res,err := myClient.Get("https://www.google.com")
当从守护进程调用 CheckConnection() 函数时会发生这种情况。如果我从 C、C++ main() 进程调用这个函数,那么它就可以工作。我在 GitHub https://github.com/golang/go/issues/47077 上报告了同样的问题。它具有重现问题的示例代码 (Sample.zip)。
请参阅 GitHub 问题 https://github.com/golang/go/issues/47077 以获取示例可重现代码。
下面添加go env
GO111MODULE=""
GOARCH="386"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOINSECURE="golang.org,googlesource.com"
GOMODCACHE="/home/nilesh/golang/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/nilesh/golang"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_386"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
GO386="sse2"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/nilesh/SVN/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m32 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build718813889=/tmp/go-build -gno-record-gcc-switches"
默认情况下,DefaultClient 没有超时。您可以尝试使用自己的客户端设置超时吗?
var myClient = &http.Client{
Timeout: time.Second * 10,
}
在 linux 上的 C/C++ 程序中使用 cgp 共享库时,如果您从分叉进程调用这些函数,请确保您应该考虑以下几点。
不要静态 link 共享库,而是使用 dlopen、dlsym、dlclose 动态加载它。
确保在执行上述操作时您没有在 linking
期间提供共享库名称
这解决了我的问题,我可以从我的分叉进程进行网络调用。