@@ -31,7 +31,7 @@ type LargeConnectionStats struct {
3131// 复用缓冲区
3232var bufPool = sync.Pool {
3333 New : func () interface {} {
34- return make ([]byte , 8192 )
34+ return make ([]byte , 4096 )
3535 },
3636}
3737
@@ -183,8 +183,10 @@ func (cs *ConnectionStats) handleUDPConnection(localConn *net.UDPConn, remoteAdd
183183 cs .TotalBytesLock .Unlock ()
184184
185185 // 处理消息的边界和错误情况
186- go cs .forwardUDPMessage (localConn , remoteAddr , buf [:n ])
187- bufPool .Put (buf [:n ])
186+ go func () {
187+ cs .forwardUDPMessage (localConn , remoteAddr , buf [:n ])
188+ bufPool .Put (& buf )
189+ }()
188190 }
189191 }
190192}
@@ -199,11 +201,12 @@ func (cs *ConnectionStats) forwardUDPMessage(localConn *net.UDPConn, remoteAddr
199201 if err != nil {
200202 fmt .Println ("写入目标时发生错误:" , err )
201203 }
204+
202205}
203206
204207func (cs * ConnectionStats ) copyBytes (dst , src net.Conn ) {
205208 buf := bufPool .Get ().([]byte )
206- defer bufPool .Put (buf )
209+ defer bufPool .Put (& buf )
207210 for {
208211 n , err := src .Read (buf )
209212 if n > 0 {
@@ -231,7 +234,7 @@ func (cs *ConnectionStats) copyBytes(dst, src net.Conn) {
231234
232235// 定时打印和处理流量变化
233236func (cs * ConnectionStats ) printStats (ctx context.Context ) {
234- ticker := time .NewTicker (5 * time .Second )
237+ ticker := time .NewTicker (10 * time .Second )
235238 defer ticker .Stop () // 在函数结束时停止定时器
236239 for {
237240 select {
@@ -286,8 +289,9 @@ func (cs *ConnectionStats) printStats(ctx context.Context) {
286289func closeTCPConnections (stats * ConnectionStats ) {
287290 stats .TotalBytesLock .Lock ()
288291 defer stats .TotalBytesLock .Unlock ()
289- for _ , conn := range stats .TCPConnections {
292+ for i , conn := range stats .TCPConnections {
290293 conn .Close ()
294+ stats .TCPConnections [i ] = nil
291295 }
292296 stats .TCPConnections = nil // 清空切片
293297}
0 commit comments