From 1d0865ca49ff50d2b81d3c0d39f680f763310eae Mon Sep 17 00:00:00 2001 From: fatedier Date: Thu, 1 Feb 2018 11:15:35 +0800 Subject: [PATCH] statsConn: avoid repetition of close function --- utils/net/conn.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/utils/net/conn.go b/utils/net/conn.go index 78319cc..b187733 100644 --- a/utils/net/conn.go +++ b/utils/net/conn.go @@ -21,6 +21,7 @@ import ( "io" "net" "sync" + "sync/atomic" "time" "github.com/fatedier/frp/utils/log" @@ -178,6 +179,7 @@ func (sc *SharedConn) WriteBuff(buffer []byte) (err error) { type StatsConn struct { Conn + closed int64 // 1 means closed totalRead int64 totalWrite int64 statsFunc func(totalRead, totalWrite int64) @@ -203,9 +205,12 @@ func (statsConn *StatsConn) Write(p []byte) (n int, err error) { } func (statsConn *StatsConn) Close() (err error) { - err = statsConn.Conn.Close() - if statsConn.statsFunc != nil { - statsConn.statsFunc(statsConn.totalRead, statsConn.totalWrite) + old := atomic.SwapInt64(&statsConn.closed, 1) + if old != 1 { + err = statsConn.Conn.Close() + if statsConn.statsFunc != nil { + statsConn.statsFunc(statsConn.totalRead, statsConn.totalWrite) + } } return }