我有一个客户端尝试连接到服务器.
我需要能够终止客户端并中止此拨号尝试.这可能吗 ?我怎么能这样做?
超时显然超过30秒,因为测试阻止直到30秒过去而没有拨号呼叫失败.
我们可以自己指定超时吗?
在net.Dialer有Timeout和Deadline领域,也可以使用上下文与DialContext它允许超时和取消.
您可以参考以DialTimeout了解如何设置基本拨号器:
func DialTimeout(network, address string, timeout time.Duration) (Conn, error) {
d := Dialer{Timeout: timeout}
return d.Dial(network, address)
}
Run Code Online (Sandbox Code Playgroud)
并举例说明context.Context:
var d Dialer
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return d.DialContext(ctx, network, address)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
794 次 |
| 最近记录: |