macos - Golang 交叉编译的 OSX 二进制文件在 net/http.Get() 中使用 VPN 时挂起

标签 macos http go cross-compiling vpn

我创建了一个简单的 go 程序,它执行 HTTP GET 请求并打印执行该请求所花费的时间:

package main

import (
    "log"
    "net/http"
    "time"
)

func main() {
    start := time.Now()
    http.Get("https://google.com")
    log.Printf("Elapsed: %v", time.Since(start))
}

在 OSX (Sierra 10.12.1) 上本地构建时,执行请求所用的时间是合理的(<500 毫秒)。

$ for i in `seq 1 10`; do ./httpgettest ; done
2017/08/15 14:20:44 Elapsed: 525.989928ms
2017/08/15 14:20:45 Elapsed: 479.785494ms
2017/08/15 14:20:45 Elapsed: 477.800294ms
2017/08/15 14:20:46 Elapsed: 494.060461ms
2017/08/15 14:20:46 Elapsed: 477.368627ms
2017/08/15 14:20:47 Elapsed: 454.152783ms
2017/08/15 14:20:47 Elapsed: 463.760254ms
2017/08/15 14:20:48 Elapsed: 470.52473ms
2017/08/15 14:20:48 Elapsed: 461.632058ms
2017/08/15 14:20:49 Elapsed: 465.769262ms

然后我从 golang Linux docker 容器交叉编译相同的程序:

GOOS=darwin GOARCH=amd64 go build -v -o httpgettest-xcompiled

然而,当在 VPN 中执行生成的二进制文件时,耗时总是超过 10 秒:

$ for i in `seq 1 10`; do ./httpgettest-xcompiled ; done
2017/08/15 14:19:43 Elapsed: 10.532037349s
2017/08/15 14:19:54 Elapsed: 10.525551121s
2017/08/15 14:20:04 Elapsed: 10.572715005s
2017/08/15 14:20:15 Elapsed: 10.532407157s
2017/08/15 14:20:25 Elapsed: 10.54083169s
2017/08/15 14:20:36 Elapsed: 10.625399239s
2017/08/15 14:20:46 Elapsed: 10.539333467s
2017/08/15 14:20:57 Elapsed: 10.533211943s
2017/08/15 14:21:07 Elapsed: 10.539430574s
2017/08/15 14:21:18 Elapsed: 10.527510134s

一旦我断开与 VPN 的连接,数字就会下降。

有人可以解释一下交叉编译的二进制文件在使用 VPN 时速度很慢,但 mac native 编译版本始终性能良好吗?这可以使用 golang 1.7.6、1.8.3 和 1.9 docker 容器重现。

我使用 Tunnelblick 连接到 OpenVPN 服务器。

最佳答案

看起来 Go 在本地编译时使用系统的 native DNS 解析代码(通过 C 库),但在交叉编译时使用 Go 自己用 Go 编写的解析器。作为确认,当 CGO_ENABLED=0 时听起来很慢。虽然它不是专门针对您在这里的体验,https://github.com/golang/go/issues/16345在快速搜索中出现并提到 CGO_ENABLED 如何确定运行哪个解析器。

不过,我不确定您是如何编译要跨平台使用 cgo native 解析器的二进制文件(可能至少需要安装 C 交叉编译器)或让解析器在 Go 中正常工作您的 Darwin VPN 设置。

关于macos - Golang 交叉编译的 OSX 二进制文件在 net/http.Get() 中使用 VPN 时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45702591/

相关文章:

c# - 向 Stripe 发出原始 HTTP POST 请求时如何发送 Stripe 的 key

performance - 为什么与strconv.Atoi相比,strconv.ParseUint这么慢?

node.js - 如何修复 "dyld: Library not loaded:/usr/lib/libc++.1.dylib Referenced from:/usr/local/bin/node Reason: image not found Trace/BPT trap"

ruby - ruby 2.3.1 缺少库

Javac版本没有更新

php - yii2 rest api异常处理

apache - .htaccess http 到 https 和 mvc 查询字符串

arrays - 在golang中转储接口(interface)数组

memory-leaks - 这会导致 Go 中的内存泄漏吗?

bash - 如何使用 sed 在 mac 终端中查找和替换字符串?