From a3e34ee9fba073d192b59666032313f334ad674a Mon Sep 17 00:00:00 2001 From: MahnoKropotkinvich Date: Tue, 24 Dec 2024 00:10:01 +0800 Subject: [PATCH] tried better way of using io.Copy --- conn.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/conn.go b/conn.go index 6591d11..f3b5bb4 100644 --- a/conn.go +++ b/conn.go @@ -4,11 +4,26 @@ import ( "io" "net" "sync" - //"time" + "time" ) -func handle(client net.Conn) { - defer client.Close() +type timeoutConn struct{ + conn net.Conn + +} +func (c timeoutConn) Read(buf []byte) (int,error) { + c.conn.SetDeadline(time.Now().Add(time.Duration(config.ConnectTimeout)*time.Second)) + return c.conn.Read(buf) +} +func (c timeoutConn) Write(buf []byte) (int,error) { + c.conn.SetDeadline(time.Now().Add(time.Duration(config.ConnectTimeout)*time.Second)) + return c.conn.Write(buf) +} + + +func handle(clientOriginal net.Conn) { + client := timeoutConn{clientOriginal} + defer clientOriginal.Close() queryChan := make(QueryChan) ChanChan <- queryChan proceed := func() { @@ -37,7 +52,7 @@ func handle(client net.Conn) { }() wg.Wait() - GetLogger().Infof("Connection from %s closed", client.RemoteAddr()) + GetLogger().Infof("Connection from %s closed", clientOriginal.RemoteAddr()) } switch state { case RUNNING: