设置客户端连接linger为0

This commit is contained in:
GoEdgeLab
2021-10-20 22:32:02 +08:00
parent e4be419c54
commit aa7f917678
4 changed files with 30 additions and 24 deletions

View File

@@ -42,17 +42,23 @@ func init() {
})
}
// TrafficConn 用于统计流量的连接
type TrafficConn struct {
// ClientConn 客户端连接
type ClientConn struct {
rawConn net.Conn
isClosed bool
}
func NewTrafficConn(conn net.Conn) net.Conn {
return &TrafficConn{rawConn: conn}
func NewClientConn(conn net.Conn) net.Conn {
tcpConn, ok := conn.(*net.TCPConn)
if ok {
// TODO 可以设置此值
_ = tcpConn.SetLinger(0)
}
func (this *TrafficConn) Read(b []byte) (n int, err error) {
return &ClientConn{rawConn: conn}
}
func (this *ClientConn) Read(b []byte) (n int, err error) {
n, err = this.rawConn.Read(b)
if n > 0 {
atomic.AddUint64(&inTrafficBytes, uint64(n))
@@ -60,7 +66,7 @@ func (this *TrafficConn) Read(b []byte) (n int, err error) {
return
}
func (this *TrafficConn) Write(b []byte) (n int, err error) {
func (this *ClientConn) Write(b []byte) (n int, err error) {
n, err = this.rawConn.Write(b)
if n > 0 {
atomic.AddUint64(&outTrafficBytes, uint64(n))
@@ -68,31 +74,31 @@ func (this *TrafficConn) Write(b []byte) (n int, err error) {
return
}
func (this *TrafficConn) Close() error {
func (this *ClientConn) Close() error {
this.isClosed = true
return this.rawConn.Close()
}
func (this *TrafficConn) LocalAddr() net.Addr {
func (this *ClientConn) LocalAddr() net.Addr {
return this.rawConn.LocalAddr()
}
func (this *TrafficConn) RemoteAddr() net.Addr {
func (this *ClientConn) RemoteAddr() net.Addr {
return this.rawConn.RemoteAddr()
}
func (this *TrafficConn) SetDeadline(t time.Time) error {
func (this *ClientConn) SetDeadline(t time.Time) error {
return this.rawConn.SetDeadline(t)
}
func (this *TrafficConn) SetReadDeadline(t time.Time) error {
func (this *ClientConn) SetReadDeadline(t time.Time) error {
return this.rawConn.SetReadDeadline(t)
}
func (this *TrafficConn) SetWriteDeadline(t time.Time) error {
func (this *ClientConn) SetWriteDeadline(t time.Time) error {
return this.rawConn.SetWriteDeadline(t)
}
func (this *TrafficConn) IsClosed() bool {
func (this *ClientConn) IsClosed() bool {
return this.isClosed
}

View File

@@ -8,16 +8,16 @@ import (
"net"
)
// TrafficListener 用于统计流量的网络监听
type TrafficListener struct {
// ClientListener 客户端网络监听
type ClientListener struct {
rawListener net.Listener
}
func NewTrafficListener(listener net.Listener) net.Listener {
return &TrafficListener{rawListener: listener}
func NewClientListener(listener net.Listener) net.Listener {
return &ClientListener{rawListener: listener}
}
func (this *TrafficListener) Accept() (net.Conn, error) {
func (this *ClientListener) Accept() (net.Conn, error) {
conn, err := this.rawListener.Accept()
if err != nil {
return nil, err
@@ -33,13 +33,13 @@ func (this *TrafficListener) Accept() (net.Conn, error) {
}
}
return NewTrafficConn(conn), nil
return NewClientConn(conn), nil
}
func (this *TrafficListener) Close() error {
func (this *ClientListener) Close() error {
return this.rawListener.Close()
}
func (this *TrafficListener) Addr() net.Addr {
func (this *ClientListener) Addr() net.Addr {
return this.rawListener.Addr()
}

View File

@@ -20,7 +20,7 @@ func (this *HTTPRequest) doWAFRequest() (blocked bool) {
// 当前连接是否已关闭
var conn = this.RawReq.Context().Value(HTTPConnContextKey)
if conn != nil {
trafficConn, ok := conn.(*TrafficConn)
trafficConn, ok := conn.(*ClientConn)
if ok && trafficConn.IsClosed() {
this.disableLog = true
return true
@@ -32,7 +32,7 @@ func (this *HTTPRequest) doWAFRequest() (blocked bool) {
this.disableLog = true
if conn != nil {
trafficConn, ok := conn.(*TrafficConn)
trafficConn, ok := conn.(*ClientConn)
if ok && !trafficConn.IsClosed() {
_ = trafficConn.Close()
}

View File

@@ -59,7 +59,7 @@ func (this *Listener) listenTCP() error {
if err != nil {
return err
}
netListener = NewTrafficListener(netListener)
netListener = NewClientListener(netListener)
events.On(events.EventQuit, func() {
remotelogs.Println("LISTENER", "quit "+this.group.FullAddr())
_ = netListener.Close()