网络通信中,为了防止长时间无响应的情况,经常会用到网络连接超时、读写超时的设置。

本文结合例子简介golang的连接超时和读写超时设置。

1.超时设置

1.1 连接超时

func DialTimeout(network, address string, timeout time.Duration) (Conn, error)

第三个参数timeout可以用来设置连接超时设置。

如果超过timeout的指定的时间,连接没有完成,会返回超时错误。

1.2 读写超时

Conn定义中,包括读写的超时时间设置。

type Conn interface {
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
... ...
SetDeadline(t time.Time) error // SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
// A zero value for t means Read will not time out.
SetReadDeadline(t time.Time) error // SetWriteDeadline sets the deadline for future Write calls
// and any currently-blocked Write call.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
SetWriteDeadline(t time.Time) error
}

通过上面的函数说明,可以得知,这里的参数t是一个未来的时间点,所以每次读或写之前,都要调用SetXXX重新设置超时时间,

如果只设置一次,就会出现总是超时的问题。

2.例子

2.1 server

server端监听连接,如果收到连接请求,就是创建一个goroutine负责这个连接的数据收发。

为了测试超时,我们在写操作之前,sleep 3s。

package main

import (
"net"
"log"
"time"
) func main() {
addr := "0.0.0.0:8080" tcpAddr, err := net.ResolveTCPAddr("tcp",addr) if err != nil {
log.Fatalf("net.ResovleTCPAddr fail:%s", addr)
} listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Fatalf("listen %s fail: %s", addr, err)
} else {
log.Println("listening", addr)
} for {
conn, err := listener.Accept()
if err != nil {
log.Println("listener.Accept error:", err)
continue
} go handleConnection(conn)
} } func handleConnection(conn net.Conn) {
defer conn.Close() var buffer []byte = []byte("You are welcome. I'm server.") for {
time.Sleep(3*time.Second)// sleep 3s
n, err := conn.Write(buffer)
if err != nil {
log.Println("Write error:", err)
break
}
log.Println("send:", n)
} log.Println("connetion end") }

2.2 client

client建立连接时,使用的超时时间是3s。

创建连接成功后,设置连接的读超时。

每次读之前,都重新设置超时时间。

package main

import (
"log"
"net"
"os"
"time"
) func main() {
connTimeout := 3*time.Second
conn, err := net.DialTimeout("tcp", "127.0.0.1:8080", connTimeout) // 3s timeout
if err != nil {
log.Println("dial failed:", err)
os.Exit(1)
}
defer conn.Close() readTimeout := 2*time.Second buffer := make([]byte, 512) for {
err = conn.SetReadDeadline(time.Now().Add(readTimeout)) // timeout
if err != nil {
log.Println("setReadDeadline failed:", err)
} n, err := conn.Read(buffer)
if err != nil {
log.Println("Read failed:", err)
//break
} log.Println("count:", n, "msg:", string(buffer))
} }

输出结果

2019/05/12 16:18:19 Read failed: read tcp 127.0.0.1:51718->127.0.0.1:8080: i/o timeout
2019/05/12 16:18:19 count: 0 msg:
2019/05/12 16:18:20 count: 28 msg: You are welcome. I'm server.
2019/05/12 16:18:22 Read failed: read tcp 127.0.0.1:51718->127.0.0.1:8080: i/o timeout
2019/05/12 16:18:22 count: 0 msg: You are welcome. I'm server.
2019/05/12 16:18:23 count: 28 msg: You are welcome. I'm server.
2019/05/12 16:18:25 Read failed: read tcp 127.0.0.1:51718->127.0.0.1:8080: i/o timeout
2019/05/12 16:18:25 count: 0 msg: You are welcome. I'm server.
2019/05/12 16:18:26 count: 28 msg: You are welcome. I'm server.

golang网络通信超时设置的更多相关文章

  1. golang RPC通信读写超时设置

    golang RPC通信中,有时候就怕读写hang住. 那是否可以设置读写超时呢? 1.方案一: 设置连接的读写超时 1.1 client RPC通信基于底层网络通信,可以通过设置connection ...

  2. delphi tidhttp 超时设置无效的解决方法

    现在delphi都发布到xe8了,tidhttp还有缺陷,那就是超时设置在没有网络或者连不上服务器的时候是无效的,不管你设置为多少都要10-20秒.connectTimeout和readTimeout ...

  3. Linux串口中的超时设置

    在Linux下使用串口通信时,默认的阻塞模式是不实用的.而采用select或epoll机制的非阻塞模式,写代码有比较麻烦.幸好Linux的串口自己就带有超时机制. Linux下使用termios.h中 ...

  4. org.apache.http.client.HttpClient; HttpClient 4.3超时设置

    可用的code import org.apache.commons.lang.StringUtils;import org.apache.http.HttpEntity;import org.apac ...

  5. HttpClient 3.X 4.3 4.x超时设置

    HttpClient 4.3.HttpClient这货和Lucene一样,每个版本的API都变化很大,这有点让人头疼.就好比创建一个HttpClient对象吧,每一个版本的都不一样, 3.X是这样的 ...

  6. Apache性能优化、超时设置,linux 重启apache

    在httpd.conf中去掉Include conf/extra/httpd-default.conf前的#以使httpd-default.php生效.其中调节以下参数Timeout 15 (连接超时 ...

  7. libcurl多线程超时设置不安全(转)

    from http://www.cnblogs.com/kex1n/p/4135263.html (1), 超时(timeout) libcurl 是 一个很不错的库,支持http,ftp等很多的协议 ...

  8. golang chan 超时

    golang chan 超时 Posted on 2013-12-24 13:03 oathleo 阅读(4227) 评论(0)  编辑  收藏 package main import (    &q ...

  9. CXF超时设置

    转自: http://peak.iteye.com/blog/1285211 http://win.sy.blog.163.com/blog/static/9419718620131014385644 ...

随机推荐

  1. pl_sql例题

    0. 准备工作: set serveroutput on hellowrold 程序 begindbms_output.put_line('hello world');end;/ [语法格式]--de ...

  2. 第十篇.2、python并发编程之多进程

    一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.P ...

  3. ShuffleNet系列学习笔记

    ShuffleNet是旷世提出的高效轻量化网络,是一款很值得一提的轻量化网络,其相关论文也是很有价值的. ShuffleNet V1 该网络提出于2017年,论文为<ShuffleNet: An ...

  4. 微软内部封杀 Slack

    就在 Slack 在股市上亮相之际,有一家大公司却不允许员工在日常工作中使用这款企业协作和聊天应用软件. 微软已禁止其 100000 多名员工使用免费版 Slack.IT 外媒 GeekWire 报道 ...

  5. label smooth

    图像分类的一个trick,推导可参考这位博主https://leimao.github.io/blog/Label-Smoothing/ 知乎上的讨论https://www.zhihu.com/que ...

  6. zencart只有购买过此产品的客户才能评价产品

    当前登录的客户买过此产品时,才显示评价按钮: <?php $rev_query = "select count(*) as count from orders o ,orders_pr ...

  7. JavaScript 的 this 指向问题深度解析

    与我们常见的很多语言不同,JavaScript 函数中的 this 指向并不是在函数定义的时候确定的,而是在调用的时候确定的.换句话说,函数的调用方式决定了 this 指向. JavaScript 中 ...

  8. springboot-activiti TaskLISTener无法注入service

    转自CSDN :https://blog.csdn.net/Laiguanfu/article/details/89366193 第一步创建springUtil类 @Componentpublic c ...

  9. 清北学堂dp图论营游记day3

    .状态压缩dp: 对于这个我们引入二进制状态压缩,因为任何一个数都可以二进制表示,而其二进制表示上每一位都可以表示当前位置是否有元素,这就构成了状态压缩. 对于这个题,上下行&一下就行. 状压 ...

  10. C++使用 new 声明动态数组

    int main() { using namespace std; int* p = new int[3]; // new运算符返回第一个元素的地址. p[0] = 10; p[1] = 9; p[2 ...