GoLang 中 当 Client 关闭链接后, Server 如何处理。
2018-01-19 15:15
495 查看
时间: 2018-01-19
最近, 使用 beego 框架开发了一些 Web应用,
Beego中的 "Serverlet" 基本, 都长类似的样子
// @router /follow [post]
func (ctl *EventController) GetEvents() {
..........
}
有时候,服务的时间, 比较长。 比如这样的:
for {
line, err := rr.ReadBytes('\n')
if err != nil { //if io.EOF == err {
break
}
................
}
服务器端的循环,如何退出?
go func() {
w := ctl.Ctx.ResponseWriter
<-http.CloseNotifier(w).CloseNotify()
ctl.Ctx.ResponseWriter.Flush()
..................
}()
可以用这种办法。
具体 golang 中方法说明如下:
// CloseNotify returns a channel that receives at most a
// single value (true) when the client connection has gone
// away.
//
// CloseNotify may wait to notify until Request.Body has been
// fully read.
//
// After the Handler has returned, there is no guarantee
// that the channel receives a value.
//
// If the protocol is HTTP/1.1 and CloseNotify is called while
// processing an idempotent request (such a GET) while
// HTTP/1.1 pipelining is in use, the arrival of a subsequent
// pipelined request may cause a value to be sent on the
// returned channel. In practice HTTP/1.1 pipelining is not
// enabled in browsers and not seen often in the wild. If this
// is a problem, use HTTP/2 or only use CloseNotify on methods
// such as POST.
CloseNotify() <-chan bool
也就是说, CloseNotify 返回一个通道变量, 当客户端关闭的时候,通道可读。
最近, 使用 beego 框架开发了一些 Web应用,
Beego中的 "Serverlet" 基本, 都长类似的样子
// @router /follow [post]
func (ctl *EventController) GetEvents() {
..........
}
有时候,服务的时间, 比较长。 比如这样的:
for {
line, err := rr.ReadBytes('\n')
if err != nil { //if io.EOF == err {
break
}
................
}
服务器端的循环,如何退出?
go func() {
w := ctl.Ctx.ResponseWriter
<-http.CloseNotifier(w).CloseNotify()
ctl.Ctx.ResponseWriter.Flush()
..................
}()
可以用这种办法。
具体 golang 中方法说明如下:
// CloseNotify returns a channel that receives at most a
// single value (true) when the client connection has gone
// away.
//
// CloseNotify may wait to notify until Request.Body has been
// fully read.
//
// After the Handler has returned, there is no guarantee
// that the channel receives a value.
//
// If the protocol is HTTP/1.1 and CloseNotify is called while
// processing an idempotent request (such a GET) while
// HTTP/1.1 pipelining is in use, the arrival of a subsequent
// pipelined request may cause a value to be sent on the
// returned channel. In practice HTTP/1.1 pipelining is not
// enabled in browsers and not seen often in the wild. If this
// is a problem, use HTTP/2 or only use CloseNotify on methods
// such as POST.
CloseNotify() <-chan bool
也就是说, CloseNotify 返回一个通道变量, 当客户端关闭的时候,通道可读。
相关文章推荐
- 如果芯片作为TCP_Server,最多可以链接几个Client,应该如何操作?
- 当SocketServer断开或发生Wifi网络变化的时候,SocketClient应该如何处理异常。
- socket链接的关闭close和shutdown的区别_TIME_WAIT和CLOSE_WAIT什么时刻出现_如何处理
- socket链接的关闭close和shutdown的区别,TIME_WAIT和CLOSE_WAIT什么时刻出现,如何处理
- socket链接的关闭close和shutdown的区别_TIME_WAIT和CLOSE_WAIT什么时刻出现_如何处理
- WCF体系架构(之一:Client与Server信息交互处理流程初略介绍)
- Mysql连接报错:1130-host ... is not allowed to connect to this MySql server如何处理
- Redis2.2.2源码学习——Server&Client链接的建立以及相关Event
- 如何对链接了mysqlclient的程序静态编译?
- Java线程的传说(3)——如何关闭一个正在accept的ServerSocket?
- 客户端意外挂掉,Server如何处理?
- TCP连接的状态与关闭方式,及其对Server与Client的影响
- 【链接】C++编译器是如何实现异常处理的
- 如何关闭Golang中的HTTP连接 How to Close Golang's HTTP connection
- Redis2.2.2源码学习——Server&Client链接的建立以及相关Event
- TCP连接的状态与关闭方式,及其对Server与Client的影响
- 如何高效的利用dbus做client-server架构
- [Golang] 从零开始写Socket Server(1): Socket-Client框架
- HBase中的Client如何路由到正确的RegionServer
- IOS高访新浪微博界面(讲解如何自定义UITableViewCell,处理@#链接 特殊字符)